Solved

Core Java. What output will be and why ?

Posted on 2016-10-09
6
75 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 29

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 36

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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41836479
(char)

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

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

729 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