View stack allocations with eclipse

Source: Internet
Author: User

This article stems from a colleague who asked me,

String str1 = "abc";

String str2 = "abc";

String str3 = new String ("abc");

STR1 = = str2 True, does it mean that str1 and str2 are allocated on top of the stack? They are not being new space.

Then LZ himself yy a bit, think of a way to use eclipse to see the stack allocation of variables, authoritative to be verified, if there is inappropriate, please enlighten!

The test procedure is as follows:

public class Stackstringtest {public static void main (string[] args) {String str1 = "abc"; String str2 = "abc"; System.out.println (str1 = = str2); String str3 = new String ("abc"); String STR4 = new String ("abc"); System.out.println (STR3 = = STR4); int a = 1;integer B = new Integer (1); System.out.println (a); System.out.println (b);}}


Put a breakpoint on each sentence, debug one-step operation, you can get the following results:


This leads to the conclusion that:

1. Whether string a = "a", or string a = new string ("a"), they are all assigned to the heap above (both have IDs, and an int type of a does not have an ID)

2. str1 = = Str2 is true because the system detects the existence of this address space (id=19) where value is "ABC" when Executing str2 = "abc", and in order to save space, it will no longer re-add a new area for str2. Instead, it points directly to the str1 heap interval, so the IDs of str1 and str2 are equal.




View stack allocations with eclipse

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.