Object Class in Java
Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Object class provides several methods such as toString(),equals(), hashCode(), and many others. Hence, the Object class acts as a root of the inheritance hierarchy in any Java Program.
Example: Here, we will use the toString() and hashCode() methods to provide a custom string representation for a class.
// Java Code to demonstrate Object classclass Person {String n; //name// Constructorpublic Person(String n) {this.n = n;}// Override toString() for a// custom string representation@Overridepublic String toString() {return "Person{name:'" + n + "'}";}public static void main(String[] args) {Person p = new Person("Geek");// Custom string representationSystem.out.println(p.toString());//geek// Default hash code valueSystem.out.println(p.hashCode());//321001045}}// Java Code to demonstrate Object class class Person { String n; //name // Constructor public Person(String n) { this.n = n; } // Override toString() for a // custom string representation @Override public String toString() { return "Person{name:'" + n + "'}"; } public static void main(String[] args) { Person p = new Person("Geek"); // Custom string representation System.out.println(p.toString());//geek // Default hash code value System.out.println(p.hashCode());//321001045 } }// Java Code to demonstrate Object class class Person { String n; //name // Constructor public Person(String n) { this.n = n; } // Override toString() for a // custom string representation @Override public String toString() { return "Person{name:'" + n + "'}"; } public static void main(String[] args) { Person p = new Person("Geek"); // Custom string representation System.out.println(p.toString());//geek // Default hash code value System.out.println(p.hashCode());//321001045 } }
Enter fullscreen mode Exit fullscreen mode
Output
Person{name:’Geek’}
321001045
package afterfeb13;public class Person {String n; // name// Constructorpublic Person(String n) {this.n = n;}public static void main(String[] args) {Person p = new Person("Geek");System.out.println(p);// afterfeb13.Person@6e2c634b//cashcode}}package afterfeb13; public class Person { String n; // name // Constructor public Person(String n) { this.n = n; } public static void main(String[] args) { Person p = new Person("Geek"); System.out.println(p);// afterfeb13.Person@6e2c634b//cashcode } }package afterfeb13; public class Person { String n; // name // Constructor public Person(String n) { this.n = n; } public static void main(String[] args) { Person p = new Person("Geek"); System.out.println(p);// afterfeb13.Person@6e2c634b//cashcode } }
Enter fullscreen mode Exit fullscreen mode
Output
afterfeb13.Person@6e2c634b
Object Class Methods(TBD)
1. toString() Method
The toString() provides a String representation of an object and is used to convert an object to a String. The default toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object.
Note: Whenever we try to print any Object reference, then internally toString() method is called.
Example:
`
public class Student {
public String toString() {return “Student object”;}}public String toString() { return “Student object”; } }public String toString() { return “Student object”; } }
Enter fullscreen mode Exit fullscreen mode
`
Explanation: The toString() method is overridden to return a custom string representation of the Student object.
**
- hashCode() Method**
For every object, JVM generates a unique number which is a hashcode. It returns distinct integers for distinct objects. A common misconception about this method is that the hashCode() method returns the address of the object, which is not correct. It converts the internal address of the object to an integer by using an algorithm. The hashCode() method is native because in Java it is impossible to find the address of an object, so it uses native languages like C/C++ to find the address of the object.
原文链接:Object Class in Java
暂无评论内容