You can use String.valueOf
builder.indexOf(String.valueOf(c));
There are good things about this approch.
- Clean code
- String.valueOf creates String object using char[] from passed char like
char data[] = {c};so no additional operation is required.
2 is really a micro optimization and I would always go for option 1 i.e. "clean code".
For what it's worth, here's the bytecode generated by the concatenation version:
new #2; //class java/lang/StringBuilder
dup
invokespecial #6; //Method java/lang/StringBuilder."<init>":()V
aload_1
invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
ldc #8; //String
invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
As you can see, it creates a second StringBuilder, does two append calls, and then a toString. In contrast, here's the String.valueOf version:
aload_0
aload_1
invokestatic #12; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
That just hands the Character (which has already been automatically unboxed into a char) off to String.valueOf. So what does that do? Let's look at the JDK source code:
public static String valueOf(char c) {
char data[] = {c};
return new String(0, 1, data);
}
So it creates a new one-character array and hands off directly to the String constructor. Very likely to be more efficient.
But again, it's probably a micro-optimization. The String.valueOf call makes the code clearer, which is the main thing.
Videos
You can use String.valueOf
builder.indexOf(String.valueOf(c));
There are good things about this approch.
- Clean code
- String.valueOf creates String object using char[] from passed char like
char data[] = {c};so no additional operation is required.
2 is really a micro optimization and I would always go for option 1 i.e. "clean code".
For what it's worth, here's the bytecode generated by the concatenation version:
new #2; //class java/lang/StringBuilder
dup
invokespecial #6; //Method java/lang/StringBuilder."<init>":()V
aload_1
invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
ldc #8; //String
invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
As you can see, it creates a second StringBuilder, does two append calls, and then a toString. In contrast, here's the String.valueOf version:
aload_0
aload_1
invokestatic #12; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
That just hands the Character (which has already been automatically unboxed into a char) off to String.valueOf. So what does that do? Let's look at the JDK source code:
public static String valueOf(char c) {
char data[] = {c};
return new String(0, 1, data);
}
So it creates a new one-character array and hands off directly to the String constructor. Very likely to be more efficient.
But again, it's probably a micro-optimization. The String.valueOf call makes the code clearer, which is the main thing.
The String and StringBuilder/StringBuffer classes in Java uses brute force method of pattern first exact string matching algorithm. This algorithm involves checking for an occurrence of the pattern at all positions of the text starting at some position and moving by one position, so I feel String.indexOf and StringBuilder.indexOf will be identical in terms of performance.
However, the String class has a indexOf(char c) method while StringBuilder/StringBuffer have indexOf(String s) methods, so if you searching for a character in StringBuilder you will have to first cast the char to String. In your example, that would probably be the only overhead.
Note: The brute force algorithm employed by String and StringBuilder classes are enough when you are dealing with small or medium sized texts but when dealing with large documents, it is preferable to use more sophisticated search string search algorithms.