Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects are created?

 
Patricia Anderson
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String s = new String("crispy");
Q: How many objects are created?
/***********************************/
String s = new Sring ("creme");
String w = "creme";
Q: How many objects are created?
/***********************************/
StringBuffer s = new StringBuffer("CrispyCreme");
String s = "CrispyCreme";
Q: How many objects are created?
 
Ravi Mann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2, 2, 2
 
Gokhan Gultekin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same question has been discussed many times in this forum.
Ravi's anwser is correct..
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patricia

Two. A string literal object "crispy" created in the string pool and a String Object in regular memory containing the characters "crispy"

Two. A string literal "creme" created in the string pool. A String object in regular memory containing the characters "creme". 'w' will point to the existing string literal object which contains the same characters "creme"

Two. Same reason as above for "creme"
Whenever you see a string literal ie a string of characters in double quotes; a String literal will be created in the pool if one doesn't already exist.
The 'new' operator always creates a unique string object in regular memory.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Patricia Anderson
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee thanks everyone for your input. I felt i needed to ask simple straightforward questions -- after going through two mock exams.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic