aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte and Char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte and Char " Watch "Byte and Char " New topic
Author

Byte and Char

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
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?


SCJP 5.0 77%
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
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 ]

SCJP 5.0 (100%)
Cherry Singhal
Greenhorn

Joined: Sep 05, 2006
Posts: 6
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

Joined: Feb 08, 2006
Posts: 88
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 ?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Because it is to big.

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

prints 195, too big for a byte.


Yours,
Bu.


all events occur in real time
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
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

Joined: Nov 07, 2006
Posts: 65
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

Joined: Nov 07, 2006
Posts: 65
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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Byte and Char