Learn Java

Learn Java Numbers

Learn Java Numbers

Java provides several data types to represent different kinds of numbers, including integers, floating-point numbers, and more. In this answer, I will provide an overview of the most common numeric data types in Java.

Integers:

Java provides four integer data types: byte, short, int, and long. All of these data types represent whole numbers, but they differ in the range of values they can represent. Here are the details of each:

  • byte: A byte is an 8-bit signed integer that can store values between -128 and 127.
  • short: A short is a 16-bit signed integer that can store values between -32,768 and 32,767.
  • int: An int is a 32-bit signed integer that can store values between -2,147,483,648 and 2,147,483,647.
  • long: A long is a 64-bit signed integer that can store values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

 

Floating-point numbers:

Java provides two floating-point data types: float and double. Floating-point numbers are used to represent decimal values. Here are the details of each:

  • float: A float is a 32-bit IEEE 754 floating-point number, which can store values with a precision of 6-7 decimal digits.
  • double: A double is a 64-bit IEEE 754 floating-point number, which can store values with a precision of 15-16 decimal digits.

 

Other data types:

Java also provides other numeric data types, including char and boolean, which are not used to represent numeric values. Here are the details of each:

  • char: A char is a 16-bit Unicode character, which can be used to represent any character from the Unicode character set.
  • boolean: A boolean is a data type that can have only two values: true or false. It is often used in conditional statements to control the flow of a program.

 

In addition to these basic data types, Java also provides wrapper classes for each of these data types. These wrapper classes provide additional functionality and can be used in situations where an object is required instead of a primitive data type. 

For example, the Integer class provides methods for converting a String to an int value or an int value to a String.

 

 

Here are some code examples in Java to work with numbers:

Initializing and using integer variables:

public class first {


    public static void main(String[] args) {


        // Integer (whole number)
        int a = 190;


        int b = 260;


        int c = a + b;


        System.out.println("The sum of " + a + " and " + b + " is " + c);


    }
}

 

 

Output:

The sum of 190 and 260 is 450

learn java numbers on home4cloud

 

 

 

Initializing and using double variables:

public class first {


    public static void main(String[] args) {


        double x = 3.14;


        double y = 2.5;


        double z = x * y;


        System.out.println("The product of " + x + " and " + y + " is " + z);
    }
}

 

Output:

The product of 3.14 and 2.5 is 7.85

 

learn java numbers for double on home4cloud

 

Converting a string to an integer:

public class first {


    public static void main(String[] args) {


        String strNum = "123";


        int num = Integer.parseInt(strNum);


        System.out.println("The integer value of the string " + strNum + " is " + num);


    }
}

 

 

Output:

The integer value of the string 123 is 123

 

learn java numbers for string on home4cloud

 

 

Using the Math class to perform mathematical operations:

public class first {


    public static void main(String[] args) {


        double radius = 5.0;


        double circumference = 2 * Math.PI * radius;


        double area = Math.PI * radius * radius;


        System.out.println("The circumference of a circle with radius " + radius + " is " + circumference);


        System.out.println("The area of a circle with radius " + radius + " is " + area);


    }
}

 

Output

The circumference of a circle with radius 5.0 is 31.41592653589793

The area of a circle with radius 5.0 is 78.53981633974483

 

learn java numbers for math on home4cloud

 

These are just a few examples, but there are many more ways to work with numbers in Java.