About Question com.enthuware.ets.scjp.v6.2.417 :

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

Moderator: admin

Post Reply
yabmob
Posts: 5
Joined: Wed Nov 21, 2012 12:31 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.417 :

Post by yabmob »

I am having a hard time understanding this.

System.out.println( 'a' + 63 ) ; //This works and prints 160

System.out.println( 'a' + 63 + " ") ; //This works and prints 160

String s = 'a' + 63 + "" ; //This works and prints 160

String s = 'a' + 63 ; //This fails to compile !

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

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by admin »

A char is an integral value just like an int, short, and byte. So 'a' + 63 means you are adding the integral value of 'a', which is 93 to 67. Hence, you get 160.

Same thing for 'a'+67+" " except the last part where you are basically doing 160+"". Now, + operator is overloaded such a way that if any of its operands is a String, it converts the other non-string operand to a string and joins the two. So 160 is converted to "160" and joined with " ", which prints 160.

Since "160" is a string, you can assign it to a String, so String s = 'a' + 63 + ""; works fine.

String s = 'a' + 63; doesn't work because none of the operands of + is a String here. So the result of this + operation is not a String but an int 160. You can't assign an int to a String.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

yabmob
Posts: 5
Joined: Wed Nov 21, 2012 12:31 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by yabmob »

Wow, I did not expect a reply today.

Its simple enough. An int cannot be assigned to a String.

Thanks !

piotrkmiotczyk
Posts: 27
Joined: Mon Sep 22, 2014 1:30 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by piotrkmiotczyk »

Hmmmm.. What do you mean by: + operator is overloaded?

I've read the docs on promotions, assign conversions, String conversions (by concatenation) this is neither of those cases. Is there some list of overloads (similar to a method) for operators? Can you provide a source please?

Also how does this relate to operator associativity, if at all?

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

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by admin »

Java doesn't support operator overloading except for +, which is overloaded for String concatenation. If one of the operands of + is a String the other is automatically converted to a String and then both are concatenated into another String. You may read more about it here: http://docs.oracle.com/javase/specs/jls ... ls-15.18.1

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Badem48
Posts: 29
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by Badem48 »

Hi,

I want to point out that in the last option Integer constructor is used however it has been depreciated since Java 9 and marked for removal.

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

Re: About Question com.enthuware.ets.scjp.v6.2.417 :

Post by admin »

You right and in the latest version of the question bank for OCP 17 (v. 1.48) all references to constructors have been santized (i.e. either removed or an explanation has been provided).
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

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