Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String literals concatenation

    public static void main(String[] args){
        one();
        two();
        three();
    }

    public static void one() {
        String s1 = "hill5";
        String s2 = "hill" + 5;
        System.out.println(s1==s2);
    }

    public static void two() {
        String s1 = "hill5";
        int i =5;
        String s2 = "hill" + i;
        System.out.println(s1==s2);
    }

    public static void three() {
        String s1 = "hill5";
        String s2 = "hill" + s1.length();
        System.out.println(s1==s2);
    }

Output is

true 
false 
false 

String literals use interning process,then why two() and three() is false.I can understand in case of three() but two() is not clear.but need proper explanation for both cases.

Can someone please explain proper reason?

like image 952
ankita gahoi Avatar asked May 27 '13 10:05

ankita gahoi


People also ask

Can you concatenate string literals?

Concatenate multiple stringsYou can combine both string variables and string literals using the “+” operator.

How do you concatenate input strings in Java?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

Can you concatenate strings in Java?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

Why is string concatenation N 2 in Java?

String concatenation It can be a bit surprising, but this code actually runs in O(N2) time. The reason is that in Java strings are immutable, and as a result, each time you append to the string new string object is created.


2 Answers

String with compile time constant expression will be put on String pool. The main condition is compile time constant expression. If you make local variable final in method two() then two() will also print true

public static void two() {
    String s1 = "hill5";
    final int i =5;
    String s2 = "hill" + i;
    System.out.println(s1==s2);
}

Output:

true
like image 85
Abu Yousuf Avatar answered Oct 04 '22 00:10

Abu Yousuf


In case of 2 and 3 , Compiler cannot calculate the value of String , since hill + i is a runtime statement , same for s1.length()

read here which i asked the same case - link

Think like this the String s1 and s2 are using compile time constant , s1="hill5" and s2="hill" + 5 , remember , string assigned as a literal is constant , its state cannot be modified , as String are immutable.

So at Compile time , compiler says "oh yeah , they are calculated as same value , i must assign the same reference to s1 and s2".

But in case of method two() and three() , compiler says " i dont know ,may be value of i can be changed any time , or s1.length() changes any time " , its a runtime thing , so compiler doesn't put s2 of two() and three() method in pool ,

Hence , they are false because at runtime , new object is created as soon it get changed right !!

like image 33
anshulkatta Avatar answered Oct 04 '22 01:10

anshulkatta