Solved

Core Java. What output will be and why ?

Posted on 2016-10-09
6
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 28

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41836479
(char)

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 53
check java version using powershell 13 306
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 78
servlet and mdb, jms error 1 57
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

726 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