Java Numbers – Number Methods with Syntax and Examples
When we hear “number” we start thinking about mathematical numbers (1,2,3, etc) but Java numbers are different. It is an abstract class which is located in java.lang package. Or in general, we can say Java number class is a superclass of classes, which contains a single constructor number().
Today, you will learn about Java numbers and different types of methods used in it with examples.
What is Java Number?
In Java language, most of the times we use primitive data type, to perform any kind of arithmetic operation. The reason behind this is these data types come in handy while creating usual programs. Furthermore, the Java platform includes the boolean, character, and void, which together with the number classes are known as the type-wrapper classes. Java also provides a wrapper class under the abstract class numbers in java.lang package, there are six subclasses under the class ‘numbers’.
The primitive data types are ‘wrapped’ under these Java classes for their corresponding objects. This wrapping is usually done by the compiler. When an object is converted into primitive type than it is called Autoboxing, and then again transferred to an object it is called Unboxing.
Wait, let’s check what is Autoboxing and Unboxing in Java
Example of Java Number
package JavaNumbers; public class TestNumber { public static void main(String args[]) { Integer operand = 5; // boxes int datatype to an Integer object operand = operand + 10; // unboxes the Integer to a int System.out.println("Value of the operand is "+operand); } }
Number Methods in Java
There are various types of Java number methods, let’s discuss them one by one:
1. xxx xxxValue() method
It represents the primitive datatypes byte, short, int, long, float, double. We can use this datatype to convert the Java number types into the mentioned types.
Recommended Reading – Methods in Java
Syntax-
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Example of xxx xxxValue() method
//Java program to demonstrate xxxValue() method package JavaNumbers; public class xxxValueMethod { public static void main(String[] args) { // Creating a Double Class object with value "12.8451" Double doubleVariable = new Double("12.8451"); // Converting this Double(Number) object to different primitive data types byte byteVariable = doubleVariable.byteValue(); short shortVariable = doubleVariable.shortValue(); int integerVariable = doubleVariable.intValue(); long longVariable = doubleVariable.longValue(); float floatVariable = doubleVariable.floatValue(); double doubleVariableObject = doubleVariable.doubleValue(); System.out.println("Value of doubleVariable after converting it to byte : " + byteVariable); System.out.println("Value of doubleVariable after converting it to short : " + shortVariable); System.out.println("Value of doubleVariable after converting it to int : " + integerVariable); System.out.println("Value of doubleVariable after converting it to long : " + longVariable); System.out.println("Value of doubleVariable after converting it to float : " + floatVariable); System.out.println("Value of doubleVariable after converting it to double : " + doubleVariableObject); } }
2. int compareTo() method
This method is used to compare the specified argument and the number object, but two different types cannot be compared, so both the argument and the number should be of the same type.
The reference could of the type byte, double, float, long, or short.
Syntax-
public int compareTo( NumberSubClass referenceName )
Example of int compareTo
//Java program to demonstrate compareTo() method package JavaNumbers; public class compareToMethod { public static void main(String[] args) { // creating an Integer Class object with value "10" Integer integerObject = 10; //initializing integer variables int comparisonValue1=8,comparisonValue2=10,comparisonValue3=11; // comparing value of integerObject System.out.println(integerObject.compareTo(comparisonValue1)); System.out.println(integerObject.compareTo(comparisonValue2)); System.out.println(integerObject.compareTo(comparisonValue3)); } }
3. boolean equals() method
This method can determine whether the number object is equal to the argument.
Do you know What is Command Line Argument in Java?
Syntax-
public boolean equals(Object obj)
Example of boolean equals()
//Java program to demonstrate equals() method package JavaNumbers; public class booleanEqualsMethod { public static void main(String[] args) { // creating a Short Class object with value "12" Short shortClassObject1 = 12; // creating a Short Class object with value "5" Short shortClassObject2 = 5; // creating an Integer Class object with value "12" Integer integerClassObject1 = 12; // creating another Short Class object with value "12" Short shortClassObject3 = 12; //comparing shortClassObject with other objects System.out.println(shortClassObject1.equals(shortClassObject2)); System.out.println(shortClassObject1.equals(integerClassObject1)); System.out.println(shortClassObject1.equals(shortClassObject3)); } }
Output-
Don’t forget to check what is wrapper class in Java.
4. int parseInt() method
The radix in this number method used to return decimal, octal, or hexadecimal representation, etc. type as output, this method used for getting the primitive datatypes.
Syntax
int parseInt(String stringVariable, int radix)
Example of int parseInt(String s, int radix)
//Java program to demonstrate Integer.parseInt() method package JavaNumbers; public class parseIntRadixMethod { public static void main(String[] args) { // parsing different strings int intVariable1 = Integer.parseInt("123",8); //string to octal conversion int intVariable2 = Integer.parseInt("-DC", 16); //string to hexadecimal conversion long longVariable = Long.parseLong("8615341221",10); //string to decimal conversion System.out.println(intVariable1); System.out.println(intVariable2); System.out.println(longVariable); // run-time NumberFormatException will occur here "DataFlair" is not a parsable string int intVariable3 = Integer.parseInt("DataFlair",8); // run-time NumberFormatException will occur here(for octal(8),allowed digits are [0-7]) int intVariable4 = Integer.parseInt("99",8); } }
5. int parseInt(String stringVariable)
int parseInt use for getting primitive data types, the only difference between the above parseInt method is that the radix uses decimal as default.
Let’s examine your knowledge with Best Java Quiz Questions
Syntax
int parseInt(String stringVariable)
Example of int parseInt (String stringVariable)
package JavaNumbers; //Java program to demonstrate Integer.parseInt() method public class parseIntStringMethod { public static void main(String[] args) { // parsing different strings int integerVariable = Integer.parseInt("655"); long longVariable = Long.parseLong("2123211234"); System.out.println(integerVariable); System.out.println(longVariable); // run-time NumberFormatException will occur here "DataFlair" is not a parsable string int x = Integer.parseInt("DataFlair"); // run-time NumberFormatException will occur here (for decimal(10),allowed digits are [0-9]) int a = Integer.parseInt("-DF"); } }
6. String toString() method
This method is used to get the Java String representation of any number, there are three variants in this method-
- toBinaryString(int i)
- toHexString(int i)
- toOctalString(int i)
Syntax
String toString() String toString(int integerObject)
Example of String toString()
//Java program to demonstrate Integer.toString() and Integer.toString(int i) method package JavaNumbers; public class stringToStringMethod { public static void main(String[] args) { // demonstrating toString() method // creating integer object Integer integerObject = 24; System.out.println(integerObject.toString()); // demonstrating toString(int integerObject) method System.out.println(Integer.toString(24));//integer to string conversion System.out.println(Integer.toBinaryString(215));//integer to binary string conversion System.out.println(Integer.toHexString(215));//integer to hex string conversion System.out.println(Integer.toOctalString(215));//integer to octal string conversion } }
7. Integer valueOf() method
This method returns the integer value of a primitive data type.
Syntax
Integer valueOf(int i) Integer valueOf(String stringValue) Integer valueOf(String stringValue, int radix)
Example of Integer valueOf()
// Java program to demonstrate valueOf() method package JavaNumbers; public class integerValueOfMethod { public static void main(String[] args) { // demonstrating valueOf(int integerObject) method System.out.println("DataFlair: Demonstrating valueOf(int integerObject) method"); Integer integerObject =Integer.valueOf(50); Double doubleObject = Double.valueOf(9.36); System.out.println(integerObject); System.out.println(doubleObject); // demonstrating valueOf(String stringValue) method System.out.println("DataFlair: Demonstrating valueOf(String stringValue) method"); Integer variable1 = Integer.valueOf("888"); Integer variable2 = Integer.valueOf("-953"); System.out.println(variable1); System.out.println(variable2); // demonstrating valueOf(String stringValue,int radix) method System.out.println("DataFlair: Demonstrating (String stringValue,int radix) method"); Integer variable3 = Integer.valueOf("333",8); Long longVariable = Long.valueOf("51688245",16); System.out.println(variable3); System.out.println(longVariable); } }
Summary
Hence, in this tutorial for Java Number, we learned about the different types of numbers available for Java in its library, examples of Java number and various methods to declare with syntax and examples. This will help us understand the more complex programs better and code better.
It’s the right time to know about Design pattern in Java
Still have a query? Feel free to share with us!