Hands on the brain 20151024

Source: Internet
Author: User

Stringpool.java
public class Stringpool {//definition

public static void Main (String args[])//main is common, static, no return value
{

String s0= "Hello";//defines a string

String s1= "Hello";//defines a string

String s2= "He" + "llo";//define a string

System.out.println (S0==S1);//true//output

System.out.println (S0==S2);//true//output

System.out.println (New string ("Hello") ==new string ("Hello"));//false//output

}


}

In Java, a string constant with the same content ("Hello") holds only one copy to conserve memory, so s0,s1,s2 actually refers to the same object. When the compiler compiles a s2 sentence, it strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization work is done automatically by the Java compiler. When you create a string object directly using the New keyword, the value is the same (all "Hello"), but it is still two separate objects.

Hands on the brain 20151024

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.