Derived Syntactical Constructs in Java: (Type Here)
Derived Syntactical Constructs in Java: (Type Here)
CONSTRUCTS IN JAVA
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
Example:
class Bike1{
Bike1()
{
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
Example:
class Student4{
int id;
String name;
name = n;
}
void display()
{ System.out.println(id+" "+name);}
public static void main(String args[])
{
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Example:
class Student6{
int id;
String name;
Student6(int i,String n)
{
id = i;
name = n;
}
Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1); //copy constructor called
s1.display();
s2.display();
}
}
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
Constructor:
a) A constructor is a special method which initializes an object
immediately upon creation
b) It has the same name as class name in which it resides and
it is syntactically similar to any method.
c) When a constructor is not defined, java executes a default
constructor which initializes all numeric members to zero and
other types to null or spaces.
d) Once defined, constructor is automatically called immediately
after the object is created before new operator completes.
e) Constructors do not have return value, but they don‟t require
“void” as implicit data type as data type of class constructor
is the class type itself.
Parameterized constructor:
It is used to pass the values while creating the objects
Example:
class Rect
{
int length, breadth;
Rect(int l, int b) // parameterized constructor
{
length=l;
breadth=b;
}
public static void main(String args[])
{
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
Example:
ClassRect
{
int length, breadth;
Rect(int l, int b) // parameterized constructor
{
length=l;
breadth=b;
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
}
public static void main(String args[])
{
Rect r = new Rect(4,5); // constructor with parameters
Rect r1 = new Rect(6,7);
System.out.println(“Area : ” +(r.length*r.breadth)); // o/p Area : 20
System.out.println(“Area : ” +(r1.length*r1.breadth)); // o/p Area :
42
}
}
General Form :
protected void finalize()
{ // finalization code here
}
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
General Form:
protected void finalize()
{ // finalization code here
}
E.g
The following parent class uses protected access control, to
allow its child class override
protected void show( )
{}
e) private protected:
Variables, methods which are declared protected in a super
class can be accessed only by the subclasses in same
package. It means visible to class and its subclasses.
Example:
private protected void show( )
{}
{
protected void show()
{
System.out.println("Hello java");
}
}
test1.java
importmypack.test;
class test1 extends test
{
public static void main(String args[])
{
test1obj=new test1();
obj.show();
}
}
}
}
a) length():
Syntax: int length()
It is used to return length of given string in integer.
Eg. String str=”INDIA”
System.out.println(str);
System.out.println(str.length()); // Returns 5
b) charAt():
Syntax: char charAt(int position)
The charAt() will obtain a character from specified position.
Eg. String s=”INDIA”
System.out.println(s.charAt(2)); // returns D
c) CompareTo():
Syntax: int compareTo(Object o)
There are two variants of this method. First method
compares this String to another Object and second method
compares two strings lexicographically.
Eg. String str1 = "Strings are immutable";
String str2 = "Strings are immutable";
String str3 = "Integers are not immutable";
int result = str1.compareTo( str2 );
System.out.println(result);
result = str2.compareTo( str3 );
System.out.println(result);
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
a) substring():
Syntax:
String substring(intstartindex)
startindex specifies the index at which the substring will
begin.It will returns a copy of the substring that begins at
startindex and runs to the end of the invoking string
Example :
System.out.println(("Welcome”.substring(3)); //come
System.out.println(("Welcome”.substring(3,5));//co
b) replace():
Syntax: String replace(char oldChar, char newChar)
This method returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
Example:
String Str = new String("Welcome”);
System.out.println(Str.replace('o', 'T')); // WelcTme
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
a) compareTo( ):
Syntax: intcompareTo(Object o)
There are two variants of this method. First method
compares this String to another Object and second method
compares two strings lexicographically.
Eg. String str1 = "Strings are immutable";
String str2 = "Strings are immutable";
String str3 = "Integers are not immutable";
int result = str1.compareTo( str2 );
System.out.println(result);
result = str2.compareTo( str3 );
System.out.println(result);
b) equalsIgnoreCase( ):
public boolean equalsIgnoreCase(String str)
This method compares the two given strings on the basis of
content of the string irrespective of case of the string.
Example:
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
System.out.println(s1.equalsIgnoreCase(s2));//true because
content and case both are same.
System.out.println(s1.equalsIgnoreCase(s3));//true because
case is ignored.
System.out.println(s1.equalsIgnoreCase(s4));//false because
content is not same.
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA
class StringDemo
{
public static void main(String args[])
{
String str1="INDIA";
String str2="India";
String str3="My India";
String str4="India";
System.out.println("The length of string INDIA is "+str1.length());
//Length of string
System.out.println("Comparing String India and India
is"+str2.equals(str4));
System.out.println("Comparing String INDIA and India
is"+str1.equals(str2));
System.out.println("Comparing String INDIA and India with
equalsIgnoreCase is "+str1.equalsIgnoreCase(str2));
String str5="I Love";
System.out.println("Result of concatinating of string is
"+str5.concat(str3));
}
}
DERIVED SYNTACTICAL CONSTRUCTS IN JAVA