About Question enthuware.ocajp.i.v8.2.924 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

About Question enthuware.ocajp.i.v8.2.924 :

Post by jamesmccreary »

Hi Paul,

I had some some research on this but I could not determine if a string literal (i.e. without the

Code: Select all

new String("hello")
part can be autoboxed into a "String" object.

I thought this was the case, but whenever a literal is created, it is created from the string pool. If this is the case, then was indeed a string literal "autoboxed" into a String object (and then widened to type Object)?

Let me know if my question does not make sense, thank you!

Sincerely,
James

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.924 :

Post by admin »

The concept of auto-boxing applies to primitives (i.e. byte, char, int, long, float, double, and boolean). String is an Object. It is not a primitive. So it has nothing to do with boxing/auto-boxing at all. Whether you get a string from a String literal pool or create a new one using the new operator, you will always get a proper String object. So, "hello" and new String("hello") are both objects of String class.

HTH,
Paul.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.924 :

Post by jamesmccreary »

Oh I see, so the strings in the pool are objects themselves as well, just cached. That is the only difference. Thanks for clearing that up!

skexsi
Posts: 1
Joined: Thu Feb 07, 2019 6:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.924 :

Post by skexsi »

The thing that threw me off about this question is that no reference variable was declared to create the (new TestClass) object. Does the object not need to have a reference when created?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.924 :

Post by admin »

No, explicit reference is not required to create an object. But remember that if the object is not referred to from any active part of the code, it may be garbage collected.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests