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

What is the differences between c++ and java? Explain

591


How arrays are stored in memory in java?

515


How many bits is size_t?

551


What is this () in java?

587


What is bifunction in java?

684






What is finally block?

576


What are 3 boolean operators?

544


Difference between method overloading and method overriding in java ?

582


What is the final method?

610


How to sort list of list in java?

673


how we can make a read-only class in java?

546


What is null mean in java?

635


What is volatile keyword in java

660


What is the main use of generics in java?

520


How many types of memory areas are allocated by jvm?

573