About Question enthuware.ocajp.i.v7.2.1188 :
Moderator: admin
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1188 :
how come (long) by/d*3;
casting comes last and arithmetic operation is done first?
because according to this java operator precedence table:
http://www.seas.upenn.edu/~palsetia/jav ... Table.html
type cast (which is in row 3 on the table) has a higher associativity than arithmetic operations.
So I thought it should be (long)( (by/d)*3 )
casting comes last and arithmetic operation is done first?
because according to this java operator precedence table:
http://www.seas.upenn.edu/~palsetia/jav ... Table.html
type cast (which is in row 3 on the table) has a higher associativity than arithmetic operations.
So I thought it should be (long)( (by/d)*3 )
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
If you know that type cast has higher associativity than arithmetic operators, why do you think (by/d*3) will be evaluated first?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
sorry, I mistyped my initial analysis while answering the question.
I thought it was:
(((long) by)/d)*3; >>> by is casted to long first, followed by division by d then multiplication by 3
If we will follow the table on my previous link.
I thought it was:
(((long) by)/d)*3; >>> by is casted to long first, followed by division by d then multiplication by 3
If we will follow the table on my previous link.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
I am sorry but I am not sure what is the issue. Isn't that what is illustrated by the result? The result of the expression is a double, not long, precisely for the reason you mentioned.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Apr 22, 2017 11:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
Hi, I have written a class with this method:
While compiling I get the following error:
MethodReturnType.java:8: error: incompatible types: possible lossy conversion fr
om int to byte
System.out.println(mrt.methodX(8));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get
full output
1 error
So it looks that the method's signature is not valid...
Code: Select all
public class MethodReturnType {
public double methodX(byte by) {
double d = 10.0;
return (long) by / d*3;
}
public static void main(String[] args) {
MethodReturnType mrt = new MethodReturnType();
System.out.println(mrt.methodX(8));
}
}
MethodReturnType.java:8: error: incompatible types: possible lossy conversion fr
om int to byte
System.out.println(mrt.methodX(8));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get
full output
1 error
So it looks that the method's signature is not valid...
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
Method signature is valid. You are passing 8, which is an int to a method that accepts a byte. You should call it like this:
mrt.methodX( (byte)8);
mrt.methodX( (byte)8);
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.1188 :
Why is the return type double?Now, division operation on long gives you a double. So the return type should be double.
I checked the book and this is what it writes:
It is not explained if one operand is double and the other is long.2. Binary numeric promotion - Both the operands of a binary operator are promoted to an int but if any of the operands is larger than an int, the other operand is promoted to the same type as the type of the larger one. Thus, for example, if both the operands are of type byte,
then both are promoted to int but if any of the operands is a long, float, or double, the other one is also promoted to long, float, or double, respectively.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
It does explain it although not very clearly. Read this, "the other operand is promoted to the same type as the type of the larger one ". The smaller operand is promoted to the larger one. So, long will be promoted to double.
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.1188 :
Is double the larger one? I thought both have 64 bits.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
It is about the range not the bit size.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Mar 02, 2020 3:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
I'm struggling with this too. I have read a good book, and am informed about lots of topics but when talking about which variable is "bigger" and which has more range. I know it's not a matter of bits but a matter of range. Knowing that, it's still confusing in a way because appparently the range of floating numbers is a complicated matter. I have read the book by Hanumath by the way, but could you clear one doubt for me?
Say that a question like this comes up on the exam, but it is not a case of long vs double like in this question, but a matter of long vs float. Which would be the "bigger" one, float or long? In a matter of numeric promotion on long and float, which is largest?
Thanks in advance
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1188 :
Any mathematical computation involving a float will give you a float. (unless the other operand is a double, in which case, the result will be a double). So, if the operands are long and float, the result will be a float.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: pavvel and 116 guests