About Question enthuware.ocajp.i.v7.2.1179 :
Moderator: admin
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1179 :
Would you please explain "implicit narrowing" with some example?
tnx
tnx
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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.
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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Thank you. you explained very well. I had never seen "implicit narrowing" in my books.
-
- Posts: 7
- Joined: Tue Apr 21, 2015 5:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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
This works:
final int i =10;
short s = i;
But this doesnt work
final int i;
i =10;
short s = i;//gives compile error
-
- Posts: 2
- Joined: Sun Jun 12, 2016 7:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
How is it narrowing when Char.MAX_VALUE = 65535 and Short.MAX_VALUE = 32767?
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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.actifefex wrote:How is it narrowing when Char.MAX_VALUE = 65535 and Short.MAX_VALUE = 32767?
As per the link I posted above:
Paul.22 specific conversions on primitive types are called the narrowing primitive conversions:
short to byte or char
char to byte or short
....
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Jun 12, 2016 7:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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
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
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
i am confused:
And is this explicit cast?
VSThis will not compile because a short VARIABLE can NEVER be assigned to a char without explicit casting.
Is this the example of implicit narrowing?Implicit narrowing occurs only for byte, char, short, and int.
Code: Select all
byte b = 100;
short sh = 200;
char ch = 300;
int i = 400;
i=b;
sh=b;
Code: Select all
byte b = 100;
short sh = 200;
char ch = 300;
int i = 400;
ch =(char) b;
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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.
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.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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!?
But short's max value is 32767 and char's is 65535.So,It should fit right!?
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
Not if the short has a negative value.
If you like our products and services, please help us by posting your review here.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
But in this case, short has a positive value(32767) so why it can't be assigned without a cast?
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1179 :
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.
Who is online
Users browsing this forum: Google [Bot] and 21 guests