Amit Tomar
Amit Tomar

Reputation: 4858

Initializing a class object using literals

If I write this code :

String s = new String("TestString");

I understand how s refers to a string created dynamically. s is not an object in itself, but refers to one.

But I am not able to figure out what this means :

String s = "TestString";

Q1. If it would have been some primitive data type I would understand, but what does this signify for a class type ?

Q2. Is this kind of initialization allowed for user created classes as well ?


Java Level : Beginner

Upvotes: 2

Views: 638

Answers (4)

jason
jason

Reputation: 241641

Q1. If it would have been some primitive data type I would understand, but what does this signify for a class type ?

In this case, "TestString" is a string literal. A string literal also serves as a reference to an instance of String. This is per the language specification, §3.10.5. So, in your particular case "TestString" is a reference to an instance of String, and you are assigning that same reference to your variable s.

Now, there are some rather special things about Strings that are referred to by literals. Two string literals with the same value (logically, as strings) always refer to the same instance of String. This is due to the "interning" of string literals.

However, when you say

String s = new String("TestString");

it is still the case that "TestString" refers to an instance of String, in fact to an instance in the string intern pool, but it is not the case that s refers to this same string. Instead, s is initialized to have its value equal to "TestString", but it is in fact a new reference. That is:

String s = new String("TestString");
String t = "TestString";
System.out.println(s == t);

This will print false.

Q2. Is this kind of initialization allowed for user created classes as well ?

No.

Upvotes: 4

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

Q1. If it would have been some primitive data type I would understand, but what does this signify for a class type ?

No, this is special case, in case of String literals, String s = "someString" statement means we are referring to someString which is stored in string constant pool. someString will be an instance of String class but will be stored in string literal pool.

The special thing about String literal pool will be.

String s = "someString"; String s1 = "someString";

Here, s == s1' will returntrue` as they will refer to the same object in string literal pool.

String s2 = new String("someString"); String s3 = new String("someString");

Here, s2 == s3 will return false as both string will be created in non-constant pool memory.

You can find a good tutorial regarding strings here

http://www.thejavageek.com/2013/06/19/the-string-constant-pool/ http://www.thejavageek.com/2013/06/17/string-immutability-in-java/

Q2. Is this kind of initialization allowed for user created classes as well ?

No we can't.

Upvotes: 0

lamarvannoy
lamarvannoy

Reputation: 485

From the Oracle Documentation:

The most direct way to create a string is to write:

String greeting = "Hello world!";

In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

As with any other object, you can create String objects by using the new keyword and a constructor.

Upvotes: 0

bluehallu
bluehallu

Reputation: 10285

String s = "TestString";

Is the normal way to create a String. In fact when you do:

String s = new String("TestString");

What you're doing is create a string first, then passing it as an argument to new String(); So the question is not why the first one exists, but why the second one does. The answer is pretty subtle and you probably won't ever care: The first way creates a String literal that doesn't get garbage collected, and is shared on all the VM. The second one, instead, does. This means, for performance reasons, there are cases when you want to use the second form, like when working with very very large strings.

You can read more about it here:

http://kjetilod.blogspot.com.es/2008/09/string-constructor-considered-useless.html

Upvotes: 0

Related Questions