Solved

Core Java. What output will be and why ?

Posted on 2016-10-09
6
58 Views
Last Modified: 2016-10-24
 int x = (int) (char) (byte) -1;
        System.out.println(x);

Open in new window


Could you explain step by step what happen here and why ? thanks you in advance !
0
Comment
Question by:SunnyX
6 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41835947
Conversion one after the other.... I haven't this kind of code before.. :)

-1 --> Byte (1) --> Char (Some Weird Character) -- > Int --- > 65535.

-1 --> Byte -- > -1
-1 --> Char --> 'ï¿¿'
'ï¿¿' --> Int - > 65535
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41836001
byte in Java is signed 8-bit twos' complement - so the -1 as a byte is eight 1s , ie. 11111111.

the char and int casts are meaningless, so you end up with an int (which in Java is 32-bit signed two's complement, giving the positive decimal result of 65535.

Is that what you meant to ask?
0
 
LVL 35

Accepted Solution

by:
mccarl earned 400 total points
ID: 41836260
To expand on what Pawan posted and to clear up some inaccuracies in Krakatoa's post..

Firstly, yes, the (int) cast is redundant as Java will apply this when storing the value into the int x. However the (char) cast is VERY meaningful, and changes the result significantly.

So you need to know the Java primitive data types (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)...

byte is a signed 8-bit number
char is an unsigned 16-bit number (used to store and represent unicode characters)
int is a signed 32-bit number

So the -1 is stored (as krakatoa said) in the byte as binary 11111111. When you cast to the char type, Java needs to expand those 8 bits to 16 bits, and it performs a sign-extension which means that it copies the leftmost bit (1) to the new 8 bits, so the char now has the value of 11111111 11111111. Finally the cast from char to int expands the value from 16 to 32 bits, and since char is unsigned Java just does a 0 expansion setting the new 16 bits to 0's, giving you the value 00000000 00000000 11111111 11111111 which is 65535 as a decimal.

Had you NOT done the (char) cast in between, you are asking Java to cast directly from byte to int, which in this case means casting a signed 8-bit to a signed 32-bit value. And because you are only dealing in signed values Java uses the sign-extension method to add the extra 24 bits, so from 11111111 Java takes the leftmost bit (1) and copies it to all 24 of the new bits, giving you 11111111 11111111 11111111 11111111 or a value of -1 as an int.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:krakatoa
ID: 41836479
(char)

quite right - I overlooked the char cast as material like that.
0
 
LVL 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 100 total points
ID: 41856675
@Author - A feedback will be highly appreciated !
0
 

Author Closing Comment

by:SunnyX
ID: 41856798
Thx everybody !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 58
topping2 challenge 13 63
Modeling a class in java 5 36
Java Jpanels and Jframe 8 21
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now