Interview Ten common Java string issues

Source: Internet
Author: User

This article describes the 10 most common questions about string in Java:

1. String comparisons, using "= =" or equals ()?
Simply put, "= =" Determines whether two references are the same memory address (the same physical object).
equals determines whether the values of two strings are equal.
Unless you want to determine whether two string references are the same object, you should always use the Equals () method.
If you understand the string interning, it will be better to understand the problem

2. Why is it better to use char[than string for sensitive information?
A string is an immutable object, meaning that once created, the entire object cannot be changed. Even if the novice feels that the string reference has changed, it is just that the (pointer) reference points to another (new) object.
The programmer can explicitly modify the character array so that sensitive information (such as passwords) is not easily exposed elsewhere (as long as you run out of char[] 0).

3. Use string as the case condition in the switch statement?
Starting with JDK7, this is possible, wordy, Java 6 and previous versions do not support this.

[Java]View Plaincopy
  1. Valid only in Java 7 and later!
  2. Switch (Str.tolowercase ()) {
  3. case "a":
  4. value = 1;
  5. Break ;
  6. case "B":
  7. Value = 2;
  8. Break ;
  9. }


4. Convert string to numeric
For very large numbers please use long, the code is as follows

[Java]View Plaincopy
    1. int age = Integer.parseint ("10");
    2. Long id = long.parselong ("190");  //If the value may be large.


5. How to split a string with whitespace characters
Strings received by the split () method of string are parsed as regular expressions.
"\s" represents a blank character, such as a space "", Tab tab "\ T", newline "\ n", and enter "\ r".
The compiler also takes a literal transcoding when parsing the source code, so "\\s" is required.

[Java]View Plaincopy
    1. string[] Strarray = Astring.split ("\\s+");


6. How is the substring () method handled internally?
In JDK6, the substring () method is also used to share the original char[] array, and a "new" string is constructed by offset and length.
Want to substring () get a completely new created object, using the following method:

[Java]View Plaincopy
    1. String sub = str.substring (start, end) + "";


Of course Java 7, substring () creates a new char[] array instead of sharing it.

For more information, refer to: JDK6 and JDK7 substring () methods and their differences

7. String vs StringBuilder vs StringBuffer
The StringBuilder is mutable, so you can modify the internal values after creation.
StringBuffer are synchronous and therefore thread-safe, but less efficient.


8. How do I double-stitch the same string?
Scenario 1: Use the Apache Commons Lang Library's StringUtils tool class.

[Java]View Plaincopy
    1. String str = "ABCD";
    2. String repeated = Stringutils.repeat (str,3); ABCDABCDABCD


Scenario 2:

Use StringBuilder constructs. More flexible.

[Java]View Plaincopy
    1. String src = "name";
    2. int len = Src.length ();
    3. int repeat = 5;
    4. StringBuilder builder = new StringBuilder (len * repeat);
    5. for (int i=0; i<repeat; i++) {
    6. Builder.append (SRC);
    7. }
    8. String DST = builder.tostring ();


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

[Java]View Plaincopy
    1. SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
    2. String str = "2013-11-07";
    3. Date date = Format.parse (str);
    4. System.out.println (Format.format (date)); //2013-11-07


10. How do I count the number of occurrences of a character?
Also use the Apache Commons Lang Library StringUtils class:

[Java]View Plaincopy
    1. int n = stringutils.countmatches ("11112222", "1");
    2. SYSTEM.OUT.PRINTLN (n);


More

How to detect a string full of lowercase letters?

Transferred from: http://blog.csdn.net/renfufei/article/details/14448147

Interview Ten common Java string issues

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.