2

Possible Duplicate:
Questions about Java's String pool

Guys what is the difference between these two.

(1)

String s = new String("hello"); // creating an object on heap then assign that object to the reference s.

(2)

String s = "hello" // did I make an object here?? Im not using the word new.

Also array example

int x[] = {1, 2, 3, 4, 5}; // did I make object here? 
Community
  • 1
  • 1
user1293258
  • 109
  • 8

3 Answers3

4

String s = "hello" is assigning the string literal "hello" to s

Using the new keyword, you create a new object, in addition to the string literal.

note that:

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

will most likely yield

true
false

Since s is referncing the string literal "hello" - you got an identity in the first case, while in the second it's a new object, and there is no identity - those are two different objects, that happen to contain the same value.

Regarding your array question: yes, an int[] object is created.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Just as a note, the String literal is a source-code representation of a pre-defined String object. So using the new keyword, you are actually making a *copy* of an existing String object. So you end up with 2 String objects instead of one. – Kevin Welker Mar 30 '12 at 16:14
  • so String s = "hello" didnt create a new object?? I thought in java everthing is an object?? – user1293258 Mar 30 '12 at 16:18
  • @user1293258: It is an object, it is just a *string literal*. the jvm maintains a *pool* of string literals, and reuses the same object. So, in the assiment `s = "hello"` and when later comparing `s` to `"hello"`, the same string literal is used - which is the same object. And regarding: `I thought in java everthing is an object` - it is not true. `int`, `boolean` [and all other *primitive types*] are **not** objects. – amit Mar 30 '12 at 16:22
  • how about this? String o = new String("hello"); then o == "hello" the answer will be false, why is it becuase string literal "hello" has a null character as a last character? – user1293258 Mar 30 '12 at 16:29
  • @user1293258: `new String("hello)` creates a new object. In java, `operator==` checks for *identity* and not *equality* - it checks if the two objects are the same object ["occupies the same space in memory"]. `new String("hello")` is located in a new place in memory, other then the one of the string literal, thus they are not the same objects and the result is `false` as expected. – amit Mar 30 '12 at 16:34
  • what uses the hashcode to compare? == or equals() ? – user1293258 Mar 30 '12 at 16:37
  • @user1293258: `hashCode()` does not compare objects, it generates int from a single object. If you mean how we compare two hash-codes of two different objects - we do it with `operator==` since the value returned by `hashCode()` is an `int` which is not an object. To check *equality* of two objects, we use `equals()`. – amit Mar 30 '12 at 16:40
  • does the string literal object also have hashcode? is it the reason why o == "hello" is false? – user1293258 Mar 30 '12 at 16:44
  • @user1293258: It has nothing to do with `hashCode()`. `"hello".hashCode() == new String("hello").hashCode()`. But this is a different discussion entirely. – amit Mar 30 '12 at 17:28
0
  1. Yes you have created a new string obj
  2. "hello" may be in the heap but its not really in the heap. And it's not created / allocated at runtime Instead, it is interned before your program gets going.
  3. Yes you created an int[] in the heap
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Regarding string constructor, you have always objects.

when you say

String test = "test";

you create one string object with content "test", but when you say

String test= new String("test");

you create one string object with content "test" and pass it as argument to the string constructor to create a new String object. So finally you had created 2 string objects.

The code of the constructor is:

public String(String original){.....}

The string constructor is consider useless in most of the cases.

In this thread they described when it can be used

Use of the String(String) constructor in Java

Community
  • 1
  • 1
Oscar Castiblanco
  • 1,626
  • 14
  • 31