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?

Thanks

Recommended Answers

All 7 Replies

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";
and
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

both are initiating a String instance but in a different way.

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")

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.

Thanks..a lot..for giving this brief knowledge about String

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.