Continue to look at the commonly used methods of the String class, as shown in the following code:
Operation Result:
So,what is the difference between "= =" and equals ()?
= =: Determines whether the first address in memory of two strings is the same, that is, whether it is the same string object
Equals (): Compares the content stored in two string objects in a consistent
PS: Bytes is the basic unit of computer storage information,1 bytes equals 8 bits , 1 characters stored in GBK encoding need 2 bytes ,1 English characters storage requires 1 bytes . So we see the above program running results, each Chinese character corresponds to two byte values, such as "learn" corresponding to "47-89", while the English letter "J" corresponds to "74". At the same time, we also found that the Chinese characters corresponding to the byte value is negative, because each byte is 8 bits, the maximum value can not exceed 127, and the Chinese character converted to a byte after more than 127, if more than will overflow, in the form of negative numbers display.
Case:
Function: Count the number of occurrences of the character ' a ' in the specified string
Analysis: You can iterate through each character in a string to determine if it is a character a, and if so, the number of occurrences of the cumulative statistic
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 //Define a string4String s = "ALJLKDSFLKJSADJFKLHASDKJLFLKAJDFLWOIUDSAFHAASDASD";5 6 //Number of occurrences7 intnum = 0;8 9 //Loop through each character to determine if it is a character a, and if so, the number of incrementsTen for(intI=0;i<s.length (); i++) One { A //gets each character, judging whether it is a character a - if(S.charat (i) = = ' A ') { - //Cumulative Statistics Number thenum++; - } - } -System.out.println ("The number of occurrences of character A:" +num); + } -}
Java Basics: Case conversion of string, separation of array, = = vs. equals ()