About Question enthuware.ocajp.i.v7.2.1164 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1164 :
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!
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!
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
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.
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.
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
Hi Paul,
I have a question regarding the expression:
Why is this accepted as none of the components are strings?
Is it because it is inside of a "println" statement?
Thanks
I have a question regarding the expression:
Normally, this will evaluate to 98 + 63 = 161System.out.println('b'+new Integer(63));
Why is this accepted as none of the components are strings?
Is it because it is inside of a "println" statement?
Thanks
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
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.
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.
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
Hi,
Yes, you are right, the Integer is unboxed and then it is just like using an addition between two numeric values.
Thanks
Yes, you are right, the Integer is unboxed and then it is just like using an addition between two numeric values.
Thanks
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
I don't understand why 'b' transfers to 98. Can you provide me with any link where i could read about this topic?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1164 :
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.
Who is online
Users browsing this forum: No registered users and 116 guests