Understanding stacks and stacks in Java
Stacks and heaps are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.
The Java heap is a run-time data area in which the objects of the class allocate space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.
The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle. Stack has a very important particularity, is that there is data in the stack can be shared.
Let's say we define both:
int a = 3;
int b = 3;
The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3. At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B.
It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference variable.
String is a special wrapper class data.
Can be used:
String str = new String ("abc");
String str = "ABC";
Two forms, the first is to create new objects with new (), which is stored in the heap. A new object is created each time the call is made. The second is to create an object reference to the string class in the stack str, and then find whether there is no "ABC" in the stack, if not, put "ABC" into the stack, and make str point to "ABC", if there is already "ABC" directly to the "ABC" Str.
Use the Equals () method when comparing values within a class, and when testing two wrapper classes for reference to the same object, use = =, the following example illustrates the above theory.
String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2); True
You can see that str1 and str2 are pointing to the same object.
String str1 =new string ("abc");
String str2 =new string ("abc");
System.out.println (STR1==STR2); False
The new method is to generate different objects. Each time one is generated.
So create multiple "abc" strings in the second way (string str1 = "abc"), in which only one object exists in memory. This writing is advantageous and saves memory space. At the same time it can improve the speed of the program to some extent, because the JVM will automatically determine whether it is necessary to create new objects based on the actual data in the stack. For the code of string str = new String ("abc"), it is necessary to create new objects in the heap, regardless of whether their string values are equal, to create new objects, thereby aggravating the burden of the program.
On the other hand, it is important to note that when you define a class using a format such as String str = "ABC", you always want to assume, of course, that the object that created the string class is Str. Worry about traps! The object may not have been created! Instead, it might just point to an object that was previously created. Only through the new () method can you guarantee that a new object is created each time.
Because of the immutable nature of the string class, you should consider using the StringBuffer class to improve program efficiency when a string variable needs to change its value frequently.
*************************************************************************************************************** *********************************************
The difference between equals and = = in Java
The data types in Java can be divided into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten, such as String,integer, In these classes, date equals has its own implementation, rather than the comparison class's storage address in heap memory.
For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).
<span style= "Font-family:microsoft yahei;font-size:12px;" >public class TestString {public static void main (string[] args) {String S1 = "Monday"; String s2 = "Monday", if (S1 = = s2) {System.out.println ("S1 = = S2"),} else {System.out.println ("S1! = s2");}} </span>
compile and run the program, output: S1 = = S2 Description: S1 and S2 refer to the same String object-"Monday"!
2. A little more change to the program, there will be more strange discovery:
<span style= "Font-family:microsoft yahei;font-size:12px;" >public class TestString {public static void main (string[] args) {String S1 = "Monday"; String s2 = new String ("Monday"), if (S1 = = s2) {System.out.println ("S1 = = S2");} ELSE{SYSTEM.OUT.PRINTLN ("S1! = s2");} if (s1.equals (S2)) {System.out.println ("S1 equals S2");} Else{system.out.println ("S1 not equals S2");}}} </span>
We will create the S2 with the new operator
Program output:
S1! = S2
S1 equals s2
Description: S1 S2 cited two "Monday" string objects respectively
3. String buffer pool
Originally, the program at run time will create a string buffer pool when using S2 = "Monday" expression is to create a string, the program first in this string buffer pool to find the same value of the object, in the first program, S1 was put into the pool, so when the S2 was created, The program found a S1 with the same value
S2 refers to the object referenced by S1 "Monday"
In the second procedure, the new operator was used, and he understood the telling program: "I want a new one!" Don't be old! "So a new" Monday "Sting object is created in memory. Their values are the same, but the locations are different, one swimming in the pool and one resting on the shore. Alas, it is a waste of resources, obviously the same must be divided into what?
4. Change the program again:
<span style= "Font-family:microsoft yahei;font-size:12px;" >public class TestString {public static void main (string[] args) {String S1 = "Monday"; String s2 = new String ("Monday"), S2 = S2.intern (), if (S1 = = s2) {System.out.println ("S1 = = S2");} ELSE{SYSTEM.OUT.PRINTLN ("S1! = s2");} if (s1.equals (S2)) {System.out.println ("S1 equals S2");} Else{system.out.println ("S1 not equals S2");}}} </span>
This time join: S2 = S2.intern ();
Program output:
S1 = = S2
S1 equals s2
Originally, (Java.lang.String's Intern () method "abc". The return value of the Intern () method is also the string "abc", which looks as if this method is of little use. But in fact, it did a little trick: Check the string pool for the existence of a string "abc", if present, return the string in the pool, if it does not exist, the method will add "ABC" to the string pool, and then return its reference.
)
*************************************************************************************************************** *********************************************
View Source Release Summary:
<span style= "Font-family:microsoft yahei;font-size:12px;" >public Boolean equals (Object anobject) { if (this = = AnObject) { return true; } if (anobject instanceof string) { string anotherstring = (string) anobject; int n = value.length; if (n = = anotherString.value.length) { char v1[] = value; Char v2[] = Anotherstring.value; int i = 0; while (n--! = 0) { if (v1[i]! = V2[i]) return false; i++; } return true; } } return false; } </span>
The above paragraph is the equals source of string, through careful interpretation we can find the mystery.
The Equals method of string overrides preserves object comparison of two object content addresses for equality. On this basis, a comparison of string values is added for equality. What does that mean? If the object in the equals bracket is not of type string, compare the memory address of the original object with the same. If Instanceofstring then turns this object into a string and then changes its value to a char array, that is, the following value, if their char array is equal in length, then it is the same for the array's char. If all is the same, equals is true, otherwise false.
Comparison from Java stacks to equals and = =