Java Basics: Case conversion of string, separation of array, = = vs. equals ()

Source: Internet
Author: User

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 ()

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.