Hands-on Brain (mend)

Source: Internet
Author: User

public class Stringpool {

public static void Main (String args[])

{

String s1= "a";

String s2=s1;

System.out.println (S1==S2);//true

s1+= "B";

System.out.println (S1==S2);//false

SYSTEM.OUT.PRINTLN (s1== "AB");//false

System.out.println (s1.equals ("AB"));//true

}

}

Parsing: Assigning a value to a string means that two variables now refer to the same string variable object, so the S1==s2 return value of the True,string object is read-only, using + modifies the value of the S1 variable, actually getting a new string with the content AB, regardless of the object referenced by the original S1, So the return value is false, the string constant refers to a string that is not related to the object referenced by S1, and the String.equas () method can compare the contents of a two string.

public class Stringequals {

/**

* @param args the command line arguments

*/

public static void Main (string[] args) {

String S1=new string ("Hello");

String S2=new string ("Hello");

System.out.println (S1==S2);//Create two different objects with the new keyword, so the return value is False

System.out.println (s1.equals (S2));//content Same, value true

String s3= "Hello";

String s4= "Hello";

System.out.println (S3==S4);//content the same constant, referencing the same object, the return value is True

System.out.println (S3.equals (S4));//content Same, return value true

}

}

Length ()//returns the current string lengths

charAt (int index): takes one of the characters in a string, where the argument index refers to the ordinal number in the string. The ordinal number of the string is from 0 to length ()-1.

GetChars (int srcbegin,int srcend,char[]dst,int Dstbegin): This method copies a string to a character array. Where Srcbegin is the starting position of the copy, Srcend is the end position of the copy, the string array DST is the target character array, and Dstbegin is the starting position of the destination character array copy.

Replace (char Oldchar,char Newchar)//replaces the first Oldchar in a string with Newchar

toUpperCase ()://convert String to uppercase

toLowerCase ()://convert string to lowercase

Trim ()://Returns the string after the beginning and trailing spaces are removed

ToCharArray ()://Convert a String object to a char array

 

Hands-on brain (complement)

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.