Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does Object mean in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the meaning of Object in Java, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

/ * * knowledge points: Object class Note: 1. Object is the root class of all classes, which means that all classes inherit Object by default but omit it without writing 2. When println () outputs an object, the toString () method of Object is automatically called. The Object toString () method returns: the class name + @ + hexadecimal hash code hashCode address hash code represents the memory address of the object So if you compare the reference equality of two objects = = must hash code equal 4. The Object equals (Object obj) method also refers to the internal implementation of = = 5. How does the equals () method of the String object compare the content? 6. If we want equals () to compare that the contents of two objects are equal, we need to override the equals method 7. When the subclass of Object overrides the toString () method, and the println () output invokes the toString () method 8. If the equals () method is overridden, in order to ensure that the hash code hashcode must be equal, we also need to override the hashCode () method to maintain the consistency of the object. Object can receive any reference data type (class, array, interface) to review knowledge points: 1. If a class defines a constructor with parameters, the java virtual machine does not automatically generate the default constructor when called with a constructor without arguments, but must manually display the default constructor that defines the constructor without parameters. * / public class TestObject {public static void main (String [] args) {/ / instantiate a Student Object p = new Student ("Li Ming"); Student p2 = (Student) p; Student p3 = new Student ("Zhang Tao"); Student p4 = new Student ("Li Ming") System.out.println (p); System.out.println (p = p2); System.out.println (p3); System.out.println (p.equals (p3)); System.out.println (p = = p4); System.out.println (p.equals (p4)) System.out.println (p); System.out.println (p4); Person P5 = new Student ("Li Ming"); System.out.println (p4.equals (p5)); String [] p6 = new String [1]; p6 [0] = "Li Ming" System.out.println (p4.equals (p6));} interface Person {/ / who can talk to public void say ();} class Student extends Object implements Person {private String name; public Student () {} public Student (String name) {this.name = name;} public String getName () {return this.name } public void say () {System.out.println ("students can speak English");} public boolean equals (Object obj) {/ / if both addresses are equal, the two objects must be equal if (this = = obj) {return true } / / determine whether it is a Student object instance if (obj instanceof Student) {/ / convert obj down to Student Student p = (Student) obj / / return true if (this.name.equals (p.getName () {return true;}} return false;} public int hashCode () {return this.name.hashCode () if the content is equal } public String toString () {return this.name;}} above is all the content of the article "what does Object in Java mean?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report