Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why we r not using 'new' for String declaration  RSS feed

 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


String is a class
but when we say String s = "abc";
we r not using 'new' here
then how its creating String object
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're telling Java to take a String, whose value is "abc", from the pool. If it doesn't exist yet, the jvm will take care of creating one for you.
 
Mateusz Sob
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class in Java is special.
This is the only Object class that you can use:
String s = "abc";
or
String s = new String("abc");
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the String class is not special. The String class is just a class like any other class.

What is special about strings is that you can specify String literals in Java. A String literal is special syntax to specify a String object.

Never use the String constructor that takes a String argument with a String literal - it is unnecessary; it only makes your source code longer and more complicated and inefficient (because you're creating an unnecessary extra String object).

What happens when you do this, for example:

String text = new String("Hello World");

1. The compiler generates a String object automatically that represents the literal "Hello World".
2. You create a new String object in which the content of the first String object is copied (which is unnecessary and inefficient).
[ June 08, 2006: Message edited by: Jesper Young ]
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for u r reply

is there any diff in these both

String s = "abc";
String s = new String("abc");

how many reference variables and objects get created in each of both
 
krishna nav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = "abc";
This statement creates the object in String pool & returns the reference.

String s = new String("abc");
This statement creates the object in Heap & returns the reference.

after above statements, now if you do
String s1 = "abc";
the reference of "abc" already existing in String pool is returned.

String s2 = new String("abc");
This will create new object & returns the refernce.






 
krishna nav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = "abc";
This statement creates the object in String pool & returns the reference.

String s = new String("abc");
This statement creates the object in Heap & returns the reference.

after above statements, now if you do
String s1 = "abc";
the reference of "abc" already existing in String pool is returned.

String s2 = new String("abc");
This will create new object & returns the refernce.

try using equals() & == , you will come to know the difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!