Create Short object with its constructor
Short(short value)
- Creates a newly allocated Short object that represents the specified short value.
Short(String s)
- Creates a newly allocated Short object that represents the short value indicated by the String parameter.
public class Main {
public static void main(String[] args) {
Short shortObject = new Short("1000");
System.out.println(shortObject);
short s = 101;
Short shortObject2 = new Short(s);
System.out.println(shortObject2);
}
}
The output:
1000
101
You cannot pass integer literal to the constructor directly as Java think it is an int not a short value.
public class Main {
public static void main(String[] args) {
Short shortObject2 = new Short(101);
System.out.println(shortObject2);
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Short(int) is undefined
at Main.main(Main.java:3)
Home
Java Book
Essential Classes
Java Book
Essential Classes
Short:
- Short class
- Find out the min value, max value and size of Short types
- Create Short object with its constructor
- Convert Short to byte, double, float, int, long and short
- Decode a string to short value
- Convert string to a short value
- Reverse the bytes in a short
- Convert short value to string
- Compare two short values