Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: String literal and + operator

Tags:

java

string

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

  1. Why a and d are not referring to same string literal object?

  2. How many objects were creating in line 1

like image 966
nantitv Avatar asked Apr 14 '15 08:04

nantitv


People also ask

What is difference between literal and operator in Java?

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.

What is the string literal in Java?

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].

What is string and string literal in Java?

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.

What is difference between string and string literal?

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.


2 Answers

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 object world
3rd literal object "hello"+"world" == "helloworld"

like image 150
Sumit Singh Avatar answered Sep 20 '22 07:09

Sumit Singh


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);
like image 34
novy1234 Avatar answered Sep 24 '22 07:09

novy1234