The Short
class in Java is a wrapper class for the primitive data type short
. It provides several utility methods to work with short
values. This class is part of the java.lang
package.
Key Features of Short Wrapper Class
- Immutability: Instances of the
Short
class are immutable. - Static Methods: The
Short
class provides static methods for various operations, such as parsing and comparing shorts. - Short Conversion: Methods to convert
short
values to other types and vice versa.
Short Class Methods
Here are some commonly used methods in the Short
class:
short shortValue()
int compareTo(Short anotherShort)
static Short decode(String nm)
boolean equals(Object obj)
float floatValue()
int hashCode()
int intValue()
long longValue()
static short parseShort(String s)
static short parseShort(String s, int radix)
byte byteValue()
double doubleValue()
String toString()
static String toString(short s)
static Short valueOf(short s)
static Short valueOf(String s)
static Short valueOf(String s, int radix)
1. short shortValue()
Returns the value of this Short
object as a short
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
short value = s.shortValue();
System.out.println(value); // Output: 100
}
}
Output:
100
2. int compareTo(Short anotherShort)
Compares two Short
objects numerically.
public class ShortExample {
public static void main(String[] args) {
Short s1 = 100;
Short s2 = 200;
System.out.println(s1.compareTo(s2)); // Output: -1
}
}
Output:
-100
3. static Short decode(String nm)
Decodes a String
into a Short
.
public class ShortExample {
public static void main(String[] args) {
Short s = Short.decode("100");
System.out.println(s); // Output: 100
}
}
Output:
100
4. boolean equals(Object obj)
Compares this object to the specified object.
public class ShortExample {
public static void main(String[] args) {
Short s1 = 100;
Short s2 = 100;
System.out.println(s1.equals(s2)); // Output: true
}
}
Output:
true
5. float floatValue()
Returns the value of this Short
object as a float
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
float value = s.floatValue();
System.out.println(value); // Output: 100.0
}
}
Output:
100.0
6. int hashCode()
Returns a hash code for this Short
object.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
System.out.println(s.hashCode()); // Output: 100
}
}
Output:
100
7. int intValue()
Returns the value of this Short
object as an int
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
int value = s.intValue();
System.out.println(value); // Output: 100
}
}
Output:
100
8. long longValue()
Returns the value of this Short
object as a long
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
long value = s.longValue();
System.out.println(value); // Output: 100
}
}
Output:
100
9. static short parseShort(String s)
Parses the string argument as a signed decimal short
.
public class ShortExample {
public static void main(String[] args) {
short s = Short.parseShort("100");
System.out.println(s); // Output: 100
}
}
Output:
100
10. static short parseShort(String s, int radix)
Parses the string argument as a signed short
in the specified radix.
public class ShortExample {
public static void main(String[] args) {
short s = Short.parseShort("64", 16);
System.out.println(s); // Output: 100
}
}
Output:
100
11. byte byteValue()
Returns the value of this Short
object as a byte
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
byte value = s.byteValue();
System.out.println(value); // Output: 100
}
}
Output:
100
12. double doubleValue()
Returns the value of this Short
object as a double
.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
double value = s.doubleValue();
System.out.println(value); // Output: 100.0
}
}
Output:
100.0
13. String toString()
Returns a String
object representing this Short
's value.
public class ShortExample {
public static void main(String[] args) {
Short s = 100;
System.out.println(s.toString()); // Output: 100
}
}
Output:
100
14. static String toString(short s)
Returns a String
object representing the specified short
.
public class ShortExample {
public static void main(String[] args) {
short s = 100;
System.out.println(Short.toString(s)); // Output: 100
}
}
Output:
100
15. static Short valueOf(short s)
Returns a Short
instance representing the specified short
value.
public class ShortExample {
public static void main(String[] args) {
Short s = Short.valueOf((short) 100);
System.out.println(s); // Output: 100
}
}
Output:
100
16. static Short valueOf(String s)
Returns a Short
object holding the value given by the specified String
.
public class ShortExample {
public static void main(String[] args) {
Short s = Short.valueOf("100");
System.out.println(s); // Output: 100
}
}
Output:
100
17. static Short valueOf(String s, int radix)
Returns a Short
object holding the value extracted from the specified String
when parsed with the radix given by the second argument.
public class ShortExample {
public static void main(String[] args) {
Short s = Short.valueOf("64", 16);
System.out.println(s); // Output: 100
}
}
Output:
100
Conclusion
The Short
class in Java provides a wide array of methods for manipulating and converting short values. Understanding and utilizing these methods can greatly enhance your ability to handle short operations in your applications.
Comments
Post a Comment
Leave Comment