String s="abc"
in java.what happened in the memory any object is created or not and what is "s
" here variable or object,and same question with String s=new String("abc");
.

- 2,572
- 6
- 41
- 77
-
2Only two lines and horrible to read. Use punctuation. Also, flagged as duplicate. Use google. See http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java – jp-jee Nov 24 '14 at 11:28
2 Answers
There's a thing called String Memory Pool in java, when you declare:
String str1="abc";
It goes to that memory pool and not on the heap. But when you write:
String str2=new String("abc");
It creates a full fledged object on the heap, If you again write:
String str3 = "abc";
It won't create any more object on the pool, it will check the pool if this literal already exists it will assign that to it. But writing:
String str4 = new String("abc");
will again create a new object on the heap
Key point is that:
A new object will always be created on the heap as many times as you keep writing:
new String("abc");
But if you keep assigning the Strings directly without using the keyword new, it will just get referenced from the pool (if it exists in the pool)

- 5,794
- 2
- 24
- 44
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

- 3,031
- 7
- 42
- 67
-
You are correct, however part of OP asked " what is "s" here variable or object,and same question with String s=new String("abc") :) – Harry Nov 24 '14 at 11:50
-
It's is an object but we call it as a String literal just because the object does not lies on the heap, java created the concept of string memory pool because strings are heavily used, and it's not a good thing to create a new object containing same data everytime – Sarthak Mittal Nov 24 '14 at 11:57