String constant pool

Source: Internet
Author: User


Interview question: how many strings have been created?

String S1 = new string ("hello"); string S2 = new string ("hello");/*** S1 creates two objects. The new string is in the heap, then Reference creating the "hello" area in the constant pool. * S2 creates only one object, that is, the new string () of the heap. Because the constant pool already has Hello, it directly references */system. out. println (S1 = S2); // false; system. out. println (s1.equals (S2); // true system. out. println ("*************************"); string S3 = new string ("hello"); string S4 = "hello"; system. out. println (S3 = S4); // falses3 references the heap, and directly references the hello that already exists in the constant pool in the reference "hello", S4 created in the constant pool, however, their address values are different. System. out. println (s3.equals (S4); // truesystem. out. println ("************************"); string S5 = "Chuanzhi "; string S6 = "Chuanzhi"; system. out. println (S5 = s6); // true // when S6 is assigned a value, it is directly referenced if "intelligence" is found in the constant pool. So S5 And S6 point to the same address value. System. out. println (s5.equals (s6); // trues5.substring (0, 1); // The truncated character. Instead of modifying the S5 value, it also opens up a storage space in the constant pool. Currently, it is not referenced by a variable. System. out. println (s5.equals (s6); // true system. out. println ("*************************");

String constant pool

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.