String Questions Flashcards

1
Q

How to compare strings? Use “==” or use equals()?

A

In brief, “==” tests if references are equal and equals() tests if values are equal. Unless you want to check if two strings are the same object, you should always use equals().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is char[] preferred over String for security sensitive information?

A

Strings are immutable, which means once they are created, they will stay unchanged until Garbage Collector kicks in. With an array, you can explicitly change its elements. In this way, security sensitive information(e.g. password) will not be present anywhere in the system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can we use string for switch statement?

A

Yes to version 7. From JDK 7, we can use string as switch condition. Before version 6, we can not use string as switch condition.

// java 7 only!
switch (str.toLowerCase()) {
      case "a":
           value = 1;
           break;
      case "b":
           value = 2;
           break;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to convert string to int?

A

int n = Integer.parseInt(“10”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to split a string with white space characters?

A

We can simple do split using regular expression. “\s” stands for white space characters such as ” “, “\t”, “\r”, “\n”.

String[] strArray = aString.split(“\s+”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What substring() method does?

A

In JDK 6, the substring() method gives a window to an array of chars which represents the existing String, but do not create a new one. To create a new array to back string, you can do add an empty string like the following:

str.substring(m, n) + “”
This will create a new char array that represents the new string. The above approach sometimes can make your code faster, because Garbage Collector can collect the unused large string and keep only the sub string.

In Oracle JDK 7, substring() creates a new char array, not uses the existing one. Check out the diagram for showing substring() difference between JDK 6 and JDK 7.

  1. String vs StringBuilder vs StringBuffer
    String vs StringBuilder: StringBuilder is mutable, which means you can modify it after its creation.
    StringBuilder vs StringBuffer: StringBuffer is synchronized, which means it is thread-safe but slower than StringBuilder.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to repeat a string?

A

In Python, we can just multiple a number to repeat a string. In Java, we can use the repeat() method of StringUtils from Apache Commons Lang package.

String str = "abcd";
String repeated = StringUtils.repeat(str,3);
//abcdabcdabcd
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to convert string to date?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to count # of occurrences of a character in a string?

A

Use StringUtils from apache commons lang.

int n = StringUtils.countMatches(“11112222”, “1”);
System.out.println(n);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe the main method?

public static void main(String[] args)

A

a) public means that the method is visible everywhere
b) static means you don’t need an instance of the class to call the method
c) void means the method doesn’t return anything
d) main is just the predefined method name for how to start a Java program
e) String[] means the method accepts an array of strings as arguments to the method
f) args is the name of the parameter to the method. Any name will do here. There is no hard and fast requirement that the parameter be called args

How well did you know this?
1
Not at all
2
3
4
5
Perfectly