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

Explain the meaning of java applet.

0 Answers   TCS,


What are access specifiers in java ?

0 Answers   Akamai Technologies,


What is final?

0 Answers  


What is console based application in java?

0 Answers  


Urgent requirement of JAVA/.NET/QA in a CMMI level3 MNC for Pune location.

0 Answers  






what is the collable collections in java?

2 Answers   Persistent,


'A class is a template for an object' explain this statement.

0 Answers   Akamai Technologies,


Can we force the garbage collection to run?

0 Answers  


What is the difference between a vector & an array list?

0 Answers   Agilent,


What is the concatenation operator in java?

0 Answers   Aspire, Infogain,


How multi processing is achieved in JAVA?

5 Answers   BMC,


What does a method signature consist of?

0 Answers  


Categories