"KEEP LEARNING, DON'T STOP."
"KEEP LEARNING, DON'T STOP."
Object is the super class for the all classes in java. By default every class in java are object type. Whether it is predefine or user define or arrays. In object class only method are there.
Total 11 methods are there in object class.
We use toString() to convert the any object into String representation or String object. By default JVM call the toString() method.
public String toString() //it return String object only
class A { private String name; private int id; public A(String name, int id) { this.name = name; this.id = id; } public String toString() { return "Name = "+name+ "\nId = "+id; } } class Test4 { public static void main(String[] arg) { A a = new A("Manzar", 123); System.out.println(a); } }
Name = Campuslife Id = 123
Creating the new object with exactly same object. A new object created with same data, it's called object cloning.
The clone() method is defined in the Object class. Syntax of the clone() method is as follows:
Employee emp2 = (Employee)emp.clone();
class Employee implements Cloneable { int id; String name; public Employee(int id, String name) { this.id = id; this.name = name; } public Object clone()throws CloneNotSupportedException { return super.clone(); } } public class Test6 { public static void main(String args[])throws CloneNotSupportedException { Employee emp = new Employee(123,"Akhtar"); Employee emp2 = (Employee)emp.clone(); System.out.println(emp.id+" "+emp.name); System.out.println(emp2.id+" "+emp2.name); } }
123 Campuslife 123 Campuslife
getClass() is use to get the name of the Class which loaded in the JVM memory.
Hello h = Hello(); Class c = h.getClass();
class A { public void show() { } } class Test { public static void main(String args[]) { Hello h = new Hello(); Class c = h.getClass(); System.out.println(c); } }
class A
getName() is use to get the name of the Class without class keyword which loaded in the JVM memory.
Hello h = Hello(); String name = c.getName();
class A { public void show() { } } class Test { public static void main(String args[]) { Hello h = new Hello(); //This method will give output with class keyword Class c = h.getClass(); //This method will give output without class keyword String name = c.getName(); System.out.println(c+"\n"+name); } }
class A //Class name with class keyword A //Class name without class keyword
We all know that an object is created in memory using new operator. Constructor is used to initialize the properties of that object. When an object is no more required, it must be removed from the memory so that that memory can be reused for other objects.
finalize() method is a special method that performs finalization, generally some form of cleanup. Finalize executed when object going to remove from JVM memory.
Methods are representing there life cycle of an object.
System.gc() is method in System class. By default it is a static method.
class Car { int regno; public Car(int regno) { this.regno = regno; System.out.println("Car Object is created.."); } public void run() { System.out.println("Car Running.."); } public void finalize() { System.out.println("Finalize called..."); } public static void main(String args[]) { Car car = new Car(123456); car.run(); car=null; System.gc(); } }
The equals() method is always used to compare two strings. It compare the string based on hash code generated by hashCode() method.
equals() method always depends on hashCode() method. equals() method also check class type or data also. equals() method return type is boolean. It always give result in the form of true or false only.
hashCode() method generate unique number based on the data. The hashCode() method is used to get the hash code of a string.
hashCode() is one Algorithm which is given by Sun MicroSystems.
If two string is different then there hashCode will be different.
hashCode() method return type is int.
class Raza { public static void main(String args[]) { String str = "Hello"; String str1 = "Hello"; //caomparing the two strings boolean b = str.equals(str1); //getting the hash code of two string int i = str.hashCode(); int f = str1.hashCode(); System.out.println(b); System.out.println(i); System.out.println(f); } }
true 69609650 69609650