Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Core Java. What output will be and why ?

Posted on 2016-10-09
6
Medium Priority
?
88 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 32

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 17

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 36

Accepted Solution

by:
mccarl earned 1600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:krakatoa
ID: 41836479
(char)

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

Assisted Solution

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

Author Closing Comment

by:SunnyX
ID: 41856798
Thx everybody !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

610 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