aspose file tools*
The moose likes Java in General and the fly likes Binary Numbers Promotion, Please Clarify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Binary Numbers Promotion, Please Clarify" Watch "Binary Numbers Promotion, Please Clarify" New topic
Author

Binary Numbers Promotion, Please Clarify

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Hi, in my study notes I have the following:
1) System.out.println(4 + '' + 2); prints 38. Binary numbers promotion rule. Java will promote each operand of a binary operator to it's widest type. Java promotes the character literal('') to it's Unicode value 32.
2) 'b'+ 63; compiles correctly, promoting char to int. 98 giving 161. All operands of type byte, char, short are promoted to at least int before mathematical operations are performed. If one of the operands is larger than an int, then the other is promoted to the same type.
My question is why is the char literal in note 1 ('')given an int value of 32(I suppose) and the char in note 2 ('b') promoted to int value 98
Thanks :confused
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
JAva uses unicode to represent its tokens internally. From the JLS section 3.1 'The first 128 characters of the Unicode character encoding are the ASCII characters.'
For a good reference of the ASCII characters and their values check out this page.
hope that helps


Dave
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
AhhHah! Thanks for the links. But Dave, how the heck is a fella supposed to know that? Experience I guess. Geez.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Mike Kelly:
...how the heck is a fella supposed to know that?
Learn C as your first real language
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Binary Numbers Promotion, Please Clarify