Java String FAQs

Source: Internet
Author: User

This article describes the 10 most common questions about String in Java: 1. String comparison, using "=" or equals ()? To put it simply, "=" is used to determine whether the two referenced addresses are the same memory address (the same physical object ). equals checks whether the values of the two strings are equal. unless you want to determine whether two strings reference the same object, you should always use the equals () method. if you know about String Interning, you can better understand this problem. for sensitive information, why should char [] Be Better Than String? String is an unchangeable object, meaning that once created, the entire object cannot be changed. even if the newbie thinks that the String reference has changed, the (pointer) Reference actually points to another (new) object. programmers can explicitly modify character arrays, so sensitive information (such as passwords) is not easily exposed elsewhere (as long as you set char [] to 0 after use ). 3. in the switch statement, use String as the case condition? From JDK 7, this is acceptable. It is not supported in Java 6 and earlier versions. [java] // only works in java 7 and later versions! Switch (str. toLowerCase () {case "a": value = 1; break; case "B": value = 2; break;} 4. use Long to convert String to a number. The Code is as follows [java] int age = Integer. parseInt ("10"); long id = Long. parseLong ("190"); // if the value may be large. 5. the String received by using the split () method of String split by blank characters is parsed as a regular expression. "\ s" indicates blank characters, such as spaces "", tab "\ t", line feed "\ n", Press ENTER "\ r ". while the compiler parses the source code, it also performs a literal transcoding, so it needs "\ s ". [java] String [] strArray = aString. split ("\ s +"); 6. substring () How is the method internally handled? In JDK 6, the substring () method still shares the original char [] array, and constructs a "new" String by offset and length. To obtain a newly created object using substring (), use the following method: [java] String sub = str. substring (start, end) + ""; of course, in Java 7, substring () creates a new char [] array instead of sharing. for more information, see substring () methods in JDK 6 and JDK 7 and their differences. string vs StringBuilder vs StringBufferStringBuilder is variable. Therefore, you can modify the internal values after creation. stringBuffer is synchronous, so it is thread-safe, but the efficiency is relatively lower. 8. how to repeat a string? Solution 1: Use the StringUtils tool class of the Apache Commons Lang library. [java] String str = "abcd"; String repeated = StringUtils. repeat (str, 3); // abcdabcdabcd solution 2: constructed using StringBuilder. more flexible. [java] String src = "name"; int len = src. length (); int repeat = 5; StringBuilder builder = new StringBuilder (len * repeat); for (int I = 0; I <repeat; I ++) {builder. append (src);} String dst = builder. toString (); 9. how to convert String to date? [Java] SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd"); String str = "2013-11-011-07"; Date date = format. parse (str); System. out. println (format. format (date); // 10. how to count the number of times a character appears? Also use the Apache Commons Lang library StringUtils class: [java] int n = StringUtils. countMatches ("11112222", "1"); System. out. println (n );

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.