digitization, transformation, binary

Class Long in Java

Class Long in Java

Long class wraps a value of the primitive type long in an immutable Object. An object of type long contains a single field whose type is long. This is useful when we need to treat a long value as an object.

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 long value for one of these arguments, but we can provide a reference to a Long object that encapsulates the long value.

This class also contains useful minimum and maximum constants and useful conversion methods. parseLong() and valueOf() convert a string to a long or to a Long, respectively. Each can take a radix argument to specify the base that the value is represented in.

toString() converts in the other direction and may also take a radix argument.

toBinaryString(),toOctalString(), and toHexString() convert a long to a string using base 2, base 8, and base 16. These methods treat the long as an unsigned value.

Other routines return the value of a Long as various primitive types, and finally, the getLong() methods return the long value of a named property or the value of the specified default.

The Class Long structure is given as:

public final class java.lang.Long extends java.lang.Number{
//Member Elements
public final static long MAX_VALUE;//MAX_VALUE=9223372036854775807L
public final static long MIN_VALUE;//MIN_VALUE=-9223372036854775808L
public static final Class TYPE;
//constrcutors
public Long(long value);
//constructs a newly created Long object and allocate memory space that represents the primitive long argument
public Long(String str); //Constructs a newly allocated Long object that represents the long value of type long represented by String str.The string is converted to a long value by valueOf() method.This method may throw NumberFormatException if the string does not contain a parsable number.
//Methods
public double doubleValue();
public boolean equals(Object obj);
public float floatValue();
public byte byteValue();
public short shortValue();
public static Long getLong(String value);
public static Long getLong(String value,Long value2);
public static Long getLong(String value,Long value2);
public int hashCode();
public int intValue();
public long LongValue();
public static long parseLong(String str)throws NumberFormatException;
public static long parseLong(String str,int radix)throws NumberFormatException;
public static String toBinaryString(long i);
public static String toHexString(long i);
public static String toOctalString(long i);
public string toString();
public static String toString(long i);
public static String toString(long i,int radix);
public static Long valueOf(String str)throws NumberFormatException;
public static Long valueOf(String str,int radix)throws NumberFormatException;
}
 

The details of the class structure are given as follows:

public static final long MIN_VALUE;

public static final long MIN_VALUE represents the largest value that can be represented by a long. MAX_VALUE = 0x7fffffffffffffffL

public static final long MAX_VALUE;

public static final long MAX_VALUE represents the smallest value that can be represented by a long. MIN_VALUE = 0x8000000000000000L

public final static Class TYPE;

public final static Class TYPE represents the Class object that represents the type long. It is always true that Long.TYPE == long.class.

public Long(long value);

public Long(long value) constructor creates a Long object with the specified long value.

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

public Long(String s);

public Long(String s) constructor Constructs a Long 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 Long object.

public static Long getLong(String nm);

public static Long getLong(String nm) method retrieves the value of the named system property and returns it as a Long object. The method obtains the value of the system property as a String using System.getProperty().

If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s parsed as a decimal integer.

This method returns the value of the system property as a Long object or a Long object with the value 0 if the named property does not exist or cannot be parsed.

Parameter
nm – The name of a system property.

public static Long getLong(String nm, long val);

public static Long getLong(String nm, long val) method retrieves the value of the named system property and returns it as a Long object. The method obtains the value of the system property as a String using System.getProperty().

If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s parsed as a decimal integer.

This method returns the value of the system property as a Long object or a Long object with the value val if the named property does not exist or cannot be parsed.

Parameter
nm – The name of a system property.
val – A default value for the property.

public static Long getLong(String nm, Long val);

public static Long getLong(String nm, Long val) method retrieves the value of the named system property and returns it as a Long object. The method obtains the value of the system property as a String using System.getProperty().

If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s parsed as a decimal integer.

This method returns the value of the system property as a Long object, or the Long object val if the named property does not exist or cannot be parsed.

Parameter
nm – The name of a system property.
val – A default value for the property.

public static long parseLong(String s);

public static long parseLong(String s) method returns the numeric value of the long 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 long represented by the String object.

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

public static long parseLong(String s, int radix);

public static long parseLong(String s, int radix) method returns the numeric value of the long 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.

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

Parameter
s – The String to be converted to a long value.
radix – The radix used in interpreting the characters in the String as digits. It 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 valid digits. If radix is in the range 11 through 36, characters in the ranges ‘A’ through ‘Z’ and ‘a’ through ‘z’ may be considered valid digits.

public static String toBinaryString(long value);

public static String toBinaryString(long value) method returns a String object that contains the representation of the given value as an unsigned binary number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative. In other words, if the given value is negative, the method adds 2^64 to it. Otherwise, the value is used as it is.

The string returned by this method contains a sequence of one or more ‘0’ and ‘1’  characters. The method returns “0” if its argument is 0. Otherwise, the string returned by this method begins with ‘1’.

This method returns a string that contains the binary representation of the given value.

Parameter
value – The long value to be converted to a string.

public static String toHexString(long value);

public static String toHexString(long value) method returns a String object that contains the representation of the given value as an unsigned hexadecimal number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative. In other words, if the given value is negative, the method adds 2^64 to it. Otherwise, the
value is used as it is.

The string returned by this method contains a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, and ‘f’. The method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with ‘0’.

To produce a string that contains upper- instead of lowercase letters, use the String.toUpperCase() method.

This method returns a string that contains the hexadecimal representation of the given value.

Parameter
value – The long value to be converted to a string.

public static String toOctalString(long value);

public static String toOctalString(long value) method returns a String object that contains a representation of the given value as an unsigned octal number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative. In other words, if the given value is negative, the method adds 2^64 to it. Otherwise, the value is used as it is.

The string returned by this method contains a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’,’6′, and ‘7’. The method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with ‘0’.

This method returns a string that contains the octal representation of the given value.

Parameter
value – The long value to be converted to a string.

public static String toString(long i);

public static String toString(long i) 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
i – The long value to be converted to a string.

public static String toString(long i, int radix);

public static String toString(long i, int radix) method returns a String object that contains the representation of the given value in the specified radix.

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 characters that represent the magnitude of the given value. The characters that can appear in the sequence are determined by the value of radix. If N is the value of radix, the first N characters on the following line can appear in the sequence:

0123456789abcdefghijklmnopqrstuvwxyz

The method does not verify that radix is in the proper range. If radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, the value 10 is used instead of the given value.

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 in the specified radix.

Parameter
i – The long value to be converted to a string.
radix – The radix used in converting the value to a string. This value must be in the range Character.MIN_RADIX to Character.MAX_RADIX.

public static Long valueOf(String s);

public static Long valueOf(String s) method constructs a Long 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 method throws a NumberFormatException.

This method returns the Long object constructed from the string.

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

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

public static Long valueOf(String s, int radix) method Constructs a Long 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.

The method does not verify that radix is in the proper range. If radix is less than
Character.MIN_RADIX or greater than Character.MAX_RADIX, the value 10 is used instead of the given value.

This method returns the Long object constructed from the string.

Parameter
s – The string to be made into a Long 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 byte byteValue();

public byte byteValue() method returns the value of this object as a byte. The high order bits of the value are discarded.

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

public double doubleValue();

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

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 Long 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. Rounding may occur.

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

public int hashCode();

public int hashCode() method returns a hashcode computed from the value of this object. More specifically, the result is the exclusive OR of the two halves of the long value represented by the object. If value is the value of the object, the method returns a result equivalent to the following expression:

(int)(value^(value>>>32))

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

public int intValue();

public int intValue() method returns the value of this object as an int. The high-order bits of the value are discarded.

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. The high-order bits of the value are discarded.

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 Long 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)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *