Comparison of data in Java (comparison = and equals)

Source: Internet
Author: User


This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.

 

 

Are Variables in Java different from objects?

 

Introduction: variables and objects

A variable is the most basic unit of storage in Java. You can use a value assignment expression to assign values to a variable. For example:
Int I = 10;

This expression is used to assign a literal value (literal) 10 to an int-type variable named I. This is an example of assigning values to a variable of the basic data type. It expresses a very simple information, that is, the value of variable I is 10.
So what changes does the meaning of this value expression change when it is extended to a variable of the reference type? Let's look at another value assignment expression:
String STR = NULL;

This expression is used to assign a null memory address (null) to a string-type variable named Str. Simply put, the value of STR is null. For a variable of the reference type, the value assignment operation only saves the memory address of the object to the variable. That is to say, the variable value of the reference type is the memory address of the object rather than the content of the object. For example:
String str1 = "ABC"; <br/> string str2 = new string ("ABC ");

There is no difference between the two assignment operations. The biggest difference is that the method for generating objects is different (this is irrelevant to the assignment operation ). For a variable, its value is still the memory address of the object.

Objects are also a type of storage unit relative to variables. An object has its own attributes and Methods. Its content is represented by the class used to instantiate the object. For example:
New java. SQL. Time (0l );

To use an object, you must specify the memory address of the object to a variable of the reference type (that is, the value assignment operation of the variable ). The variable type can be the same as the object type, the parent class of the object type, or the interface implemented by the object type. The last two types are typical polymorphism applications. For example:
Java. util. Date = new java. SQL. Time (0l );

Of course, we can only use variables to call object methods or set object attributes. The function is nothing more than obtaining or modifying object content. For example:
Java. util. date = new Java. SQL. time (0l); <br/> date. settime (3600000l); <br/> system. out. println (date. tostring ());

Note that there is only one connotation of a variable, that is, its value. When we call an object method through a variable, we can only change the content of the object. It is necessary to distinguish variables from objects. variables are always related to their values when we talk about them, while objects are more about how to get or modify them. Remember:

 

The value of a variable can only be changed through a value expression. The content of an object can only be changed through its own methods or attributes.

 

 

Comparison of variable values

When we discuss whether variables are equal, we usually use the "=" relational operator. For example:
Int I = 10; <br/> Int J = 20; <br/> if (I = J) {<br/> system. out. println ("the values of the two variables are equal"); <br/>}

In the above example, the comparison between basic data types is essentially a comparison of values between variables. Compare two referenced variables, for example:
String str1 = "ABC"; <br/> string str2 = new string ("ABC"); <br/> If (str1 = str2) {<br/> // The Judgment fails because the memory addresses of the objects referred to by the two variables are different. <Br/> system. Out. println ("the values of the two variables are equal"); <br/>}

Essentially, the comparison between variables of the reference type is also a comparison of values, that is, the comparison of memory addresses. In the preceding example, "the values of two variables are equal" is not printed because the two variables point to objects with different memory addresses.

 

Object Content Comparison

For reference variables, if we do not want to compare the memory addresses, we want to make a deeper comparison (such as object content. How to implement it? The Java object class provides the equals method, which compares the content between objects. Because the object class is the parent class of all Java classes, we only need to rewrite the equals method in our own classes to implement content comparison between these classes. For example:
String str1 = "ABC"; <br/> string str2 = new string ("ABC"); <br/> If (str1.equals (str2 )) {<br/> // The determination is true, because the content of both objects is "ABC ". <Br/> system. Out. println ("two objects have the same content"); <br/>}

For details about how to rewrite the equals method and related hashcode methods, refer to Article 7th translated by Pan aimin in "valid Java Chinese version: when you rewrite equals, follow the general conventions and set forth in Article 8th: hashcode is always rewritten when you rewrite equals.

 

Old saying: = differences from equals

= And equals are confusing for beginners of Java. Of course, it is easy to distinguish them, just remember:

 

= Only for the value of the variable; equals only for the content of the object.

 

Remember the following sentence while remembering the previous sentence:

 

The reference type variable value refers to the memory address of the object.

 

 

Appendix 1: in Java function calls, there is only one parameter transfer method: Value Transfer

There are many examples on the Internet to prove the conclusion of value transfer. Here is an example:
Public class test {</P> <p> Public static void main (string [] ARGs) {<br/> JAVA. util. date = new Java. util. date (0); </P> <p> system. out. println (date); <br/> change (date); <br/> system. out. println (date); <br/>}</P> <p> Public static void change (Java. util. date) {<br/> // The parameter (variable) value is changed here <br/> date = new Java. util. date (3600000l); <br/> system. out. println (date); <br/>}< br/>}

The output result of the above example is:

Thu Jan 01 08:00:00 CST 1970
Thu Jan 01 09:00:00 CST 1970
Thu Jan 01 08:00:00 CST 1970

Let's slightly change the content:
Public class test {</P> <p> Public static void main (string [] ARGs) {<br/> JAVA. util. date = new Java. util. date (0l); </P> <p> system. out. println (date); <br/> change (date); <br/> system. out. println (date); <br/>}</P> <p> Public static void change (Java. util. date) {<br/> // The object content is changed here <br/> date. settime (3600000l); <br/> system. out. println (date); <br/>}< br/>}

The output result of the above example is:

Thu Jan 01 08:00:00 CST 1970
Thu Jan 01 09:00:00 CST 1970
Thu Jan 01 09:00:00 CST 1970

What can we prove through these two examples?
When a function is called, JVM generates the second referenced variable and copies the value of the original referenced variable (the memory address of the object) to the second referenced variable. Note,The essence of value transfer is copying variable values rather than copying object content.
In the first example, the function internally changes the value of the second referenced variable, and the value of the original referenced variable does not change. In the second example, the function changes the content of the object referred to by the referenced variable. Because the original referenced variable has the same value as the second referenced variable (pointing to the same object ), this leads to the fact that the object content has changed after the function call.

 

Appendix 2: the keyword final only acts on the value of the variable and does not act on the content of the object.

The keyword final is used to assign values to a variable only once. For example:
Final int I = 10; <br/> I = 20; // an error is reported during compilation.

However, the limit that can only be assigned once only applies to the value of the variable, rather than the content of the object. For example:
Final java. util. Date = new java. util. Date (0l); <br/> date. settime (3600000l); // the code can be compiled and run properly.

This raises another question: how to define constants.
To define a constant, use the keyword final to restrict the variable. However, this only limits the value of a variable. If the variable type is a reference type, you must ensure that the object is a value object (value objects ). In other words, the class of the value object must be an immutable class ).
The string class is an unchangeable class. We can use the string class to directly define constants. The Java. util. Date class can change its content through its own methods, so constants cannot be defined using this type. For example:
Final string China = "China ";

For more information about how to implement immutable classes, see article 63rd in practical Java: carefully defining and implementing immutable classes.

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.