JavaRush /Java Blog /Random EN /Top 10 Java String Questions
ext4
Level 20

Top 10 Java String Questions

Published in the Random EN group

10 most asked questions about strings in Java are mentioned here

[Translated] Top 10 Java String Questions - 1
  1. How to compare strings? What to use: " ==" or equals()?

    In short, " ==" compares references to strings, but equals()compares the values ​​contained in those strings. If you don't want to check that two strings are the same object, then you should use equals().

    It would also be nice to know the concept of a string pool .

  2. Why char[]is string ( String) preferred over sensitive information?

    Strings are immutable . This means that once a string is created, it will not change until the garbage collector mechanism is run. In the case of an array, it is quite obvious that we can change its elements. Thus, confidential information (for example, a password) will not be stored in clear text anywhere in the program.

  3. Can we pass strings as a parameter when using a switch statement ?

    Yes, starting from version 7 of the JDK. Earlier in the 6th version, this was not possible.

    // java 7 only!
    switch (str.toLowerCase()) {
          case "a":
               value = 1;
               break;
          case "b":
               value = 2;
               break;
    }
  4. How to convert string to integer value?

    int n = Integer.parseInt("10");

    The same is true for other Java primitive types.

  5. How to split a string into lines with a space delimiter?

    The easiest way is to use regular expressions. " \s" acts as a substitute for whitespace characters such as " ", " \t", " \r", " \n".

    String[] strArray = aString.split("\\s+");
  6. < What does the method actually do substring()?

    In JDK 6, the method substring()showed a portion of the characters from an existing string, but did not create a separate instance of the string. To create a new string represented as an array of characters, you can add some empty string, for example, like this:

    str.substring(m, n) + ""

    As a result, we get an array of characters, which is our new string. This approach can sometimes reduce code execution time, because the garbage collector can destroy unused large strings and store only substrings.

    In Oracle JDK 7, the method substring()creates a new character array without using an existing one. You can also take a look at the diagram showing the difference between the substring() methods in JDK 6 and JDK 7 .

  7. String vs StringBuilder vs StringBuffer

    Stringvs StringBuilder: StringBuildermutable, which means we can change its content after the object has been created.

    StringBuilderversus StringBuffer: StringBuffersynchronized, which means that it functions correctly when used from multiple threads at the same time, but is slower than StringBuilder.

  8. How to repeat a string multiple times?

    In Python, we can simply multiply a string by a number nand it will then repeat n times. In Java, we can use the method repeat()from the StringUtilsApache Commons Lang library class.

    String str = "abcd";
    String repeated = StringUtils.repeat(str,3);
    //abcdabcdabcd
  9. How to convert string to date?

    String str = "Sep 17, 2013";
    Date date = new SimpleDateFormat("MMMM d, yy", Locale.ENGLISH).parse(str);
    System.out.println(date);
    //Tue Sep 17 00:00:00 EDT 2013
  10. How to count the number of occurrences of a certain character in a string?

    Use a class StringUtilsfrom the Apache Commons Lang library.

    int n = StringUtils.countMatches("11112222", "1");
    System.out.println(n);
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION