Spread the love

Class Short in Java

Spread the love

Class Short in Java

This class provides an object wrapper around the short primitive type. It defines useful constants for the minimum and maximum values that can be stored by the short type, and also a Class object constant that represents the short type.

For example, there are a number of utility methods that take a reference to an Object as one of their arguments. we cannot specify a short value for one of these arguments, but you can provide a reference to a Short object that encapsulates the byte value. Furthermore, the Short support the Reflection API and class literals.

It also provides various methods for converting Short values to and from strings and other numeric types. Most of the static methods of this class are used to convert a String to a Short object or a short value: the four parseShort() and valueOf() methods parse a number from the specified string, using an optionally specified radix, and return it in one of these two forms.

The decode() method parses a number specified in base 10, base 8, or base 16 and returns it as a Short. If the string begins with “0x” or “#”, it is interpreted as a hexadecimal number, or if it begins with “0”, it is interpreted as an octal number. Otherwise, it is interpreted as a decimal number.

The Short class also provides a number of utility methods for converting short values to other primitive types and for converting short values to strings and vice-versa.

Note that this class has two different toString() methods. One is static and converts a short primitive value to a String. The other is the usual toString() method that converts a Short object to a string. Most of the remaining methods convert a Short to various primitive numeric types.

The structure of the class Short is given by

public final class java.lang.Short extends java.lang.Number {
// Public Constructors
public Short(short value);
public Short(String s) throws NumberFormatException;
// Constants
public static final short MAX_VALUE;
public static final short MIN_VALUE;
public static final Class TYPE;
//constructor
public Short(short value);
public Short(String s);
// Class Methods
public static Short decode(String nm) throws NumberFormatException;
public static short parseShort(String s) throws NumberFormatException;
public static short parseShort(String s, int radix) throws NumberFormatException;
public static String toString(short s);
public static Short valueOf(String s, int radix) throws NumberFormatException;
public static Short valueOf(String s) throws NumberFormatException;
// Public Instance Methods
public byte byteValue(); // Overrides Number
public double doubleValue(); // Defines Number
public boolean equals(Object obj); // Overrides Object
public float floatValue(); // Defines Number
public int hashCode(); // Overrides Object
public int intValue(); // Defines Number
public long longValue(); // Defines Number
public short shortValue(); // Overrides Number
public String toString(); // Overrides Object
}

The details of the class structure are given as follows:

public static final short MAX_VALUE;

public static final short MAX_VALUE represents the largest value that can be represented by a short.MAX_VALUE= 32767

public static final short MIN_VALUE;

public static final short MIN_VALUE represents the smallest value that can be represented by a short. MIN_VALUE= -32768

public static final Class TYPE;

public static final Class TYPE represents the Class object that represents the primitive type short. It is always true that Short.TYPE == short.class.

public Short(short value);

public Short(short value) constructor creates a Short object with the specified short value.

READ  Class ObjectInputStream in Java
READ  Class ObjectInputStream in Java

Parameter
value – The short value to be encapsulated by this object.

public Short(String s);

public Short(String s) constructor constructs a Short object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single `-‘ character. If the string contains any other characters, the constructor throws a NumberFormatException

Parameter
s – The string to be made into a Short object.

public static Short decode(String nm);

public static Short decode(String nm) method returns a Short object that encapsulates the given value.

This method returns A Short object that encapsulates the given value.

Parameter
nm – A String representation of the value to be encapsulated by a Short object. If the string begins with # or 0x, it is a radix 16 representation of the value. If the string begins with 0, it is a radix 8 representation of the value. Otherwise, it is assumed to be a radix 10 representation of the value.

public static short parseShort(String s);

public static short parseShort(String s) method returns the numeric value of the short represented by the contents of the given String object.

The String must contain only decimal digits, except that the first character may be a minus sign.

This method returns the numeric value of the short represented by the String object.

Parameter
s – The String to be converted to a short value.

public static short parseShort(String s, int radix);

public static short parseShort(String s, int radix) method returns the numeric value of the short represented by the contents of the given String object in the specified radix. The String must contain only valid digits of the specified radix, except that the first character may be a minus sign. The digits are parsed in the specified radix to produce the numeric value.

READ  Class System in Java

This method returns the numeric value of the short represented by the String object in the specified radix.

Parameter
s – The String to be converted to a short value.
radix – The radix used in interpreting the characters in the String as digits. This value must be in the range of Character.MIN_RADIX to Character.MAX_RADIX. If radix is in the range 2 through 10, only characters for which the Character.isDigit() method returns true are considered to be valid digits. If radix is in the range 11 through 36, characters in the ranges ‘A’ through ‘Z’ and ‘a’ through ‘z’ are considered valid digits.

public static String toString(short s);

public static String toString(short s) method returns a String object that contains the decimal representation of the given value. This method returns a string that begins with `-‘ if the given value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’. This method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.

This method returns the string representation of the given value.

Parameter
s – The short value to be converted to a string.

public static Short valueOf(String s, int radix);

public static Short valueOf(String s, int radix) method Constructs a Short object with the value specified by the given string in the specified radix. The string should consist of one or more digit characters or characters in the range ‘A’ to ‘Z’ or ‘a’ to ‘z’ that are considered digits in the given radix. The digit characters can be preceded by a single `-‘ character. If the string contains any other characters, the method throws a NumberFormatException.

READ  Class Long in Java

This method returns the Short object constructed from the string.

Parameter
s – The string to be made into a Short object.
radix – The radix used in converting the string to a value. This value must be in the range
Character.MIN_RADIX to Character.MAX_RADIX.

public static Short valueOf(String s);

public static Short valueOf(String s) method constructs a Short object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single `-‘. If the string contains any other characters, the method throws a NumberFormatException.

READ  Set Concept Simplified In Java

This method returns the Short object constructed from the string.

Parameter
s – The string to be made into a Short object

public byte byteValue();

public byte byteValue() method returns the value of this object as a byte.

This method returns the value of this object as a byte. The high order bits of the value are discarded.

public double doubleValue();

public double doubleValue() method returns the value of this object as a double.

This method returns the value of this object as a double.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of Short and it contains the same value as the object this method is associated with.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public float floatValue();

public float floatValue() method returns the value of this object as a float.

This method returns the value of this object as a float.

public int hashCode();

public int hashCode() method returns a hash code computed from the value of this object.

This method returns a hashcode based on the short value of the object.

public int intValue();

public int intValue() method returns the value of this object as an int.

This method returns the value of this object as an int.

public long longValue();

public long longValue() method returns the value of this object as a long.

This method returns the value of this object as a long.

public short shortValue();

public short shortValue() method returns the value of this object as a short.

This method returns the value of this object as a short.

public String toString();

public String toString() method returns a String object that contains the decimal representation of the value of this object.

This method returns a string that begins with ‘-‘ if the value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’.

This method returns “0” if the value of the object is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.

This method returns the string representation of the value of this object.

Apart from these Short class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)
READ  How to Resolve NoSuchSessionException in selenium free?




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love