Check java stack issues by string, including = and equal ().

Source: Internet
Author: User

Check java stack issues by string, including = and equal ().
First, check the Code:
1

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 = "ABC ";
  4. String b0 = "ABC ";
  5. If(A0 = b0 ){
  6. System. Out. Print ("= ");
  7. }Else{
  8. System. Out. Print ("! = ");
  9. }
  10. }
  11. }
public class TestString {public static void main(String[] args) {String a0 = "abc";String b0 = "abc";if (a0 == b0) {System.out.print("==");} else {System.out.print("!=");}}}

The execution result is:
=

2

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 = string. valueof ("ABC ");
  4. String b0 = string. valueof ("ABC ");
  5. If(A0 = b0 ){
  6. System. Out. Print ("= ");
  7. }Else{
  8. System. Out. Print ("! = ");
  9. }
  10. }
  11. }
public class TestString {public static void main(String[] args) {String a0 = String.valueOf("abc");String b0 = String.valueOf("abc");if (a0 == b0) {System.out.print("==");} else {System.out.print("!=");}}}

The execution result is:
=

3

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 = "ABC" + "def ";
  4. String b0 = "abcdef ";
  5. If(A0 = b0 ){
  6. System. Out. Print ("= ");
  7. }Else{
  8. System. Out. Print ("! = ");
  9. }
  10. }
  11. }
public class TestString {public static void main(String[] args) {String a0 = "abc"+"def";String b0 = "abcdef";if(a0==b0){System.out.print("==");}else{System.out.print("!=");}}}

The execution result is:
=

4

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 = "ABC ";
  4. String b0 = "def ";
  5. String C0 = "abcdef ";
  6. String D0 = A0 + B0;
  7. If(C0 = D0 ){
  8. System. Out. Print ("= ");
  9. }Else{
  10. System. Out. Print ("! = ");
  11. }
  12. }
  13. }
public class TestString {public static void main(String[] args) {                  String a0 = "abc";String b0 = "def";String c0 = "abcdef";String d0 = a0 + b0;if (c0 == d0) {System.out.print("==");} else {System.out.print("!=");}}}

The execution result is:
! =

5

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 =NewString ("ABC ");
  4. String b0 =NewString ("ABC ");
  5. If(A0 = b0 ){
  6. System. Out. Print ("= ");
  7. }Else{
  8. System. Out. Print ("! = ");
  9. }
  10. }
  11. }
public class TestString {public static void main(String[] args) {String a0 = new String("abc");String b0 = new String("abc");if (a0 == b0) {System.out.print("==");} else {System.out.print("!=");}}}

The execution result is:
! =

6

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. Public ClassTeststring {
  2. Public Static VoidMain (string [] ARGs ){
  3. String a0 = string. valueof ("ABC") + String. valueof ("def ");
  4. String b0 = string. valueof ("abcdef ");
  5. If(A0 = b0 ){
  6. System. Out. Print ("= ");
  7. }Else{
  8. System. Out. Print ("! = ");
  9. }
  10. }
  11. }
public class TestString {public static void main(String[] args) {String a0 = String.valueOf("abc")+String.valueOf("def");String b0 = String.valueOf("abcdef");if (a0 == b0) {System.out.print("==");} else {System.out.print("!=");}}}

The execution result is:
! =

We all know that Java's memory allocation policy: stores basic data types (or built-in data types) and reference types (or object handles) in the stack, and stores object data in the heap.
String is special. According to think in Java: "using the compiler and special overwrite or overload operators + and + =, you can convert the quotation mark string into a string ". It can be seen that the quotation mark string itself is not a string, but is converted to a string through the heavy load of the operator. Note: No = is displayed.
Therefore, we must ask where the quotation mark string is converted to a string after being reloaded?
No specific analysis is found on the internet, and no accurate description is provided on the books.
Conjecture based on execution results:
I have learned how to load a class and may know that there is a constant zone.
For the moment, we assume that this part of data is stored in this space for a reasonable explanation.

Java memory allocation stack stores two types of data:
1. Basic data type: Do you have any questions? It stores the specific data or a memory address.
Suppose 1: The stack stores specific data. Of course, all the operations that appear in the basic type can be explained.
Suppose 2: the data is stored in the constant area mentioned above, and the stack stores the address of the data corresponding to this constant area. All basic data type operations can also be explained.
In fact, Java designers pay great attention to memory utilization. We may see many articles mentioned this concept. When int n = 1;, Java will search in memory, check whether 1 exists. If yes, 1 is not re-allocated. If NO, 1 is created. If int M = 1; java will not create 1 space. It seems that there is a place to store data in the memory. In this case, hypothesis 2 is fully true.
Now the following conclusions are shown in Hypothesis 2.
2. Reference Type: the stack stores the object reference, which is the address allocated to the center for the object created through new.
Analyze the string.
What type does it belong? Basic data type and reference type.
When created as string STR = "ABC";, hypothesis 2 is reasonable as the basic type (see section 1st code ).
When created as string STR = new string ("ABC");, it is undoubtedly processed as a reference type (see section 5th code ).

For the moment, we will use + as a special Java processing mechanism. When it processes the quotation mark string, it will not use the new method to create a string (like "ABC" + "def "), for data processing in the stack, the new method is used to create a string (like a + B ). (See Code 3rd, 4, and 6 ).

When it is created or executed as string STR = string. valueof ("ABC"); +, it can be fully interpreted.
In fact, String. valueof () can be seen as an encapsulation of "ABC", but it is not new (see Code 2nd and 6 ).

In fact, based on the above points of view, we can come to the following conclusion:
The stack stores handles, including basic data types. Another area (Constant Area) exists in the memory, which stores basic data type data and quotation mark string data.
Objects are stored in the heap, and must be new or reload implicitly new using the + operator.

I had a hard time thinking about it, which is contrary to some theories. Many things cannot be explained, so I am confused.
Welcome to comments. Thank you.

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.