Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test
{
public static void main(String[] args)
{
final int i=500;
short s=i;
byte b=(byte)s;
final long l=877l;
int j=(int)l;
//float f=l;
System.out.println("l=:"+l+" j=:"+j);
System.out.println("b=:"+b+" i=:"+i);
}
}

the output of the above program is
l=877
j=877
b=-12
i=500

now my question is if int daytype can be narrowed to short, why cant short be narrowed to byte automatically...as per my view i belive its becouse the value is too wide to fit in the byte range(byte range is -128 to 127) but later in the programe when long is assigned to int why it is not automatically narrowed...
can someone please explain me how b=-12..
 
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you assign an int to short or int to byte or short to byte, then you are actually narrowing the datatype. In that case you have to use a cast. If you are assigning a final int to a short or byte and it is in range, then the cast is not needed. Look at these examples

final int i1 = 300;
final int i2 = 10;
final int i3 = 5000000;
short s1 = i1; //ok
byte b1 = i1; //error, the value of i1 is not in range of byte
short s2 = i2; //ok
byte b2 = i2; //ok
short s3 = i3; //error, the value of i3 is not in range of short

final short ss = 10;
final short sss = 300;
byte bb = ss; //ok
byte bbb = ssss; //error, the value of sss is not in range of byte

but if you try to assign a non final int to a short or byte, then you will have to use a cast. You cannot avoid the cast. There are a few things here. The final constant must be a compile time constant for implicit narrowing to work.

final int i;
i = 10;
short s = i; //error, i is not a compile time constant so cast is required

Also if you use a compound assignment operator, cast is not needed

int i = 2555555;
short s = 10;
s+= i; //no cast needed
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final variable are considered compile time constant by compiler, that's why compiler does not need to cast if value is small enough to fit in the variable type:

final int i=1000;
short s=i; //ok

final short s2=127;
byte b1=s2;//ok

final short s3=128;
byte b2=s3; //not ok as byte range is -128 to 127 only
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

final long L1=120;
int i1=L1;//error



Anybody has idea why this is giving compile time error? Are these type of conversions limited to int.
[ December 24, 2008: Message edited by: punit singh ]
 
Ankit Garg
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by punit singh:
Are these type of conversion limited to int.



return true;
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!