Object_Class_1.1
Object_Class_1.1
Package:
import java.lang.Object;
Object class methods Description
Returns the Class class
Class getClass();
object of this object.
Returns a hash value that is
int hashCode(); used to search objects in a
collection
boolean equals( ); Gives generic way to
compare objects
Can be used to convert the
String toString()
object to String
creates and returns the
protected Object clone() exact copy (clone) of this
object.
Object class methods Description
void notify() Used in synchronizing threads
Syntax:
public int hashCode()
1 //Predict the Outpu
2 import java.lang.Object;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Integer i = new Integer(8421);
8 int Value = i.hashCode();
9 System.out.println(" " +Value);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2 import java.io.*;
3 class Citizen {
4 public String name;
5 public long aadhar_no;
6 Citizen(String name, long aadhar_no)
7 {
8 this.name = name;
9 this.aadhar_no = aadhar_no;
10 }
11 }
12 public class Main
13 {
14 public static void main (String[] args)
15 {
16 Citizen c1 = new Citizen("Deepak", 655012340326L);
17 Citizen c2 = new Citizen("Abdul", 798237802268L);
18 System.out.println(c1.hashCode());
19 System.out.println(c2.hashCode());
20 }
21 }
22
1 // Predict the output
2 import java.io.*;
3 class Citizen
4 {
5 public String name;
6 public long aadhar_no;
7 Citizen(String name, long aadhar_no)
8 {
9 this.name = name;
10 this.aadhar_no = aadhar_no;
11 }
12 public int hashCode()
13 {
14 return 101;
15 }
16 }
17
18
19
20
21
22
1 public class Main
2 {
3 public static void main (String[] args)
4 {
5 Citizen c1 = new Citizen("Deepak", 655012340326L);
6 Citizen c2 = new Citizen("Abdul", 798237802268L);
7 System.out.println(c1.hashCode());
8 System.out.println(c2.hashCode());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Outpu
2 import java.lang.Object;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Integer i = new Integer("String");
8 int Value = i.hashCode();
9 System.out.println(" " +Value);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Scanner sc= new Scanner(System.in);
8 Integer i = sc.nextInt();
9 sc.close();
10 int Value = i.hashCode();
11 System.out.println(" " + Value);
12 }
13 }
14
15
16
17
18
19
20
21
22
Boolean equals()
The Boolean.equals(Object obj) returns true if and only if the
argument is not null and is a Boolean object that represents the
same boolean value as this object.
Syntax:
BooleanObject.equals(Object obj)
1 //Predict the Outpu
2 import java.lang.Object;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Boolean b1, b2;
8 boolean res;
9 b1 = new Boolean(true);
10 b2 = new Boolean(false);
11 res = b1.equals(b2);
12 System.out.println( res );
13 }
14 }
15
16
17
18
19
20
21
22
1 //Predict the Outpu
2 import java.lang.Object;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Boolean b1, b2;
8 boolean res;
9 b1 = new Boolean(true);
10 b2 = new Boolean(true);
11 res = b1.equals(b2);
12 System.out.println(res);
13 }
14 }
15
16
17
18
19
20
21
22
String toString()
The String toString() method returns the string representation of the
object.
Syntax:
public String toString()
1 //Predict the Output
2 import java.lang.Object;
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 Integer s = new Integer(678);
8 System.out.println(s.toString());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.lang.Object;
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 String s = new String("Super");
8 System.out.println(s.toString());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.lang.Object;
3 public class emp
4 {
5 int id;
6 String name;
7 String city;
8 emp(int id, String name, String city)
9 {
10 this.id=id;
11 this.name=name;
12 this.city=city;
13 }
14 public static void main(String args[])
15 {
16 emp s1=new emp(1325,"Mathu","India");
17 emp s2=new emp(1326,"Dinesh","London");
18 System.out.println(s1);
19 System.out.println(s2);
20 }
21 }
22
1 //Predict the Output
2 import java.lang.Object;
3 public class emp
4 {
5 int id;
6 String name;
7 String city;
8 emp(int id, String name, String city){
9 this.id=id;
10 this.name=name;
11 this.city=city;
12 }
13 public String toString() {
14 return id+" "+name+" "+city;
15 }
16 public static void main(String args[]){
17 emp s1=new emp(1325,"Mathu","Patna");
18 emp s2=new emp(1326,"Dinesh","Delhi");
19 System.out.println(s1);
20 System.out.println(s2);
21 }
22 }
THANK YOU