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

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

Moderator: admin

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

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

Post 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 )

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

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

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

Post 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.

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

olaimonas
Posts: 2
Joined: Sat Apr 22, 2017 11:18 am
Contact:

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

Post 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...

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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);
If you like our products and services, please help us by posting your review here.

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

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

Post 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.

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post by flex567 »

Is double the larger one? I thought both have 64 bits.

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It is about the range not the bit size.
If you like our products and services, please help us by posting your review here.

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

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

Post 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

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests