• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialization

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody
really confused aboout this
What is the differece between these two initializations

String s="Hello!";

AND
String s=new String(Hello!);
are they the same???
Thanx.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zukof,
I dont think there is any difference between these 2 string initializations.
Could anybody help..
regards.
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second one is not compilable!
Even you change it by adding quotes, they are still different.
Read here: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html
I think you should post this on biginner's forum.
Roseanne
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
String s="hello"; is a string literal.
String s=new String("hello"); is an object.
there is difference in these two initializations..
In Java if two string literal are same then they point to the same memory location...for example..
String s="hello";
String s1="hello"; are equal...when u compare like (s==s1)..they print "true"...but the same thing is false if two String object are compared...for example
String s=new String("hello");
String s1=new String("hello");
they print "false" when compared like this(s==s1)
becuse == refers to the memory locations...
this site will clarify ur doubts..
"http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html"
hope this answer clear's ur doubts
ganapathy
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought they were the same, and after reading that article, I still do.


Because the compiler automatically creates a new String object for every literal string it encounters, you can use a literal string to initialize a String.
String s = "Hola Mundo";
The above construct is equivalent to, but more efficient than, this one, which ends up creating two Strings instead of one:
String s = new String("Hola Mundo");


 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"zukof",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp .
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a basic concept that comes up a lot in the scjp exams. When you declare a string as such
String s = "Hello";
then it is initialized and placed into a constant pool in the VM when the class is loaded for the first time. Therefore, if you code this
String s1 = "Hello";
String s2 = "Hello";
they will actually reference the same string in the pool and the statement s1 == s2 will be true. However, when you declare a string as such
String s3 = new String("Hello");
then this is instantiated when the instance of the class is created and it is not in the pool. Therefore, s3 == s1 will result in false. Interestingly, you can force s3 into the pool by calling s3.intern();
I hope this helps.
Sean
 
reply
    Bookmark Topic Watch Topic
  • New Topic