• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

why we r not using 'new' for String declaration

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic