Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java provides a set of Number classes as part of the java.lang
package, which serve as the base classes for numeric data types such as Integer
, Double
, Float
, Long
, Short
, and Byte
. These classes are all subclasses of the abstract Number
class, which defines several methods for converting numeric values between different formats. In this tutorial, we will cover the basics of using Java Number classes, including some examples.
You can create instances of the Number classes using their constructors or using the valueOf()
static methods.
Example:
Integer i1 = new Integer(42); Integer i2 = Integer.valueOf(42); Double d1 = new Double(3.14); Double d2 = Double.valueOf(3.14);
The Number
class defines several methods for converting numeric values to different formats, such as intValue()
, doubleValue()
, floatValue()
, longValue()
, shortValue()
, and byteValue()
. These methods are available in all Number subclasses.
Example:
Double d = 3.14; int i = d.intValue(); float f = d.floatValue(); long l = d.longValue(); short s = d.shortValue(); byte b = d.byteValue();
The Number subclasses provide parseXXX()
and valueOf()
static methods for converting string representations of numbers into numeric values.
Example:
String intString = "42"; int i = Integer.parseInt(intString); Integer integer = Integer.valueOf(intString); String doubleString = "3.14"; double d = Double.parseDouble(doubleString); Double dbl = Double.valueOf(doubleString);
The Number subclasses provide toString()
methods for converting numeric values into string representations. Additionally, you can use String.format()
or java.text
package classes such as NumberFormat
, DecimalFormat
, and ChoiceFormat
for more advanced formatting.
Example:
int i = 42; String intString = Integer.toString(i); double d = 3.14; String doubleString = Double.toString(d); String formattedDouble = String.format("%.2f", d); // Output: "3.14"
Number classes implement the Comparable
interface, which provides the compareTo()
method for comparing numeric values. You can also use the equals()
method to check if two Number objects have the same value.
Example:
Integer i1 = 42; Integer i2 = 24; int comparisonResult = i1.compareTo(i2); // Output: 1 (i1 > i2) boolean isEqual = i1.equals(i2); // Output: false
This tutorial introduced the basics of using Java Number classes, including creating instances of Number classes, converting between numeric types, parsing strings into numeric values, formatting numeric values as strings, and comparing numeric values. These classes provide a convenient and flexible way to work with numeric data in Java, and understanding how to use them effectively will help you write cleaner and more efficient code.
Working with numbers in Java using Number class:
Number
class to work with numbers:Number num = 42.5; int intValue = num.intValue(); // Converts to int
Conversion between Number and primitive types in Java:
Number
and primitive types:Number num = 42.5; double doubleValue = num.doubleValue(); // Converts to double
Parsing and formatting numbers with Java Number class:
NumberFormat
:NumberFormat formatter = NumberFormat.getInstance(); Number parsedNumber = formatter.parse("123.45");
Java NumberFormatException and Number class:
NumberFormatException
can occur when parsing strings to numbers if the string is not a valid numeric representation.try { int result = Integer.parseInt("abc"); // Throws NumberFormatException } catch (NumberFormatException e) { // Handle the exception }
Use of Number class in Java collections:
Number
and its subclasses can be used in collections to store numeric values as objects:List<Number> numbersList = new ArrayList<>(); numbersList.add(42); numbersList.add(3.14);
Java Number class examples:
Number
class:Number num = 10.75; System.out.println(num.intValue()); // Outputs 10