About Question enthuware.ocajp.i.v7.2.1240 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1240 :
Hey Paul,
byte b = 1; char c = 1; short s = 1; int i = 1;
c = c + b ; //can't compile
it's not a compound assignment so c+b is an int and needs to be down-casted, right?
Secondly, on a side note-
why does
c = 1 + 1;
compile without casting? Doesn't adding two ints make an int?
Thirdly, on an even sidelier note-
I can't figure out which exception, array, arraylist, string/builder/buffer, number wrapper
methods I have to know. Do you have a list? It covers more than K&B 6, but I can't/don't want to
learn all of them. Please advise.
Thanks,
Karlie
byte b = 1; char c = 1; short s = 1; int i = 1;
c = c + b ; //can't compile
it's not a compound assignment so c+b is an int and needs to be down-casted, right?
Secondly, on a side note-
why does
c = 1 + 1;
compile without casting? Doesn't adding two ints make an int?
Thirdly, on an even sidelier note-
I can't figure out which exception, array, arraylist, string/builder/buffer, number wrapper
methods I have to know. Do you have a list? It covers more than K&B 6, but I can't/don't want to
learn all of them. Please advise.
Thanks,
Karlie
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Yes.ETS User wrote:Hey Paul,
byte b = 1; char c = 1; short s = 1; int i = 1;
c = c + b ; //can't compile
it's not a compound assignment so c+b is an int and needs to be down-casted, right?
Addition of integral variables (i.e. int, byte, char, short) returns an int. So c+b returns an int. But c is a char, so you need a cast. i.e. c = (char) c + b;
compound assignment is an exception because c++; actually gets translated to c = (int) c +1;
FYI,
If any variable is a long, then the result is a long.
If any variable is a float or a double, then the result is a double.
Because both the operands of + are compile time constants. cast is required only if you use variables. For example, if you have final int x = 0;Secondly, on a side note-
why does
c = 1 + 1;
compile without casting? Doesn't adding two ints make an int?
c = 1+x; will work without cast as well because x is a compile time constant.
Yes, please see this post.Thirdly, on an even sidelier note-
I can't figure out which exception, array, arraylist, string/builder/buffer, number wrapper
methods I have to know. Do you have a list? It covers more than K&B 6, but I can't/don't want to
learn all of them. Please advise.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
How come this doesn't hold for method calling?CONSTANT values up to int can be assigned (without cast) to variables of lesser size
Code: Select all
public class ParamBinding3 {
static void myMethod(short s){}
public static void main(String[] args) {
myMethod(15);
}
}
required: short
found: int
reason: actual argument int cannot be converted to short by method invocation conversion
Why would 'method invocation conversion' differ from assignment?
Is it just a language design feature?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
It is indeed a language design thing. Automatic narrowing doesn't apply to method calls.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue Dec 08, 2015 9:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
In the answer explanation for this question it says:
I am thinking double is the only other possibility, which happens if the operands are float or double, correct?
I don't think integers can ever be implicitly promoted to long?
Thanks!
When would operands not be promoted to int and what else can they be implicitly promoted to?3. operands of mathematical operators are ALWAYS promoted to AT LEAST int. (i.e. for byte * byte both bytes will be first promoted to int.) and the return value will be AT LEAST int.
I am thinking double is the only other possibility, which happens if the operands are float or double, correct?
I don't think integers can ever be implicitly promoted to long?
Thanks!
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Implicit promotion happens when there is a difference in the sizes of the operand (except when it happens because of the "at least an int" rule mentioned above). The smaller sized one is promoted to the type of the larger sized one. Thus, if one operand is a long and other an int, then the int value will be promoted to a long.Steffe wrote:In the answer explanation for this question it says:When would operands not be promoted to int and what else can they be implicitly promoted to?3. operands of mathematical operators are ALWAYS promoted to AT LEAST int. (i.e. for byte * byte both bytes will be first promoted to int.) and the return value will be AT LEAST int.
I am thinking double is the only other possibility, which happens if the operands are float or double, correct?
I don't think integers can ever be implicitly promoted to long?
Thanks!
In the words of JLS:
HTH,If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon May 23, 2016 11:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
1. Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char, or short) without explicit cast.
Can anyone give sample for this statement? I'm having a hard time comprehending it.
Can anyone give sample for this statement? I'm having a hard time comprehending it.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
What can be bigger than an int? float or double. So you can't assign a float or double to an int (or anything smaller than an int) without a cast. Example:
float f = 10.0f;
int i = f; //will not compile.
float f = 10.0f;
int i = f; //will not compile.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon May 23, 2016 11:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Thanks for replying. How about the other way around? It says it can't assign something smaller than int without explicit cast.admin wrote:What can be bigger than an int? float or double. So you can't assign a float or double to an int (or anything smaller than an int) without a cast. Example:
float f = 10.0f;
int i = f; //will not compile.
However I can assign byte to int with no cast inside the compiler.
byte b = 127;
int i = b;
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
What it says is you cannot assign anything bigger than an int to an int or to anything smaller than an int. e.g., you cannot assign a float to an int or to a char, short, or byte.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
If I am multiplied : char * char result int?
byte * byte result int?
Is it true?
Thanks Paul
byte * byte result int?
Is it true?
Thanks Paul
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Hi, Paul,
First of all, I want to thank you for taking such good care of the board. I found your explanations excellent.
However, I found the following statement from a long time ago:
===
I found today (accidentally), that it's not exactly accurate. Maybe it was 7 years ago. But if any variable is a float, and the rest are not doubles, the result will be a float, not a double.
This fails to compile, but the compiler gives the following error:
"required reference, found float". So clearly, it gets evaluated to a float, not a double.
If you change type of f to a double, the compile changes the complaint to "required reference, found double".
So it will evaluate to double only if double is present. If only float is present, it will evaluate to float.
I might be missing something, though...
First of all, I want to thank you for taking such good care of the board. I found your explanations excellent.
However, I found the following statement from a long time ago:
===
===FYI,
If any variable is a long, then the result is a long.
If any variable is a float or a double, then the result is a double.
I found today (accidentally), that it's not exactly accurate. Maybe it was 7 years ago. But if any variable is a float, and the rest are not doubles, the result will be a float, not a double.
Code: Select all
float f = 10.0f;
int x = 10;
System.out.println( (f*x) instanceof float);
"required reference, found float". So clearly, it gets evaluated to a float, not a double.
If you change type of f to a double, the compile changes the complaint to "required reference, found double".
So it will evaluate to double only if double is present. If only float is present, it will evaluate to float.
I might be missing something, though...
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
You are right. As per section 4.2.4 of JLS 8:
Paul.
thank you!If at least one of the operands to a numerical operator is of type double, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type double. If the other operand is not a double, it is first widened (§5.1.5) to type double by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit floating-point arithmetic, and the result of the numerical operator is a value of type float. (If the other operand is not a float, it is first widened to type float by numeric promotion.)
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Fri May 04, 2018 4:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Why in:
s = b * b ;
b * b returns an int???
and in:
c = c + b;
c + b returns an int???
s = b * b ;
b * b returns an int???
and in:
c = c + b;
c + b returns an int???
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Because that is how the Java language designers designed the mathematical operators to work.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Fri May 04, 2018 4:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Are there specifications where i can find all of the rules that indicates the implicit and explicit cast for primitive variables?admin wrote:Because that is how the Java language designers designed the mathematical operators to work.
Thanks, Elias.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Yes, of course. You can find all the rules here: https://docs.oracle.com/javase/specs/jl ... index.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
From the explanation:
evaluated only once
identity conversion,
narrowing primitive conversion ?
What does it mean,A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion or a narrowing primitive conversion. For example, the following code is correct:
evaluated only once
identity conversion,
narrowing primitive conversion ?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
1. Example: ia[0] += 1; is same as ia[0] = ia[0] + 1; but in the second case, the exact array element needs to figure out twice - once for evaluating the RHS and once for assigning that value to the LHS.
2. Identity conversion
3. narrowing primitive conversion
2. Identity conversion
3. narrowing primitive conversion
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Hi,
I don't understand what the sentence in bold means:
Can you please explain it to me perhaps by means of an example?
Thanks
Regards
Fabio
I don't understand what the sentence in bold means:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion or a narrowing primitive conversion.
Can you please explain it to me perhaps by means of an example?
Thanks
Regards
Fabio
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1240 :
Sorry, I am not sure what is the need for the bold part.
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 51 guests