IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Promotion" Watch "Promotion" New topic
Author

Promotion

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
if byte b= -1
ie b=11111111
when it is promoted to int what is the rule?
will it become 00000000000000000000000011111111
or
11111111111111111111111111111111
- Thanks
robl
Greenhorn

Joined: Aug 26, 2000
Posts: 25
Originally posted by Doit:
if byte b= -1
ie b=11111111
when it is promoted to int what is the rule?
will it become 00000000000000000000000011111111
or
11111111111111111111111111111111
- Thanks

I believe a byte can be promoted to an int automatically (no cast needed)
The result will be -1 or 11111111111111111111111111111111
Promotion here should not change the value.
Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
what would have happened if the byte value had been
01100010
and has to be promoted to int??
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
Fact: Whenever you 'promote' something, no information is lost.
(Take care while converting int to float and long to double, because float and double by nature are less precise than int and long resp.)
Now the answers are easy.
if b is -1 ( 11111111 ) then i will also be -1 ( ie 111....)
if b is 01100010 then i will be
00000000 00000000 00000000 01100010
Excercise: what happens if you are trying to promote char c = 0xffff to int? ( Note that char is always positive ).
HTH,
Paul.

------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
[This message has been edited by Paul A (edited August 28, 2000).]


<A HREF="http://pages.about.com/jqplus" TARGET=_blank rel="nofollow">http://pages.about.com/jqplus</A> <BR>Get Certified, Guaranteed!
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
*** what happens if you are trying to promote char c = 0xffff to int?
00000000 00000000 11111111 11111111?


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
That's correct.
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
 
IntelliJ Java IDE
 
subject: Promotion
 
Threads others viewed
question
Puzzling question from Mughal and Rasmussen
A Poll, a Poll, a very simple Poll!
Interesting classpath question
Static Initializers.
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com