About Question com.enthuware.ets.scjp.v6.2.417 :
Moderator: admin
-
- Posts: 5
- Joined: Wed Nov 21, 2012 12:31 pm
- Contact:
About Question com.enthuware.ets.scjp.v6.2.417 :
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 !
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 !
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
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.
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.
-
- Posts: 5
- Joined: Wed Nov 21, 2012 12:31 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
Wow, I did not expect a reply today.
Its simple enough. An int cannot be assigned to a String.
Thanks !
Its simple enough. An int cannot be assigned to a String.
Thanks !
-
- Posts: 27
- Joined: Mon Sep 22, 2014 1:30 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
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?
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?
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Thu Aug 24, 2023 4:33 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
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.
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.
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.417 :
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.
Who is online
Users browsing this forum: No registered users and 2 guests