I have doubt that what we are using in java as String, is it class or datatype?

String str = "hello"
String s1 = new String("Hi")

What is the difference in above two statements?
Which is object of String class and which is varialble of datatype String?

Because as i know, there are String class also and we are using different methods like toUpperCase, toLowerCase, subString, CompareTo, equals etc.....

So, how java differentiate between class object and variable?


4 Years
Discussion Span
Last Post by jalpesh_007

ehm ... what do you mean: "class OR datatype"?
a String object is an object that is an instance of the class String, so the instance is of datatype String.

not sure what it is you mean?
the above two lines:
String str= "hello";
String s1 = new String("Hi");
both instantiate an object of type String

just check this for more information

both the statements 
    String str = "hello"
    String s1 = new String("Hi")

    create objects of class string they have a slight differnce in the way they are created
    String str = "hello"
    is created in the string pool 
    and the second one 
    String s1 = new String("Hi")
    is created on the heap

Here's the explanation ripped of the Java link stultuske provided:

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.

I emphasized the part which is of interest to you.

So basically, having

String something = "Something"

will be replaced by the compiler with

String something = new String("Something")

Edited by Lucaci Andrew


In Java you have primitive and reference types.
Primitives are boolean, char, byte, short, int, long, float, double.
Anything else is a reference type.


So basically, having
String something = "Something"
will be replaced by the compiler with
String something = new String("Something")

I'm not sure whether you can consider these as equivalent. Following that rule the following would create two distinct String instances, however that is not the case:

String something = "Bla";
String somethingElse = "Bla";

The follwing is stated in the JLS 3.10.5:

Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.

Edited by mvmalderen

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.