what is the difference between String s="hello"; and String
s=new String("hello");?

Answers were Sorted based on User's Feedback



what is the difference between String s="hello"; and String s=new String("hello"..

Answer / khan

Both are the way to create a String object
String s = "hello";
this is literal String if there is "hello" string presnet
in poll it doesn't create the new String "hello" it will
pick it form the pool and refer it
otherwise it will create the String "hello"

but in String s = new String("hello");
first the literal hello pick from the pool if it present
and pass to the overloaded String class constructor,
otherwise it will create and pass to the
overloaded constructor of String String(String s)
and create the new string object Through new String
("hello");
you shoul to checkit through
some coding what happens
String s = "hello";
String s1 = "hello";
String s3 = new String("hello");
s==s1 return true
s.equals(s1) return true
s==s3 return false
s.equals(s3) return true
and "previous answers also true which gave by other person"
scjp,scwcd,scbcd

Is This Answer Correct ?    11 Yes 0 No

what is the difference between String s="hello"; and String s=new String("hello"..

Answer / amit singh

not the difference both are the string
String s = "hello"
its aliteral string it will be in a pool
imagine that when you cretae the anothre string
String s1 = "hello"
thn ther is not new Styrin object wil cretae its will pick
from the pool where the literal strin g hello will presnet
so s>
----------"hello"

s1>
both pointing the same string
2)String s = new String("hello");
there are two string will create frist the litreal "hello"
then the other through the new String("hello"); which is
point through referenece s is pointed so there is always
create a new String which is pointed by the reference s
its not going to pick through the pool
amitsing2008@gmail.com
amit09mca(scjp 1.5 and manymore)

Is This Answer Correct ?    3 Yes 2 No

what is the difference between String s="hello"; and String s=new String("hello"..

Answer / amit singh

when you put String s = new String(null)
you will get the thing waht i'm trying to say that
beacuse my english is weak so what i'm trying to say
understatd
beacuse there is no realtion between the
1)which is for literal
2)which is for new String()
don't add both at the same time
because both are different question itself and different
answer itself.
but you will connect that then the problem is your own so
please
if you try to add both the thing is comeup
throug the itself that the hello string in a pool for the
first statement
so for the second String s = new String("hello");
compiler go to the pool and and check that "hello" it is
present if it present then it will pick it through the pool
and assign it to the overloaaded constructor of the String
class which will take the String(string s)so new operator
creates new object which will contain the hello.
and the the new String object will create so please check
to put String s = new String(null)
you will get ambiguous error
overloaded constructor in String class
String(String s) and String(StringBuffer s) and many
and you will get what i'm trying to say so do't add
ist STATEMENT TO SECOND WHICH IS FOR NEW STRING()
thanks
amitsing2008@gmail.com

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

What is java autoboxing?

0 Answers  


explain copyonwritearraylist and when do we use copyonwritearraylist?

0 Answers  


Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? How can a hash map syncronized with out using syncrozed blocks in programm?

3 Answers   Four soft,


What is object cloning in Java?

0 Answers   SwanSoft Technologies,


What is fail fast in java?

0 Answers  






If a variable is declared as private, where may the variable be accessed?

0 Answers  


What we have to do, when we don't want to implement all methods of an interface?

5 Answers  


Why 1 is not a prime number?

0 Answers  


Is array synchronized in java?

0 Answers  


What is difference between path and classpath variables?

0 Answers  


What's a method in programming?

0 Answers  


What is variable in java?

0 Answers  


Categories