Chapter 2 (18 Marks) : Constructors
Chapter 2 (18 Marks) : Constructors
Chapter 2 (18 Marks) : Constructors
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.
Default Constructor
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;
//creating a parameterized constructor
Rectangle(int l,int w)
{
length = l;
width = w;
}
void calculate()
{
int area=length*width;
System.out.println("area of rectangle="+area);
}
}
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 calculate()
{
area=length*width;
}
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();
}
}
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]);
} }
1) String Literal
Java String literal is created by using double quotes.
For Example:
1. String s="welcome";
) By new keyword
1. String s=new String("Welcome");
Example
public class StringExample
{
public static void main(String args[])
{
String s1="java";
//creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);
//converting char array to string
String s3=new String("example");
//creating java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}}
output:
java
strings
example
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";
char ch=name.charAt(9);//returns the char value at th
e 9th index
System.out.println(ch);
}}
output- t
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 vatp
oint
}}
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 th
e index of this substring
System.out.println("index of substring "+in
dex);
}
}
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:
HELLO STRING
9)valueOf()
valueOf() method converts different types of values
into string. By the help of string valueOf() method,
you can convert int to string, long to string, boolean
to string, character to string, float to string, double to
string, object to string and char array to string.
public class StringValueOfExample
{
public static void main(String args[])
{
int value=30;
String s1=String.valueOf(value);
System.out.println(s1);//concatenating string wit
h 10
}}
Output:
30
Wrapper class
Wrapper class in java provides the mechanism to
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).
4. Hierarchical Inheritance:
In Hierarchical Inheritance, one class is
inherited by many sub classes.