Showing posts with label Number Class. Show all posts
Showing posts with label Number Class. Show all posts

Number class Methods in Java

java.lang.Number

The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and short.


Some methods of Number class


byteValue()It returns the value of the specified number as a byte. 


doubleValue()It returns the value of the specified number as a double.


equals()It indicates whether some other object is "equal to" this one.


floatValue()It returns the value of the specified number as a float.


hashCode()It returns a hash code value for the object.


intValue()It returns the value of the specified number as an int.


longValue()It returns the value of the specified number as a long.


shortValue()It returns the value of the specified number as a short. 


toString(): It returns a string representation of the object.



Number toString() in Java

toString(): This method is available in java.lang.Object class of Java.

Syntax:

String java.lang.Object.toString()

This method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

Parameters: NA

Returns: a string representation of the object.

Exceptions: NA

Approach

Java

public class NumbertoString {
    public static void main(String[] args) {

        Number number = 1234;
        System.out.println(number.toString());
    }
}

Output:

1234

Number shortValue() in Java

shortValue(): This method is available in java.lang.Number class of Java.

Syntax:

short java.lang.Number.shortValue()

This method returns the value of the specified number as a short. This implementation returns the result of int Value cast to a short.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type short.

Exceptions: NA

Approach

Java

public class NumbershortValue {
    public static void main(String[] args) {

        Number number = 19;
        System.out.println(number.shortValue());
    }
}

Output:

19

Number longValue() in Java

longValue(): This method is available in java.lang.Number class of Java.

Syntax:

long java.lang.Number.longValue()

This method returns the value of the specified number as a long.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type long.

Exceptions: NA

Approach

Java

public class NumberlongValue {
    public static void main(String[] args) {

        Number number = 1234;
        System.out.println(number.longValue());
    }
}

Output:

1234

Number intValue() in Java

intValue(): This method is available in java.lang.Number of Java.

Syntax:

int java.lang.Number.intValue()

This method returns the value of the specified number as an int.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type int.

Exceptions: NA

Approach

Java

public class NumberintValue {
    public static void main(String[] args) {

        Number number = 123;
        System.out.println(number.intValue());
    }
}

Output:

123

Number hashCode() in Java

hashCode(): This method is available in java.lang.Object class of Java.

Syntax:

int java.lang.Object.hashCode()

This method returns a hash code value for the object.

Parameters: NA

Returns: a hash code value for this object.

Exceptions: NA

Approach

Java

public class NumberhashCode {
    public static void main(String[] args) {

        Number number = 123;

        System.out.println(number.hashCode());
    }
}

Output:

123

Number floatValue() in Java

floatValue(): This method is available in java.lang.Number class of Java.

Syntax:

float java.lang.Number.floatValue()

This method returns the value of the specified number as a float.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type float.

Exceptions: NA

For Example

Number number = 1234

number.floatValue() = > It returns 1234.0

Approach

Java

public class NumberfloatValue {
    public static void main(String[] args) {

        Number number = 1234;
        System.out.println(number.floatValue());
    }
}

Output:

1234.0

Number equals() in Java

equals(): This method is available in java.lang.Object class of Java.

Syntax:

boolean java.lang.Object.equals(Object obj)

This method takes one argument of type Object as its parameter. This method indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.

2. It is symmetric: for any non-null reference values x and y, x.equals(y)should return true if and only if y.equals(x) returns true.

3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

5. For any non-null reference value x, x.equals(null) should return false.

Parameters: obj the reference object with which to compare.

Returns: true if this object is the same as the obj argument; false otherwise.

Approach

Java

public class Numberequals {
    public static void main(String[] args) {

        Number number = 1234;
        Object obj = 1234;
        System.out.println(number.equals(obj));
    }
}

Output:

true

Number doubleValue() in Java

doubleValue(): This method is available in java.lang.Number class of Java.

Syntax:

double java.lang.Number.doubleValue()

This method returns the value of the specified number as a double.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type double.

Exceptions: NA

For Example:

Number number = 1234

number.doubleValue() = > It returns 1234.0

Approach

Java

public class NumberdoubleValue {
    public static void main(String[] args) {

        Number number = 1234;

        System.out.println(number.doubleValue());
    }
}

Output:

1234.0

Number byteValue() in Java

byteValue(): This method is available in java.lang.Number class of Java.

Syntax:

byte java.lang.Number.byteValue()

This method returns the value of the specified number as a byte. This implementation returns the result of int Value cast to a byte.

Parameters: NA

Returns: the numeric value represented by this object after conversion to type byte.

Exceptions: NA

For Example:

Number number = 1234

number.byteValue() = > It returns 1234.

Approach

Java

public class NumberbyteValue {
    public static void main(String[] args) {

        Number number = 1234;
        System.out.println(number.byteValue());
    }
}

Output:

1234