Professional Documents
Culture Documents
Chapter2notes Incomplete
Chapter2notes Incomplete
Constructors
● In Java, a constructor is a block of codes similar
to the method.
● It is called when an instance of the object is
created(new), and memory is allocated for the
object.
● It is called constructor because it constructs
the values at the time of object creation.
● It is not necessary to write a constructor for a
class. It is because java compiler creates a
default constructor if your class doesn't have
any.
efault Constructor
D
A constructor is called "Default Constructor" when
it doesn't have any parameter.
class Rectangle
{
Rectangle()
{
System.out.println("reclangle is created");
}
Parameterized Constructor
A constructor which has a specific number of
parameters is called a parameterized constructor.
class Rectangle
{
int length;
int width;
class paraconstruct
{
public static void main(String args[])
{
//creating objects and passing values
Rectangle r = new Rectangle(111,78);
r.calculate();
}
}
class Nesting
{
int length;
int width;
int area;
void display()
{
calculate();
System.out.println("area of rectangle="+area);
}
for(int i=0;i<count;i++)
System.out.println(args[i]);
}
}
javac commandline.java
java commandline 10 20 30
10
20
30
example2-
class commandline
{
public static void main(String args[])
{
int a,b,sum;
a=Integer.parseInt(args[1]); //"10" convert to 10
and it will store in a
b=Integer.parseInt(args[0]); //"20" convert to 20
and it will store in b
sum=a+b;
System.out.println(“sum=”sum);
}
}
javac commandline.java
java commandline 10 20
sum=30
Vrags-variable arguments
● Let’s suppose you are creating a Java method.
However, you are not sure how many arguments
your method is going to accept. To address this
problem, Java introduced varargs.
● Varargs is a short name for variable
arguments. In Java, an argument of a method
can accept arbitrary number of values. This
argument that can accept variable number of
values is called varargs.
● The syntax for implementing varargs is as
follows:
accessModifier methodName(datatype… arg)
{
// method body
}
In order to define vararg, ... (three dots) is used in
the formal parameter of a method.
for eg.
class Varg
{
void display(int ... arr)
{
for(int x: arr)
{
System.out.println(x);
}
v.display(2, 4);
System.out.println("---------------------------------");
v.display(1, 3, 5);
}
}
this keyword
There can be a lot of usage of java this keyword. In
java, this is a reference variable that refers to the
current object.
void calculate()
{
int area=l*w;
System.out.println("area of rectangle="+area);
}
}
class thisprogram
{
public static void main(String args[])
{
Rectangle r = new Rectangle(10,20);
r.calculate();
}
}
Access Modifiers/specifiers/visibility control in
java
Access Modifiers the name suggests access
modifiers in Java helps to restrict the scope of a
class, constructor , variable , method or data
member. There are four types of access modifiers
available in java:
1.Default(friendly) – No keyword required
2.Private
3.Protected
4.Public
ublic: The public access modifier is specified
p
using the keyword public.
Java Array
Normally, an array is a collection of similar type of
elements that have a contiguous memory location.
Java array is an object which contains elements of
a similar data type.
It is a data structure where we store similar elements.
We can store only a fixed set of elements in a Java
array.
Array in java is index-based, the first element of the
array is stored at the 0 index.
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];
//declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
class Testarray1
{
public static void main(String args[])
{
int a[]={33,3,4,5};
//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)
//length is the property of array
System.out.println(a[i]);
} }
What is String in java
Generally, String is a sequence of characters. But in
Java, string is an object that represents a sequence of
characters.
How to create a string object?
There are two ways to create String object:
1.By string literal
2.By new keyword
1) String Literal
Java String literal is created by using double quotes.
For Example:
1.String s="welcome";
) By new keyword
String functions
1)charAt()-
The java string charAt() method returns a char
value at the given index number.
public class CharAtExample
{
public static void main(String args[]){
String name="javatpoint";
2)length()
It returns count of total number of characters.
public class LengthExample
{
public static void main(String args[])
{
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//
10 is the length of javatpoint string
System.out.println("string length is: "+s2.length());//
6 is the length of python string
}}
output:
string length is: 10
string length is: 6
3)substring()
substring() method returns a part of the string.
public class SubstringExample{
public static void main(String args[]){
String s1="javatpoint";
System.out.println(s1.substring(2,4));//returns va
System.out.println(s1.substring(2));//returns vatpoint
}}
output:
vat
vatpoint
4)concat
concat() method combines specified string at the
end of this string. It returns combined string. It is
like appending another string.
System.out.println(s1);
}}
output:
java string is immutable so assign it explicitly
5)replace()
output:
my name was abc.
6)indexOf()
indexOf() method returns index of given character
value or substring. If it is not found, it returns -1.
The index counter starts from zero.
public class IndexOfExample2
{
public static void main(String[] args)
{
String s1 = "This is indexOf method";
// Passing Substring
int index = s1.indexOf("method"); //Returns the in
dex of this substring
output:
index of substring 16
7)toLowerCase()
toLowerCase() method returns the string in
lowercase letter. In other words, it converts all
characters of the string into lower case letter.
8)toUpperCase()
}}
Output:
30
Wrapper class
Wrapper class in java provides the mechanism t o
convert primitive data type into object and object
into primitive data type.
The automatic conversion of primitive into object is
known as autoboxing and vice-versa unboxing.
The eight classes of java.lang package are known as
wrapper classes in java. The list of eight wrapper
classes are given below:
Inheritance
● Inheritance is a mechanism in which one class
acquires the property of another class. For
example, a child inherits the traits of his/her
parents.
● With inheritance, we can reuse the fields and
methods of the existing class.
● The parent class is termed super class and the
inherited class is the sub class
● The keyword "extend" is used by the sub class
to inherit the features of super class
Types of Inheritance
There are Various types of inheritance in Java:
1.Single Inheritance:
In Single Inheritance one class extends another
class (one class only).
In above diagram, Class B extends only Class A.
Class A is a super class and Class B is a
Sub-class.
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.sala
ry);
System.out.println("Bonus of Programmer is:"+p.b
onus);
}
}
Programmer salary is:40000.0
Bonus of programmer is:10000
2.Multiple Inheritance:
In Multiple Inheritance, one class extending
more than one class. Java does not support
multiple inheritance.
As per above diagram, Class C extends Class A
and Class B both.
3.Multilevel Inheritance:
In Multilevel Inheritance, one class can inherit
from a derived class. Hence, the derived class
becomes the base class for the new class.
As per shown in diagram Class C is subclass of
B and B is a of subclass Class A.
4.Hierarchical Inheritance:
In Hierarchical Inheritance, one class is
inherited by many sub classes.
As per above example, Class B, C, and D inherit
the same class A.
5.Hybrid Inheritance:
Hybrid inheritance is a combination of Single
and Multiple inheritance.