Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of String literals is confusing

People also ask

What is special about a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

Are string literals bad?

While there is nothing inherently wrong with the use of string literals, there is room for improvement. String literals very often lead to stringly typed code, a play on strongly typed.

Which is incorrect string literal?

9. Which of these is an incorrect string literal? Explanation: All string literals must begin and end in the same line. 10.

Is there a difference between a string and a string literal?

The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator. String is a set of characters. Generally, it is necessary to perform String operations in most applications.


Every compile-time constant expression that is of type String will be put into the String pool.

Essentially that means: if the compiler can (easily) "calculate" the value of the String without running the program, then it will be put into the pool (the rules are slightly more complicated than that and have a few corner cases, see the link above for all the details).

That's true for all the Strings in lines 1-3.

"Hel"+lo is not a compile-time constant expression, because lo is a non-constant variable.

The hash codes are the same, because the hashCode of a String depends only on its content. That's required by the contract of equals() and hashCode().


Strings computed by concatenation at runtime are newly created and therefore distinct

here is a link to read: http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5


String object can be created in the following ways:

String str = new String("abcd");  // Using the new operator 
                                  // str is assigned with "abcd" value at compile time.

String str="abcd";         // Using string literal
                           // str is assigned with "abcd" value at compile time.

String str="ab" + "cd";    // Using string constant expression.
                           // str is assigned with "abcd" value at compile time.
String str1 = "cd";
String str = "ab"+str1;    // Using string expression.
                           // str is assigned with "abcd" value at run time only.

and Hashcode will be calculated only at runtime based on the contents of the String objects.


It's because the comipler in this instance is not smart enough to work out that it can burn in the same string literal.

Hashcode needs to always return the same value for strings that are equivelent (calling .equals on it returns true) so will return the same result.