About the equal sign operator "=" in java"

Source: Internet
Author: User

We all know that the equal sign operator "=" is a magic symbol in the encoding language, because it represents a value assignment operation rather than simply "equal ". We also know that "=" indicates the left and right values when assigning values. Of course, the left and right values are not detailed here. This is not the focus of this article. Next, let's briefly describe the "=" Operator in java:

First, the data types in java are divided into two categories: Basic Types and reference types. basic types:Int,Byte,Char,Short,Long,Float,Double,Boolean. Reference Type:String,Array,Object.

First, compare the following two sections of code: 1.
1 int a = 3;2 int b = a;3 b++;4 System.out.println(a);

2.

1 String a = "Hello";2 String b = a;3 b = b + "World";4 System.out.println(a);
The final result will be the above A = 3; A = "Hello ";On the surface ABut the mechanism is actually different. "=" For the basic type is only a value assignment operation, that is AThis variable is added to the stack and ASet the content of the memory area 3. The reference type is "=", but it is a reference operation. Specifically, it is the transfer of the address. String a = "Hello ";In fact, a memory is opened in the heap, And the content in the memory is a string "Hello"Object, and variable ABut we declare it in the stack. The "=" operator is AReference "Hello"This object, AThe actual value should be "Hello"String object address! While String B =;At this time AThe address is assigned B(Reference is an address assignment ), A, BThe value is the same address. A, BPoint to the same object, B = B + "world ";Actually changed BThe referenced object is not BThe value of the referenced object. BRe-reference another object "Helloworld", AOr reference "Hello"This string object. Of course System. out. println ()The value will not change (this is what I understand PrintlnThe method transfer reference type is an overload of the basic type. If you are interested, refer to javadoc. This is to be verified ). To expand, let's talk about the Java memory reclaim mechanism. The following code is used as an example:
1 String a = "Hello";2 a = a + "World";3 System.out.println(a);
Take a look A = a + "World ";We already know this sentence. AIs a reference to a memory object in the heap, A + "World ";The first response is to change "HelloWorld", Otherwise, "Hello"This StringObjects of the type are immutable after initialization, A = a + "World ";It is actually opening up another piece of memory in the heap area. The content in the memory is "HelloWorld", Originally stored "Hello"The memory area of will be recycled, ARereference "HelloWorld"This memory area, AValue ( A. Let's look at another example:
 1 public class Text{ 2     private String word; 3     public void setWord(String Word){ 4         this.word = Word; 5     } 6     public void printWord(){ 7         System.out.println(this.word); 8     } 9 }10 public class Main{11     public static void main(String[] args){12         Text t1 = new Text();13         t1.setWord("Hello");14         Text t2 = t1;15         t2.setWord("HelloWorld");16         t1.printWord();17     }18 }
T1.printWord ()The output is "HelloWorld ";Description in Text t2 = t1, T2Referenced T1The referenced object is New Text ()Initialized object, T2.setWord ("HelloWorld ");The member of this object. WordChanged, that is T2.wordThe address of the referenced object has changed, T1, T2Referenced New Text ();This object is not changed because T1Also reference the original object, T1.wordIt will also change. The last sentence is a summary: variables can reference objects, but variables are not objects!

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.