Reputation: 472
I tried executing the below code :
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
short x = 0;
int i = 123456;
x = (short) (x + i);
System.out.println(x);
}
}
How is the output "-7616"? how does integer and short work when type casted to short?
Upvotes: 1
Views: 988
Reputation: 41230
Maximum value of short
datatype is 32,767. And here you have added 123456 with 0. 123456 is bigger than max value of short
, so it is truncated.
Upvotes: 0
Reputation: 382374
When you add an int and a short, the resut is an int, which is 4 bytes.
But when you cast it to a short, you're making a short from the last two bytes of the int.
123456
's bytes are 0x0 0x1 0xe2 0x40
, so you're making a short from 0xe2 0x40
.
The first bit of 0xe2
being a 1, the resulting short is negative.
You can play with this short program to see what happens :
int i = 123456; // <== change the value
System.out.format("Bytes of %d: ", i);
byte[] bytes = ByteBuffer.allocate(4).putInt(i).array();
for (byte b : bytes) {
System.out.format("0x%x ", b);
}
System.out.format("\nlast two bytes : %d*256 + %d = %d\n", bytes[2], bytes[3], (short)(bytes[2]*256 + bytes[3]));
Output :
Bytes of 123456: 0x0 0x1 0xe2 0x40
Last two bytes : -30*256 + 64 = -7616
Upvotes: 1
Reputation: 2151
Or look at this code, run it. It ought to be easier to figure it out with an example.
public class Test {
public static void main(String args[]) throws Exception {
short v = 32767;
System.out.println(v);
v += 1;
System.out.println(v);
v += 1;
System.out.println(v);
System.out.println();
v = 0;
for (int i = 0; i < 123; i++) {
int intVal = i * 1000;
v = (short) intVal;
System.out.println("intVal[" + intVal + "] shortVal[" + v + "]");
}
}
}
The output...
32767
-32768
-32767
intVal[0] shortVal[0]
intVal[1000] shortVal[1000]
intVal[2000] shortVal[2000]
intVal[3000] shortVal[3000]
intVal[4000] shortVal[4000]
intVal[5000] shortVal[5000]
intVal[6000] shortVal[6000]
intVal[7000] shortVal[7000]
intVal[8000] shortVal[8000]
intVal[9000] shortVal[9000]
intVal[10000] shortVal[10000]
intVal[11000] shortVal[11000]
intVal[12000] shortVal[12000]
intVal[13000] shortVal[13000]
intVal[14000] shortVal[14000]
intVal[15000] shortVal[15000]
intVal[16000] shortVal[16000]
intVal[17000] shortVal[17000]
intVal[18000] shortVal[18000]
intVal[19000] shortVal[19000]
intVal[20000] shortVal[20000]
intVal[21000] shortVal[21000]
intVal[22000] shortVal[22000]
intVal[23000] shortVal[23000]
intVal[24000] shortVal[24000]
intVal[25000] shortVal[25000]
intVal[26000] shortVal[26000]
intVal[27000] shortVal[27000]
intVal[28000] shortVal[28000]
intVal[29000] shortVal[29000]
intVal[30000] shortVal[30000]
intVal[31000] shortVal[31000]
intVal[32000] shortVal[32000]
intVal[33000] shortVal[-32536]
intVal[34000] shortVal[-31536]
intVal[35000] shortVal[-30536]
intVal[36000] shortVal[-29536]
intVal[37000] shortVal[-28536]
intVal[38000] shortVal[-27536]
intVal[39000] shortVal[-26536]
intVal[40000] shortVal[-25536]
intVal[41000] shortVal[-24536]
intVal[42000] shortVal[-23536]
intVal[43000] shortVal[-22536]
intVal[44000] shortVal[-21536]
intVal[45000] shortVal[-20536]
intVal[46000] shortVal[-19536]
intVal[47000] shortVal[-18536]
intVal[48000] shortVal[-17536]
intVal[49000] shortVal[-16536]
intVal[50000] shortVal[-15536]
intVal[51000] shortVal[-14536]
intVal[52000] shortVal[-13536]
intVal[53000] shortVal[-12536]
intVal[54000] shortVal[-11536]
intVal[55000] shortVal[-10536]
intVal[56000] shortVal[-9536]
intVal[57000] shortVal[-8536]
intVal[58000] shortVal[-7536]
intVal[59000] shortVal[-6536]
intVal[60000] shortVal[-5536]
intVal[61000] shortVal[-4536]
intVal[62000] shortVal[-3536]
intVal[63000] shortVal[-2536]
intVal[64000] shortVal[-1536]
intVal[65000] shortVal[-536]
intVal[66000] shortVal[464]
intVal[67000] shortVal[1464]
intVal[68000] shortVal[2464]
intVal[69000] shortVal[3464]
intVal[70000] shortVal[4464]
intVal[71000] shortVal[5464]
intVal[72000] shortVal[6464]
intVal[73000] shortVal[7464]
intVal[74000] shortVal[8464]
intVal[75000] shortVal[9464]
intVal[76000] shortVal[10464]
intVal[77000] shortVal[11464]
intVal[78000] shortVal[12464]
intVal[79000] shortVal[13464]
intVal[80000] shortVal[14464]
intVal[81000] shortVal[15464]
intVal[82000] shortVal[16464]
intVal[83000] shortVal[17464]
intVal[84000] shortVal[18464]
intVal[85000] shortVal[19464]
intVal[86000] shortVal[20464]
intVal[87000] shortVal[21464]
intVal[88000] shortVal[22464]
intVal[89000] shortVal[23464]
intVal[90000] shortVal[24464]
intVal[91000] shortVal[25464]
intVal[92000] shortVal[26464]
intVal[93000] shortVal[27464]
intVal[94000] shortVal[28464]
intVal[95000] shortVal[29464]
intVal[96000] shortVal[30464]
intVal[97000] shortVal[31464]
intVal[98000] shortVal[32464]
intVal[99000] shortVal[-32072]
intVal[100000] shortVal[-31072]
intVal[101000] shortVal[-30072]
intVal[102000] shortVal[-29072]
intVal[103000] shortVal[-28072]
intVal[104000] shortVal[-27072]
intVal[105000] shortVal[-26072]
intVal[106000] shortVal[-25072]
intVal[107000] shortVal[-24072]
intVal[108000] shortVal[-23072]
intVal[109000] shortVal[-22072]
intVal[110000] shortVal[-21072]
intVal[111000] shortVal[-20072]
intVal[112000] shortVal[-19072]
intVal[113000] shortVal[-18072]
intVal[114000] shortVal[-17072]
intVal[115000] shortVal[-16072]
intVal[116000] shortVal[-15072]
intVal[117000] shortVal[-14072]
intVal[118000] shortVal[-13072]
intVal[119000] shortVal[-12072]
intVal[120000] shortVal[-11072]
intVal[121000] shortVal[-10072]
intVal[122000] shortVal[-9072]
Upvotes: 0
Reputation: 12924
This is something called narrowing primitive conversions. You are trying to convert int
to short
.
It may be too technical but try to understand Java Language Specification, section 5.1.3.
Upvotes: 1
Reputation: 692043
The type of x + i
is int
. This int contains a value that is bigger than the max value of short. When you cast it to a short, the 16 last bits of the 32 bits int are taken to make the short, and these 16 last bits happen to represent a negative short value.
The Java Language Specification says:
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
Upvotes: 0
Reputation: 11298
Explicit casting. when you cast a Java int to a short, you will get silent truncation.
Upvotes: 0
Reputation: 2473
Long version: http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
Short version: Java takes the 16 least significant bits of the int, and puts them into the short in the same order.
Upvotes: 3
Reputation: 115378
short
is 2-byte integer. And java types cannot be "unsigned" as in C. So, max value is 32767. (Short.MAX_VALUE).
As you can see you tried to put bigger value to this variable:
123456
32767
The binary representation of such value is as negative value you posted. BTW the same happens in C.
Upvotes: 1