import java.util.*;
import java.lang.*;
import java.io.*;
class Test {
public static void main (String[] args) {
String a="hello"+"world"; //line 1
String b="hello";
String c="world";
String d=b+c;
String e="helloworld";
System.out.println(e==a);
System.out.println(a==d);
}
}
output:
true
false
From this discussion How does the String class override the + operator? I understood that 'a' and 'e' will refer to same String literal object.
Could anybody please tell
Why a and d are not referring to same string literal object?
How many objects were creating in line 1
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.
A string literal is simply a reference to an instance of the String class, which consists of zero or more characters enclosed in double quotes. Moreover, a string literal is also a constant, which means it always refers to the same instance of the String class, due to interning [2].
A string literal in Java is basically a sequence of characters from the source character set used by Java programmers to populate string objects or to display text to a user. These characters could be anything like letters, numbers or symbols which are enclosed within two quotation marks.
String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
From JLS 4.3.3. The Class String
The string concatenation operator
+
(§15.18.1) implicitly creates a new String object when the result is not a compile-time constant expression (§15.28).
Because literal value of a is "helloworld"
and literal value of d is a reference object of b
+ c
that is not a literal as from above JLS 4.3.3.
From JLS 3.10.5 String Literals
A string literal consists of zero or more characters enclosed in double quotes.
From 15.28. Constant Expressions
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
If you want to make System.out.println(a==d);
true use final
keyword see below code:
String a="hello"+"world"; //line 1
final String b="hello"; // now b is a literal
final String c="world";// now c is a literal
String d=b+c;
String e="helloworld";
System.out.println(e==a);
System.out.println(a==d);// now it will be true because both are literals.
Answer for comment:
How many objects were creating in line 1: Answer is 3
String a="hello"+"world";
1st literal object
hello
2nd literal objectworld
3rd literal object"hello"+"world"
=="helloworld"
The ==
sign checks if references to variables are the same. To check if any object is equal to other use Object.equals(Object o) method like this:
e.equals(a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With