About Question enthuware.ocajp.i.v7.2.1240 :
Moderator: admin
-
- 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: 10053
- 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: 10053
- 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: 10053
- 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: 10053
- 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: 10053
- 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: 10053
- 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: Google [Bot] and 44 guests