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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

State the difference between strings and arrays.

835


When does a class need a virtual destructor?

732


What is difference between public static and void?

681


What is the byte range?

781


What is the formula to calculate percentage?

738


Explain jdk, jre and jvm?

755


Can we have multiple catch block for a try block?

768


Which category the java thread do fall in?

743


In which language java is written?

725


What is return in java?

728


What is hash table in java?

662


Discuss 2D arrays.

791


Is it safe to install java on my computer?

758


How many types of string data types are there?

738


Which is better stringbuilder or stringbuffer?

698