• 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:

Byte and Char

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no error is porduced for following statement
byte b='a'/'b';

But if i say
b='a'+'b';

an error is produced.
found : int
required: byte
b='a'+'b';
^
1 error

Why is this difference happening. Even 1st statement must give a loss of precision?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one compiles, because, narrowing conversion can occur when you initialize a variable at the time of declaration. To be precise, this narrowing conversion will only occur if the expression is of type int & a compile-time constant expression & the type of variable is byte, short or char.
The second one fails, because, narrowing conversion is not available there. Remember, binary numeric promotion occurs when we use the %, /, *, + or - operators. This promotion converts both operands if they are smaller than int.
[ November 22, 2006: Message edited by: Abdul Rehman ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abdul please elaborate on your ans ...

i tried
byte b1='a'+'b'; (declaration & initialization at the same time)
but it still gives the compilation error 'can not convert from int to byte'

Also what do you mean by binary numeric promotion?
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abdul as you said

I tried doing this
byte b='a'+'b';

Now since i am intializing b at the time of declaration and it is a compile time constant expression and since all literals are of type char narrowing conversion should happen. But when i compile i get the same error


found : int
required: byte
byte b='a'+'b';
^
1 error

Why is it so ?


And moreover if i do this
short s;
s='a'+'b';
It compiles fine. Why is it so ?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is to big.

char ly = 'a'+'b';
System.out.println( (int) ly);

prints 195, too big for a byte.


Yours,
Bu.
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i got the solution.

when arithmetic operator inolves compile time constants, the arithmetic operation is done at compile time and the result of the operation is checked to see if it fits into the variable. If it doesnt an error is produced.
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bu(rkhard) for answering.

I wrote in my earlier post:-

"Narrowing conversion can occur when you initialize a variable at the time of declaration. To be precise, this narrowing conversion will only occur if the expression is of type int & a compile-time constant expression & the type of variable is byte, short or char."

I was wrong in my first post. Implicit narrowing conversion CAN occur even if the assignment does not occur at the time of declaration. I also forgot one condition which Bu has mentioned i.e. the compile-time constant expression must fit in the range of the datatype of the variable. If not, then narrowing conversion will not occur. Take a look at these examples; they will help you in understanding things.

final int x = 5;
final long y = 4L;
byte a = 5+4*3/2; // Compiles fine
byte b = x+4; // Compiles fine ... x is a "constant variable"
byte c = b*5; // Error! Not compile-time constant expression
byte d = 128; // Error! Not in range of byte
a = 2+3+x; // Compiles fine
short e = x + 32762; // Compiles fine ... within range
int f = y+5; // Error! No narrowing conv from long to int
float g = 5.0; // Error! No narrowing conv from double to float
float h = y; // Compiles fine ... implicit widening conv
double i = g * b; // Compiles fine ... implicit widening conv

Best regards,
Abdul Rehman.
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cherry Singhal:
Also what do you mean by binary numeric promotion?



BINARY NUMERIC PROMOTION
Binary numeric promotion implicitly applies appropriate widening primitive conversions so that a pair of operands have the broadest numeric type of the two, but which is always at least int. Given T to be the broadest numeric type of the two operands, the operands are promoted as follows under binary numeric promotion:-

"If T is broader than int, both operands are converted to T, otherwise both operands are converted to int."

This means that byte, short, char are always converted to at least int by binary numeric promotion.

Binary numeric promotion applies to the following:-
  • Operands of arithmetic operators *, /, %, + and -
  • Operands of the relational operators <, <=, > and >=
  • Operands of the numeric equality operators == and !=
  • Operands of the integer bitwise operators &, ^ and |

  • For example,

    byte b = 2;
    int i = 5;
    int j = 6;
    b = ++b; // No binary numeric promotion ... Works fine
    b = b+i+j; // Error! int found, instead of byte {not constant exp}
    [ November 22, 2006: Message edited by: Abdul Rehman ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic