Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating string objects

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found this question in whizlab simulator:
(I do not remember the right question but I believe it was something like this)

How many String objects were created?
Please explain me that line by line. Thanks!
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These type of questions are asked so many times here, just try searching a bit you wil find so many threads. All the String literals (if it was not met before in the code), concatenation,toUpperCase() etc.. will create a new object but toString() will not.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why the correct answer is:3 ?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks - almost everything was discussed here
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type of question creates a lot of confusion for people who refer to K&B. It misses the statement that string literals are created when the class is loaded. Well that's how its 3.....seems tricky. and toString doesn't create a new object because that is already there in the pool.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to leave a deliberately cryptic response:

If you're not sure of your sources, you might end up spending a lot of time studying topics that you don't need to study.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry...but its just to know stuff that's all
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert Bates wrote:I'm going to leave a deliberately cryptic response:

If you're not sure of your sources, you might end up spending a lot of time studying topics that you don't need to study.


Every question is worth studying. Even if it won't appear on SCJP
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course there's lots of great stuff to study that's not on the exam - but this is the SCJP forum

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic