Avatar of SunnyX
SunnyX
 asked on

Core Java. What output will be and why ?

 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 !
JavaJava EE

Avatar of undefined
Last Comment
SunnyX

8/22/2022 - Mon
Pawan Kumar

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
krakatoa

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?
ASKER CERTIFIED SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
krakatoa

(char)

quite right - I overlooked the char cast as material like that.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SunnyX

ASKER
Thx everybody !