JavaRush /Java Blog /Random EN /10 Most Asked Questions About Strings in Java
ext4
Level 20

10 Most Asked Questions About Strings in Java

Published in the Random EN group

Top 10 most asked questions about strings in Java are mentioned here

[Translation] 10 most asked questions about strings in Java - 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 a good idea to know the concept of a string pool .

  2. Why char[]are strings ( String) preferred for protecting sensitive information?

    Strings are immutable . This means that once a string is created, it will not be modified until the garbage collector is run. In the case of an array, it is quite obvious that we can change its elements. This way, sensitive information (such as 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. Previously, in version 6, this was not possible.

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

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

    The same goes for other primitive types in Java.

  5. How to split a string into space delimited lines?

    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) + ""

    The result is 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 representing the difference between substring() methods in JDK 6 and JDK 7 .

  7. String vs StringBuilder vs StringBuffer

    Stringvs StringBuilder: StringBuildermutable, this means that we can change its content after the object is created.

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

  8. How to repeat a line several times?

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

    String str = "abcd";
    String repeated = StringUtils.repeat(str,3);
    //abcdabcdabcd
  9. How to convert a string to a 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