Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sat Feb 22, 2014 11:02 pm
by fasty23
Would you please explain "implicit narrowing" with some example?
tnx
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sat Feb 22, 2014 11:38 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sun Feb 23, 2014 11:22 am
by fasty23
Thank you. you explained very well. I had never seen "implicit narrowing" in my books.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Wed Apr 22, 2015 10:58 am
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
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sun Jun 12, 2016 7:48 pm
by actifefex
How is it narrowing when Char.MAX_VALUE = 65535 and Short.MAX_VALUE = 32767?
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sun Jun 12, 2016 9:01 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Mon Jun 13, 2016 12:13 am
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
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Mon Jun 13, 2016 12:23 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sun Oct 29, 2017 12:14 pm
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;
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sun Oct 29, 2017 8:29 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sat Dec 02, 2017 6:09 pm
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!?
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Sat Dec 02, 2017 7:25 pm
by admin
Not if the short has a negative value.
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Mon Dec 04, 2017 8:24 am
by Rinkesh
But in this case, short has a positive value(32767) so why it can't be assigned without a cast?
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Posted: Mon Dec 04, 2017 9:34 am
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.