• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

final byte

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test8{
public static void main(String a[]){
byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
}
}

The above code will not compile since at marker //1 it is trying to move a byte to char.
1. Not sure why it is not letting it even though byte is only 8 bits and char can hold upto 16 bits..

2. On the other hand if the byte declaration is changed to final byte =1 the code compiles...

Can anyone explain this..

Thanks
JP
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In //1, the compiler only knows that c is a char and b is a byte. The range of a char is 0 to 65535, and the range of a byte is -128 to 127. Values from -128 to -1 can't be represented with a char (buy they can with a byte). This is why the compiler complains.

Yet when you declare b to be final, the compiler knows that b will always be equivalent to the value 1 and that it will never change in all the program, thus it knows that it will always fit into a char variable.

Hope this clarifies.

...Ariel
[ October 12, 2005: Message edited by: Ariel Ortiz ]
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect it makes sense now. THANKS A BUNCH !!
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you do explicit cast to char, it compiles also fine, even when your byte is negative (a question mark is printed)



output:
? | -50 | -50
-50 | -50 | -50
[ October 13, 2005: Message edited by: Roel De Nijs ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JP Ravi:
public class Test8{
public static void main(String a[]){
byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
}
}

The above code will not compile since at marker //1 it is trying to move a byte to char.
1. Not sure why it is not letting it even though byte is only 8 bits and char can hold upto 16 bits..

2. On the other hand if the byte declaration is changed to final byte =1 the code compiles...

Can anyone explain this..

Thanks
JP

 
reply
    Bookmark Topic Watch Topic
  • New Topic