Page 1 of 1

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

Posted: Wed Mar 12, 2014 2:34 pm
by shining_dragon
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 )

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

Posted: Wed Mar 12, 2014 9:12 pm
by admin
If you know that type cast has higher associativity than arithmetic operators, why do you think (by/d*3) will be evaluated first?

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

Posted: Thu Mar 13, 2014 7:32 am
by shining_dragon
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.

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

Posted: Thu Mar 13, 2014 8:25 am
by admin
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.

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

Posted: Sat Apr 22, 2017 11:23 am
by olaimonas
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...

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

Posted: Sat Apr 22, 2017 9:00 pm
by admin
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);

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

Posted: Wed May 08, 2019 10:55 am
by flex567
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.

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

Posted: Wed May 08, 2019 11:40 am
by admin
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.

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

Posted: Thu May 09, 2019 2:25 am
by flex567
Is double the larger one? I thought both have 64 bits.

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

Posted: Thu May 09, 2019 3:45 am
by admin
It is about the range not the bit size.

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

Posted: Thu Jun 18, 2020 3:31 pm
by Sieusc
admin wrote:
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?

Thanks in advance

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

Posted: Fri Jun 19, 2020 9:06 am
by admin
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.

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

Posted: Fri Dec 20, 2024 9:12 am
by jorgeruiz
In the explanation, at the end, you have:
Had b been a float, the resulting value would have been a float.
But it should say:
Had d been a float, not b, correct?

This is the snippet from the question:

Code: Select all

public RETURNTYPE methodX( byte by){
    var d = 10.0;
    return (long) by/d*3;
}

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

Posted: Fri Dec 20, 2024 10:32 am
by admin
Yes, it should be d. I see that it is correctly mentioned in 1z0-829 and 1z0-830 question banks but not in 1z0-819 question. Should be fixed.
thank you for your feedback!