About Question enthuware.ocajp.i.v7.2.1164 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1164 :

Post by ETS User »

Hello,

Does anyone know why
String s = 63 + new Integer(10); will lead to a compile error?

The explanation is: Since none of '+' the operands is a String, the + operator will not generate a String. However, due to auto-unboxing, it will generate an int value of 73.

Thank you!

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

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by admin »

Since the variable s is of type String, you need a String on the right hand side of the assignment. Now, on the right hand side, you have 63 + new Integer(10) , which does not generate a String. It is an int, which cannot be assigned to s. So the compiler will complain.

For + to generate a String, at least one of the operands must be a String. So, had it been something like 63+"10", the resulting value would have been "6310", a String, which can be assigned to s.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by Guest »

Thank you

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by ElizabethCM »

Hi Paul,

I have a question regarding the expression:
System.out.println('b'+new Integer(63));
Normally, this will evaluate to 98 + 63 = 161

Why is this accepted as none of the components are strings?
Is it because it is inside of a "println" statement?

Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by admin »

No, it is accepted because both the sides of the operator are numeric, which makes is the basic usecase for the + operator i.e. addition. Remember that char is also a numeric data type. Char is rot a String.
Also, remember that Integer will be unboxed into an int.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by ElizabethCM »

Hi,

Yes, you are right, the Integer is unboxed and then it is just like using an addition between two numeric values.
Thanks

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by Arold Aroldson »

I don't understand why 'b' transfers to 98. Can you provide me with any link where i could read about this topic?

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

Re: About Question enthuware.ocajp.i.v7.2.1164 :

Post by admin »

98 is the unicode (and ascii) value of character b. If you use a char as an int, you will get the unicode value of that char. You may read more about it here: https://coderanch.com/t/404152/java/con ... er-integer
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests