• Register
First time here? Checkout the FAQ!
x
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system assignment writing service bandar bola best waikiki restaurants boiler business car car insurance quotes car insurance quotes car insurance quotes car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cosmetic dentist csr classics hack csr classics hack csr classics hack data data recovery maidenhead deals design development double glazing leeds electrician electrician manchester electricians emergency farm filmy bez limitu filmy online finlock finlock solutions general genral golf holidays spain graduate jobs in london graduate jobs london heap how to jump higher how to jump higher how to jump higher infrared sauna infrared sauna saunas inline skating java judi bola law law firm in leeds lease leasing led lights led bulbs leeds legal smoke life insurance quotes log london manchester mercedes lease deals mercedes leasing mezzanine movies museums and art nikogo szybka chwilówka none none none ny male revue outsource link building paid search agency performance car hire personal personal injury lawyer porcelain veneers ramię szybkie chwilówki recovery restaurant seo seo agencies seo company seo company london seo las vegas seo services services sky diamond seo solutions steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko temp cover car insurance temp cover car insurance temp cover car insurance thread tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web development company window repairs london windows zobaczyć szybka chwilówka

Difference between String s = "Marcus"; vs String s2 = new String("Marcus");

0 votes

Hi please tell me difference between
String s = "Marcus";
String s2 = new String("Marcus");
What happens in heap?

asked Nov 17, 2011 in Java/J2EE by anonymous
    

2 Answers

0 votes
Don't get confused between "String pools" and "HEAP" as String Object in Java is treated bit differently.

1. lets take a scenario where you would have used new String() post String literal created in a POOL:

String myString = "fun" ; // this shall create an entry in the String pools (seperate memory location other than heap). [OBJECT CREATED] 


and now if you do

String myString2 = "fun"; // this is mere a reference to the same String Object "fun" present in the Pool as created above [NO OBJECT CREATED]


and now if you do

String myString3 = new String("fun"); // this shall create a New Object in HEAP but not in POOL . [OBJECT CREATED] hence creating only single Object that is in HEAP 


2. Now lets take a scenario where you would have used new String() pre String literal created in a POOL

String myString = new String("fun"); // this shall create a New Object in HEAP but not in POOL . [OBJECT CREATED] hence creating only single Object that is in HEAP


and then if you do

String myString2 = foo ; // this shall create an Object in the POOL . [OBJECT CREATED]


3. Now let's use intern() function of String to make the String Objects created using new operator available in the POOL

String myString = new String("fun").intern() // this shall create a New Object in HEAP and also place it in the POOL . [OBJECT CREATED]


and then if you do

String myString2 = foo ; // this shall make a reference to "fun" object in the POOL as 'intern()' method has done the job above . [NO OBJECT CREATED]


I hope this shall clear your doubt ...

Cheers!!!
answered Nov 17, 2011 by javaboy Hot Users (380 points)
edited Nov 21, 2011 by javaboy
0 votes

1- String objects pool issue

String object is immutable; thats mean when you say

	String s = "Mohammed";
	s += "El-Adawi";
	

on runtime this means that a new String "Mohammed El-adawi" is created
and assigned to reference s
and String "Mohammed" now is eligable for gc

why sun have to do that ?, because String literals
(any thing between " " is reccognized by compiler as String literal)
are put by compiler in String pool, then if compilar found the same literal again for example
	String s = "abc";
	String m = "abc"; // compiler now found the same String literal
	

compiler will have one copy of "abc" and will assign both s,m to the same String literal, (at run time one object constructed and asigned to s,m)

if m change the String, s will point to the wrong thing, thats why String is immutable.

2- answer to your question

	String s = "Marcus"; // One String object is built and assigned to s.
	
	String s2 = new String("Marcus");

	
	/* now compiler found "Marcus" in the pool
	so it will not create a new object
	new operator creates new object contains
	Marcus at runtime
	*/
	
	
	String s3 = new String("Mohammed Not Marcus");
	// compiler adds "Mohammed Not Marcus" to String pool but with no
	// reference assignment, and String object created and assigned to S3 // at runtime
	

3- Another thing
	String s1 = "abc";
	String s2 = "abc";
	System.out.println(s1 == s2); // true
	

but
	String s1 = "abc";
	String s2 = new String ("abc");
	System.out.println(s1 == s2); // false
	

I hope that is helpfull

answered Nov 17, 2011 by sarada Hot Users (2,340 points)
edited Nov 22, 2011 by sarada

Related questions

0 votes
1 answer 95 views
0 votes
1 answer 231 views
0 votes
2 answers 109 views
0 votes
3 answers 217 views
...