Java Object-Oriented foundation string class

Source: Internet
Author: User

Two ways to instantiate a String class

A. Direct Assignment

public class Stringlei{public static void Main (string args[]) {string name= "Zhang San"; System.out.println ("Name:" +name);}}

B. Through the keyword new

public class Stringlei{public static void Main (string args[]) {string Name=new string ("Zhang San"); System.out.println ("Name:" +name);}}

content Comparison of string

1. Use "= =" to compare

public class Stringlei{public static void Main (string args[]) {   string  str1= "Hello";   String Str2=new string ("Hello");   String str3=str2;   System.out.println ("Str1==str2?-->" + (STR1==STR2))//false   System.out.println ("Str1==str3?-->" + (str1= =STR3)//false   System.out.println ("Str2==str3?-->" + (STR2==STR3));//true}}

It can be said that "= =" Determines whether the address space is equal to determine the address, if you want to determine the content, you must use the Equals () method provided in string to complete:

public class Stringlei{public static void Main (string args[]) {   string  str1= "Hello";   String Str2=new string ("Hello");   String str3=str2;   System.out.println ("Str1==str2?-->" + (Str1.equals (str2));//true   System.out.println ("Str1==str3?-->" + (Str1.equals (STR3))); /true   System.out.println ("Str2==str3?-->" + (Str2.equals (STR3)));//true}}
two ways to instantiate the difference

The constructor method can be completed using the direct assignment and the new call in string. Compare two methods.

A string is an anonymous object of string

public class Stringlei{public static void Main (string args[]) {   string  str1= "Hello";//actually the point of a heap memory space is given to the stack memory space   String  str2= "Hello";   String  str3= "Hello";   System.out.println ("Str1==str2?-->" + (STR1==STR2))//true   System.out.println ("Str1==str3?-->" + (str1= =STR3)//true   System.out.println ("Str2==str3?-->" + (STR2==STR3));//true}}



the contents of the string cannot be changed    

public class Stringlei{public static void Main (string args[]) {   string  str= "Hello";   str=str+ "world!";   System.out.println ("str =" +str);}}

This is not a simple change to the contents of STR, as follows:


Java Object-oriented foundation string class

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.