Java की Object Class
In this page:
@Override
public String toString() { ... }
@Override
public boolean equals(Object obj) { ... }
@Override
public int hashCode() { ... }
What is the Object Class?
Java में हर class implicitly Object extend करती है अगर यह explicitly कुछ और extend नहीं करती, यही कारण है कि हर object में toString(), equals(), और hashCode() जैसे methods अपने आप available हैं भले ही आपने कभी उन्हें खुद नहीं लिखा।
उदाहरण: What is the Object Class?
class Dog {}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println(dog.toString()); // inherited from Object
}
}
Login to try C/C++/Java/PHP code in the editor
The toString() Method
Object से inherited default toString() class name plus एक hash code print करता है, जो debugging के लिए शायद ही कभी उपयोगी है — meaningful field values return करने के लिए इसे override करना आपकी अपनी classes पर आप करने वाले सबसे आम customizations में से एक है।
उदाहरण: The toString() Method
// Define the class Dog
class Dog {
// Declare name and set it to "Rex"
String name = "Rex";
// Override the method inherited from the parent type
@Override
public String toString() {
// Send "Dog: " + name back to the caller
return "Dog: " + name;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(new Dog());
}
}
Login to try C/C++/Java/PHP code in the editor
The equals() Method
Object से default equals() बस memory references compare करता है (== के identical), इसलिए अलग addresses वाली दो logically-equal objects unequal मानी जाएंगी जब तक आप असल field values compare करने के लिए equals() override न करें।
उदाहरण: The equals() Method
// Define the class Point
class Point {
int x;
Point(int x) { this.x = x; }
// Override the method inherited from the parent type
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) return false;
// Send ((Point) obj).x == this.x back to the caller
return ((Point) obj).x == this.x;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p1
Point p1 = new Point(5);
// Create a new Point object and store it in p2
Point p2 = new Point(5);
// Print a line to the console
System.out.println(p1.equals(p2));
}
}
Login to try C/C++/Java/PHP code in the editor
The hashCode() Method
hashCode() एक integer return करता है जो HashMap और HashSet जैसी hash-based collections objects को efficiently bucket करने के लिए उपयोग करती हैं; Java का contract मांगता है कि equals() से equal माने गए दो objects को वही hash code return करना चाहिए, नहीं तो वे collections गलत behave करेंगी।
उदाहरण: The hashCode() Method
// Define the class Point
class Point {
int x;
Point(int x) { this.x = x; }
// Override the method inherited from the parent type
@Override
public int hashCode() {
// Send Integer.hashCode(x) back to the caller
return Integer.hashCode(x);
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(new Point(5).hashCode());
}
}
Login to try C/C++/Java/PHP code in the editor
The getClass() Method
getClass() एक instance के असली type को describe करता runtime Class object return करता है, जो reflection के लिए, concrete class name log करने के लिए, या अकेले instanceof checks काफी precise न होने पर subclasses के बीच distinguish करने के लिए उपयोगी है।
उदाहरण: The getClass() Method
// Define the class Dog
class Dog {}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Dog object and store it in dog
Dog dog = new Dog();
// Print a line to the console
System.out.println(dog.getClass().getName());
}
}
Login to try C/C++/Java/PHP code in the editor
hashCode()के बिनाequals()override करना, जोHashSetऔरHashMapको तोड़ता है।equals()चाहिए होने पर==उपयोग करना।toString()override न करना, इसलिए एक object print करनाPerson@1b6d3586जैसा कुछ दिखाता है।
- Classes blueprints हैं और objects instances हैं, constructors के साथ created और initialized,
thiscurrent object को refer करते हुए। - Access modifiers, non-access modifiers, और getters और setters visibility control करते हैं और data encapsulate करते हैं।
static,final,instanceof, औरObjectclass round out करते हैं कि objects कैसे behave करते हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: