10 questions about string types in Java

Source: Internet
Author: User

1. How do I compare two strings? with "=" or equals

Simply put, "= =" is used to detect whether two references point to the same object in memory, while the Equals () method detects that the values of two objects are equal. You have to use the Equals () method as long as your item detects two strings that are not equal.

If you know the concept of "string intern", that's better.

2. Why is security-sensitive string information using char[] better than a string object?

String objects are immutable, which means that they do not change until the garbage collector comes clean. With an array, it is possible to modify the character element in any position. In this way, security-sensitive information, such as passwords, will not be present anywhere in the system.

3. Can a string object be used in a switch expression?

Starting with JDK7, we can use a string in the switch conditional expression, which means that the version before 7 is not possible.

Java 7 only!
Switch (Str.tolowercase ()) {
Case "a":
Value = 1;
Break
Case "B":
Value = 2;
Break
}

4. How do I convert a string to an integer value?

int n = integer.parseint ("10");
So simple, often used, is occasionally forgotten.

5. How do I use spaces to separate strings?

We can easily use regular expressions to separate them. "\s" means spaces, as well as "", "\ T", "\ r", "\ n".
string[] Strarray = Astring.split ("\\s+");

6. Substring () method specifically, what are they doing?

In JDK6, this method will only represent the resulting string on a character array that identifies an existing string, but does not create a new string object. If you need to create a new string object, you can do so after the result + an empty string:
Str.substring (M, N) + ""
If you write like this, a new character array is created to represent the result string. At the same time, there is a chance that your code will run faster because the garbage collector can leave substrings without a large string collection in use.
The substring () method in Oracle JDK7 creates a new character array instead of the one that existed earlier. Look at this picture to see the difference between the substring () method in JDK6 and JDK7.

7. String&stringbuilder&stringbuffer

String vs Stringbuilder:stringbuilder is mutable, which means that you can also modify its value after you create the object. StringBuilder vs Stringbuffer:stringbuffer is synchronous, meaning it is thread-safe, but it will be slower than StringBuilder.

8. How to construct a string quickly and repeatedly?

In Python programming, you just have to multiply the string by a number to get it done, and in Java programming we can use the repeat () method from the StringUtils class in the Apache Commons Lang packet.
String str = "ABCD";
String repeated = Stringutils.repeat (str,3);
Abcdabcdabcd

9. How do I convert a time-format string to a Date object?

String str = "Sep 17, 2013";
Date date = new SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str);
SYSTEM.OUT.PRINTLN (date);
Tue Sep 00:00:00 EDT 2013

10. How do I count the number of occurrences of a character in a string?

This work can be done using the StringUtils class in the Apache Commons lang package.
int n = stringutils.countmatches ("11112222", "1");
System.out.printl ' n (n);

10 questions about string types in Java

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.