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
ETS User

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

Post by ETS User »

Hey Paul, 

byte b = 1;   char c = 1;   short s = 1;   int i = 1;
c = c + b ; //can't compile
it's not a compound assignment so c+b is an int and needs to be down-casted, right?

Secondly, on a side note-
why does
c = 1 + 1;
compile without casting? Doesn't adding two ints make an int?

Thirdly, on an even sidelier note-
I can't figure out which exception, array, arraylist, string/builder/buffer, number wrapper
methods I have to know. Do you have a list? It covers more than K&B 6, but I can't/don't want to
learn all of them. Please advise.

Thanks,
Karlie

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

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

Post by admin »

ETS User wrote:Hey Paul, 

byte b = 1;   char c = 1;   short s = 1;   int i = 1;
c = c + b ; //can't compile
it's not a compound assignment so c+b is an int and needs to be down-casted, right?
Yes.
Addition of integral variables (i.e. int, byte, char, short) returns an int. So c+b returns an int. But c is a char, so you need a cast. i.e. c = (char) c + b;
compound assignment is an exception because c++; actually gets translated to c = (int) c +1;
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.
Secondly, on a side note-
why does
c = 1 + 1;
compile without casting? Doesn't adding two ints make an int?
Because both the operands of + are compile time constants. cast is required only if you use variables. For example, if you have final int x = 0;
c = 1+x; will work without cast as well because x is a compile time constant.
Thirdly, on an even sidelier note-
I can't figure out which exception, array, arraylist, string/builder/buffer, number wrapper
methods I have to know. Do you have a list? It covers more than K&B 6, but I can't/don't want to
learn all of them. Please advise.
Yes, please see this post.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

CONSTANT values up to int can be assigned (without cast) to variables of lesser size
How come this doesn't hold for method calling?

Code: Select all

public class ParamBinding3 {
	
	static void myMethod(short s){}
	
	public static void main(String[] args) {
		myMethod(15);
	}	
}
[javac] error: method myMethod in class ParamBinding3 cannot be applied to given types;
required: short
found: int
reason: actual argument int cannot be converted to short by method invocation conversion

Why would 'method invocation conversion' differ from assignment?
Is it just a language design feature?

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

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

Post by admin »

It is indeed a language design thing. Automatic narrowing doesn't apply to method calls.
If you like our products and services, please help us by posting your review here.

Steffe
Posts: 5
Joined: Tue Dec 08, 2015 9:18 am
Contact:

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

Post by Steffe »

In the answer explanation for this question it says:
3. operands of mathematical operators are ALWAYS promoted to AT LEAST int. (i.e. for byte * byte both bytes will be first promoted to int.) and the return value will be AT LEAST int.
When would operands not be promoted to int and what else can they be implicitly promoted to?
I am thinking double is the only other possibility, which happens if the operands are float or double, correct?
I don't think integers can ever be implicitly promoted to long?

Thanks!

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

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

Post by admin »

Steffe wrote:In the answer explanation for this question it says:
3. operands of mathematical operators are ALWAYS promoted to AT LEAST int. (i.e. for byte * byte both bytes will be first promoted to int.) and the return value will be AT LEAST int.
When would operands not be promoted to int and what else can they be implicitly promoted to?
I am thinking double is the only other possibility, which happens if the operands are float or double, correct?
I don't think integers can ever be implicitly promoted to long?

Thanks!
Implicit promotion happens when there is a difference in the sizes of the operand (except when it happens because of the "at least an int" rule mentioned above). The smaller sized one is promoted to the type of the larger sized one. Thus, if one operand is a long and other an int, then the int value will be promoted to a long.

In the words of JLS:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

pink.freak
Posts: 3
Joined: Mon May 23, 2016 11:23 am
Contact:

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

Post by pink.freak »

1. Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char, or short) without explicit cast.

Can anyone give sample for this statement? I'm having a hard time comprehending it.

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

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

Post by admin »

What can be bigger than an int? float or double. So you can't assign a float or double to an int (or anything smaller than an int) without a cast. Example:
float f = 10.0f;
int i = f; //will not compile.
If you like our products and services, please help us by posting your review here.

pink.freak
Posts: 3
Joined: Mon May 23, 2016 11:23 am
Contact:

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

Post by pink.freak »

admin wrote:What can be bigger than an int? float or double. So you can't assign a float or double to an int (or anything smaller than an int) without a cast. Example:
float f = 10.0f;
int i = f; //will not compile.
Thanks for replying. How about the other way around? It says it can't assign something smaller than int without explicit cast.
However I can assign byte to int with no cast inside the compiler.

byte b = 127;

int i = b;

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

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

Post by admin »

What it says is you cannot assign anything bigger than an int to an int or to anything smaller than an int. e.g., you cannot assign a float to an int or to a char, short, or byte.
If you like our products and services, please help us by posting your review here.

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: 10046
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: 10046
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: 10046
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: 10046
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: 10046
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: 10046
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: No registered users and 51 guests