Object-oriented (Object Class-equals (), object-equals

Source: Internet
Author: User

Object-oriented (Object Class-equals (), object-equals

   

import com.sun.xml.internal.ws.server.ServerRtException;

   

/**
* An Object is a direct or indirect parent class of all objects.
* All objects are defined in this class.
* Created by rabbit on 2014-07-29. Blog garden. Liu pengcheng
*
*/
Class Demo111
{

   

}

* Created by rabbit on 2014-07-29. Blog garden. Liu pengcheng
   

Public class ObjectDemo {
Public static void main (String [] args)
{
String s1 = new String ("123 ");
String s2 = new String ("123 ");
Demo111 d1 = new Demo111 ();
Demo111 d2 = new Demo111 ();
D1 = d2;

System. out. println (s1.equals (s2); // true because they all have the same 123 Value
System. out. println (s1 = s2); // false because their memory addresses are different. Note:
// The comparison method has been rewritten.
System. out. println (d1.equals (d2 ));
System. out. println (d1 = d2); // both results are true because they call
// The methods in the Object, equals and = are
} // The same is true for memory addresses.
}


The object class in C # has an in-depth understanding! It is best to give the instance! And its conversion!

The object class is the base class of all objects.

In fact, MSDN has a lot of explanations about it. Let me show you a link.
Link: msdn.microsoft.com/zh-cn/library/system.object (VS.80). aspx

I also found some explanations on the Internet and thought it was a good explanation. I hope it will be useful to you.

All classes in C # are directly or indirectly inherited from the System. Object Class, which enables the class in C # to be inherited by a single root. If the inheritance class is not explicitly specified, the compiler determines that the class inherits from the System. Object Class by default. The System. Object class can also be expressed by the lower-case object keyword. The two are exactly the same. Naturally, all classes in C # inherit the public interfaces of the System. Object class, and it is very important for us to understand and master the behavior of classes in C. The following is a System. Object class that is expressed in the form of an interface only:

Namespace System
{
Public class Object
{
Public static bool Equals (object objA, object objB ){}
Public static bool ReferenceEquals (object objA, object objB ){}

Public Object (){}

Public virtual bool Equals (object obj ){}
Public virtual int GetHashCode (){}
Public Type GetType (){}
Public virtual string ToString (){}

Protected virtual void Finalize (){}
Protected object MemberwiseClone (){}
}

Let's first look at the two static methods of the object: Equals (object objA, object objB), ReferenceEquals (object objA, object objB), and an instance method Equals (object obj ). Before describing these two methods, we must first understand the two important equality concepts of object-oriented programming: equal values and equal references. Equal values mean that their data members are equal by memory bit. When the references are equal, they point to the same memory address, or their object handles are equal. If the reference value is equal, the reference value must be equal. For the value type relationship equal sign "=", determine whether the two values are equal (the structure type and enumeration type have no defined relationship equal sign "=", we must define it ourselves ). For the reference type relationship equal sign "=", determine whether the two references are equal. The Value Type in C # usually does not reference equal representation. It is used to indirectly judge whether the addresses of the two are equal only by using the "&" in the unmanaged programming.

The static method Equals (object objA, object objB) first checks whether both objects objA and objB are null. If yes, true is returned; otherwise, objA is performed. equals (objB) calls and returns its value. The problem comes down to the instance method Equals (object obj ). The default implementation of this method is {return this = obj;}, that is, to determine whether two objects reference equal ...... the remaining full text>

Why is super unnecessary for a subclass in Java to call methods in the Object class? For example, the supplementary code is printed as False. How can this problem be solved?

Hello, the question you asked involves some attributes of the Object class in java. In Java, an Object is the first top-level parent class as the superclass of all objects. Other new classes, such as Animal, are inherited by default, so they inherit some methods defined by the Object, such as toString );
In your code, although oo1 and oo2 have the same parameter value, it is false, Because java is an object-oriented language. Therefore, each created object in the stack is allocated with a memory. Because oo1 and oo2 are two objects, they are not "equal" despite the same parameters ".
Hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.