Solved

Core Java. What output will be and why ?

Posted on 2016-10-09
6
65 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 24

Expert Comment

by:Pawan Kumar
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41836479
(char)

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

Assisted Solution

by:Pawan Kumar
Pawan Kumar 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 53
diffSum example 4 21
javap bin 2 26
How to increase Spring boot/Tomcat max file upload size 2 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

919 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

19 Experts available now in Live!

Get 1:1 Help Now