You are simply overflowing the maximum value of a short :
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
What happens when there is such overflow is equivalent to this algorithm :
/** Returns an integer which is equal to the short obtained by the ((short) n) conversion */
public static int int2short(int n) {
int sign = n > 0 ? 1 : -1;
int increment = sign * (Short.MAX_VALUE - Short.MIN_VALUE + 1);
for ( ; n > Short.MAX_VALUE || n < Short.MIN_VALUE ; n -= increment);
return n;
}
Answer from Dici on Stack OverflowVideos
You are simply overflowing the maximum value of a short :
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
What happens when there is such overflow is equivalent to this algorithm :
/** Returns an integer which is equal to the short obtained by the ((short) n) conversion */
public static int int2short(int n) {
int sign = n > 0 ? 1 : -1;
int increment = sign * (Short.MAX_VALUE - Short.MIN_VALUE + 1);
for ( ; n > Short.MAX_VALUE || n < Short.MIN_VALUE ; n -= increment);
return n;
}
Good question! It made me think about things I haven't thought about in a long while and I had to brush up on a couple of concepts. Thanks for helping me knock the rust off my brain.
For me this type of question is best visualized in binary (for reasons that will quickly become apparent):
Your original number (forgive the leading zeroes; I like groups of 4):
0001 1110 0010 0100 0000
A short, however, is a 16-bit signed two's complement integer according to the Java Language Specification (JLS) section 4.2. Assigning the integer value 123456 to a short is known as a "narrowing primitive conversion" which is covered in JLS 5.1.3. Specifically, 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."
Discarding all but the lowest 16 bits leaves us with:
1110 0010 0100 0000
In an unsigned integer this value is 57,290, however the short integer is a signed two's complement integer. The 1 in the leftmost digit indicates a negative number; to get the value of the number you must invert the bits and add 1:
Original:
1110 0010 0100 0000
Invert the bits:
0001 1101 1011 1111
Add 1:
0001 1101 1100 0000
Convert that to decimal and add the negative sign to get -7,616.
Thanks again for asking the question. It's okay to not know something so keep asking and learning. I had fun answering...I like diving into the JLS, crazy, I know!
Hello everyone, started learning how to code by myself a few weeks ago and I'm trying to understand something and can't find the answer nowhere on google. When I try to creat a HashMap with the short variable, why do I need to write "<Short>" before the number? It doesn't happen with Integer, String or Double (have not tested the other variables yet).
public class IDandPASS {
HashMap<String,Short> logininfo = new HashMap<String,Short>();
IDandPASS(){
**logininfo.put("OP",452); (WRONG)**
**logininfo.put("OP",<Short>452); (CORRECT)**The problem is with Math.pow(2, 63), as discussed in this question: Inconsistent behaviour of primitive integer types in Java
Math.pow returns an double, when the cast occurs maybe you can lose some information.
If you want that IF works, if( x >= -1 * (long) Math.pow(2,63) && x<=(long) Math.pow(2,63) -1 ) System.out.println("* long");
you need to add a bracket, like x<=(long) ( Math.pow(2,63)-1 )
if(x>=-1*(long)Math.pow(2,63) && x<=(long) ( Math.pow(2,63)-1 ) )System.out.println("* long");
There's actually a better way to solve the problem! It's actually suggested in the title of the Problem Statement itself: 'MIN_VALUE' & 'MAX_VALUE'
The wrapper classes: Byte, Short, Integer & Long all have the static fields 'MIN_VALUE' & 'MAX_VALUE' which describe the limits of the primitive data types that they wrap around.
Using these fields, we can re-write your solution in the following way:
Copytry {
long x=sc.nextLong();
System.out.println(x + " can be fitted in:");
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
System.out.println("* byte");
if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE)
System.out.println("* short");
if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE)
System.out.println("* int");
if(x >= Long.MIN_VALUE && x <= Long.MAX_VALUE)
System.out.println("* long");
}
catch(Exception e) {
System.out.println(sc.next()+" can't be fitted anywhere.");
}