0% found this document useful (0 votes)
7 views24 pages

Object_Class_1.1

The document covers various Java programming concepts including the Object class, its methods, and their functionalities. It provides examples and explanations for methods like getClass(), hashCode(), equals(), and toString(), along with code snippets to predict outputs. Additionally, it includes topics on strings, arrays, regex, and date/time handling.

Uploaded by

ishow2002speed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Object_Class_1.1

The document covers various Java programming concepts including the Object class, its methods, and their functionalities. It provides examples and explanations for methods like getClass(), hashCode(), equals(), and toString(), along with code snippets to predict outputs. Additionally, it includes topics on strings, arrays, regex, and date/time handling.

Uploaded by

ishow2002speed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Pattern Programs + CAT1 Syllabus

Numerical Looping question


MCQs
Strings 1.1
Strings 1.2
Strings 1.3
String Inbuilt methods
Advanced String Problems
Regex
Date and Time and Object Class
Arrays 1.1
Object class
• Object class is the superclass of all Java classes.

• All Java classes inherited from this class.

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

void notifyall() Used in synchronizing threads

void wait(); Used in synchronizing threads


protected Object clone()
Return a new object that are
throws
exactly the same as the
CloneNotSupportedException
current object
;
This method is called just
protected void finalize()
before an object is garbage
throws Throwable;
collected
Class getClass( )
Syntax:
public final Class getClass( )

• getClass() is the method of Object class.

• This method returns the runtime class of this object.


1 //Predict the Output
2 import java.lang.Object;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Object obj = new Integer(567);
8 Class a = obj.getClass();
9 System.out.println(a.getName());
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 Main
4 {
5 public static void main(String[] args)
6 {
7 Object obj = new Double(5.68349023);
8 Class a = obj.getClass();
9 System.out.println(a.getName());
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
int hashCode( )
• The hashCode() method is a Java Integer class method.

• It returns the hash code for the given inputs.

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.

This method does not accept any parameters

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

You might also like