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

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

Moderator: admin

Post Reply
AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

If I am multiplied : char * char result int?
byte * byte result int?

Is it true?

Thanks Paul

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

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:
===
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);
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...

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

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

Post by admin »

You are right. As per section 4.2.4 of JLS 8:
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.)
thank you!
Paul.
If you like our products and services, please help us by posting your review here.

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

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

Post by elias86 »

Why in:

s = b * b ;

b * b returns an int???

and in:

c = c + b;

c + b returns an int???

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

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

Post by admin »

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.

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

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

Post by elias86 »

admin wrote:Because that is how the Java language designers designed the mathematical operators to work.
Are there specifications where i can find all of the rules that indicates the implicit and explicit cast for primitive variables?

Thanks, Elias.

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

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

Post by admin »

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.

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

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

Post by flex567 »

From the explanation:
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:
What does it mean,
evaluated only once
identity conversion,
narrowing primitive conversion ?

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

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

Post by admin »

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

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

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

Post by javabean68 »

Hi,

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

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

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

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 44 guests