Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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 )

Posts: 8853
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?

shining_dragon
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

Posts: 8853
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.

olaimonas
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:

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));
}
}``````
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...

Posts: 8853
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);

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

### Re: About Question enthuware.ocajp.i.v7.2.1188 :

Now, division operation on long gives you a double. So the return type should be double.
Why is the return type double?

I checked the book and this is what it writes:
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.
It is not explained if one operand is double and the other is long.

Posts: 8853
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.

flex567
Posts: 201
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.

Posts: 8853
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.

Sieusc
Posts: 16
Joined: Mon Mar 02, 2020 3:38 am
Contact:

### Re: About Question enthuware.ocajp.i.v7.2.1188 :

Thu May 09, 2019 3:45 am
It is about the range not the bit size.
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?