Java beginners must know about Java string problems

Source: Internet
Author: User

Excerpted from Http://developer.51cto.com/art/201503/469443.htm

Here I summarize the 10 Java developers often ask about the Java string problem, if you are also Java beginners, take a closer look:

1, how to compare strings, should use "= =" or equals ()?

In general, "= =" is used to compare the reference address of a string, and equals () is the value of the comparison string. Two strings with the same value may be false with "= =", while equals () must be true. Unless the two string is the same new object, the comparison string value should be equal to equals ().

2. For those security-sensitive information, why use char[] to store better than string?

The string is immutable, which means that once it is created, it will reside permanently in memory until the garbage collector recycles it. With array storage, however, you can explicitly change the elements in the array so that the security information will probably not exist anywhere in the system memory in the array way.

3. Can I use a string to declare a switch statement?

JAVA 7 and later versions are supported. In JDK 7, you are allowed to use strings as a comparison condition for switch statements. The previous version of JDK 6 cannot be used this way:

    1.                                   switch (str.tolowercase ()) {case "a": = 1;   Break ;         Case "B":            = 2;              Break

4. How do I convert a string to a numeric int type?

    1. Int

It's simple, but it's often used, and it's easy to ignore.

5, how to use space characters to split the string?

We can easily use regular expressions to split the string, "s" for space characters, such as "," "T", "R", "N"

    1. string[] Strarray = Astring.split ("s+");

6. What is the substring () method?

In JDK 6, the substring () method provides a window that intercepts characters in the original string, and it does not create a new string instance. If you want to create a new character array, you can add a null character after substring (), just like this:

This creates a new string instance, which can sometimes make your program run faster because the garbage collector can reclaim the useless large string and preserve its substring.

7, String, StringBuilder and StringBuffer which is better?

String and Stringbuilder:stringbuilder are mutable, meaning that strings created with StringBuilder you can change it at any time. StringBuilder and Stringbuffer:stringbuffer are synchronous, and they are thread-safe (thread-safe), but are much less efficient than StringBuilder.

8, how to repeat the output string?

In Python, we only need to multiply the string by a number to repeat the output of the string. In Java, however, we can use the StringUtils repeat () method, StringUtils is one of the class library members of the Apache Common Language library.

    1. String str = "ABCD";   = Stringutils.repeat (str,3);   //

9. How do I convert a string to a date type?

You can do this in the following manner, the code is as follows:

    1. String str = "Sep,";   New SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str);  SYSTEM.OUT.PRINTLN (date);   //

10. How to count the frequency of occurrences of a specified character in a string

We also took advantage of StringUtils in the Apache Common Language library, with the following code:

    1. int n = stringutils.countmatches ("11112222", "1"

Java beginners must know about Java string problems

Related Article

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.