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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Would you please explain "implicit narrowing" with some example?
tnx

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

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

Post by admin »

It is very simple really. You are trying to put a bigger data type into smaller one. For example,
int i = 10;
short s = i; //this will not work because int is bigger than short.

short s = (short) i; //this will work because you are explicitly narrowing the int to short.

But if you had: final int i = 10;
short s = i; //this will work because i is now a compile time constant. Explicit cast is not required. But this is still narrowing conversion, albeit implicit.

Please see http://docs.oracle.com/javase/specs/jls ... #jls-5.1.3 for more details.

There are several rules about this which will be too much for this post. It is better if you read the above mentioned section completely.

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

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Thank you. you explained very well. I had never seen "implicit narrowing" in my books.

girish_v
Posts: 7
Joined: Tue Apr 21, 2015 5:26 am
Contact:

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

Post by girish_v »

Yes, and the the important point is, it has to be compile time constant.

This works:
final int i =10;
short s = i;

But this doesnt work
final int i;
i =10;
short s = i;//gives compile error

actifefex
Posts: 2
Joined: Sun Jun 12, 2016 7:45 pm
Contact:

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

Post by actifefex »

How is it narrowing when Char.MAX_VALUE = 65535 and Short.MAX_VALUE = 32767?

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

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

Post by admin »

actifefex wrote:How is it narrowing when Char.MAX_VALUE = 65535 and Short.MAX_VALUE = 32767?
That is why it is narrowing. You are narrowing down an out of range value to within the range of a data type of the variable.
As per the link I posted above:
22 specific conversions on primitive types are called the narrowing primitive conversions:

short to byte or char

char to byte or short
....
Paul.
If you like our products and services, please help us by posting your review here.

actifefex
Posts: 2
Joined: Sun Jun 12, 2016 7:45 pm
Contact:

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

Post by actifefex »

I disagree at least with the explanation you gave in another answer. In another explanation you say something around the lines of "Think of it like having 2 buckets with bucket A being smaller than bucket B. Bucket A can be placed in bucket B because it is smaller. However bucket B needs to guarantee that he has an amount that A can hold (casting).

So short which is smaller than int, can be put into an int variable without casting because it is smaller.
However short which is smaller than char cannot be put into a char without casting. That statement is conflicting and confusing.

In other words, the bucket story doesn't hold unless I'm missing something (and I believe that that something might be related to signed and unsigned):

short 32767 vs int 2147483647 => no casting
short 32767 vs char 65535 => needs casting

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

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

Post by admin »

actifefex wrote:I disagree at least with the explanation you gave in another answer. In another explanation you say something around the lines of "Think of it like having 2 buckets with bucket A being smaller than bucket B. Bucket A can be placed in bucket B because it is smaller. However bucket B needs to guarantee that he has an amount that A can hold (casting).

So short which is smaller than int, can be put into an int variable without casting because it is smaller.
However short which is smaller than char cannot be put into a char without casting. That statement is conflicting and confusing.
It is not a technical explanation. It is just an aid to visualize the concept. You can't assign a short to char or char to short without an explicit cast (unless there is implicit narrowing due to final) because they are have different ranges. If the variable side is smaller than the value side (in terms of capacity or range), you can't do the assignment without a cast.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

i am confused:
This will not compile because a short VARIABLE can NEVER be assigned to a char without explicit casting.
VS
Implicit narrowing occurs only for byte, char, short, and int.
Is this the example of implicit narrowing?

Code: Select all

        byte b = 100;
        short sh = 200;
        char ch = 300;
        int i = 400;

        i=b;
        sh=b;
And is this explicit cast?

Code: Select all

        byte b = 100;
        short sh = 200;
        char ch = 300;
        int i = 400;
       
        ch =(char) b;

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

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

Post by admin »

The following are examples of implicit narrowing (a value of larger type is being assigned to a variable of smaller type) -
byte b = 100;
short sh = 200;
char ch = 300;
int i = 400;

The following are not -
i=b;
sh=b;

The following are examples of an explicit cast -
ch =(char) b;
b = (byte) sh;

The statement given in the explanation about short variable being assigned to a char type is also correct. A short CONSTANT can be assigned to a char without a cast only if the value fits into a char.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

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

Post by Rinkesh »

A short CONSTANT can be assigned to a char without a cast only if the value fits into a char.

But short's max value is 32767 and char's is 65535.So,It should fit right!?

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

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

Post by admin »

Not if the short has a negative value.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

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

Post by Rinkesh »

But in this case, short has a positive value(32767) so why it can't be assigned without a cast?

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

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

Post by admin »

Yes, if the value fits, it can be assigned without a cast. But for the compiler to know that the value fits, it has to be a constant. Compiler does not execute the code, so if the value is not a constant, the compiler cannot take that value into account.
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 21 guests