ShowTutorials.com

Refreshing your ideas and broadening your visions


First Java Program

Data is an important part of program. All processing in an program is done upon data. That is, when program is running, it should hold some data temporarily in memory. This data is stored in variables. So, Variables are meant for storing data.
Variables will have some Data type, and that is the type of data that the program will store.
There are some pre-defined types available in Java. When we are declaring any variable to store data, we have to choose the correct Data type that we need.

Below image shows the list of Primitive Datatypes provided by Java

java data types

Primitive Data Types:

Primitive data types are categorized as:

  • Integral: To store numeric value without any decimal point
    Under Integral we have datatypes as byte, short, int and long, which are differentiated by their sizes
  • Floating Point: To store numeric value with decimal point.
    Under Floating Point we have float and double, also differentiated by sizes
  • Char: To store character values. Here, there is char data type to store characters such as ‘a’, ‘b’…
  • Boolean: To store Boolean value (true/false). Here, we have boolean data type to store true/false value.

 

Data type Size, Range and Default values:

java data ranges

byte: byte takes just 1 byte of memory and ranges from -128 to 127. Its default value is 0.
short: short takes 2 bytes of memory and ranges from -32768 to 32767. Its default value is 0.
int: int takes 4 bytes of memory and ranges from -2147483648 to 2147483647. Its default value is 0.
long: long takes 8 bytes of memory and ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is 0.
float: float takes 4 bytes of memory and stores fractional numbers up to 7 decimal digits. Its default value is 0.0f
double: double takes 8 bytes of memory and stores fractional numbers up to 16 decimal digits. Its default value is 0.0d
char: char takes 2 bytes of memory and stores single character/letter or ASCII values. Its default value is \u0000
boolean: boolean takes 1 bit of memory and store true or false values. Its default value is false.

How to check data type range and size:

Java provides Wrapper classes for the above primitive types. We can use these wrapper classes to check the range, size, etc
The Wrapper classes are as follows:

  1. Byte
  2. Short
  3. Integer
  4. Long
  5. Float
  6. Double
  7. Character
  8. Boolean

We can check the class members and methods of these classes using javap command or check the Java source code.

Program to check the data type range and size:

public class CheckDataType {
    public static void main(String[] args) {
        System.out.println("int min range: " + Integer.MIN_VALUE);
        System.out.println("int max range: " + Integer.MAX_VALUE);
        System.out.println("int size: " + Integer.BYTES);
        System.out.println("");

        System.out.println("float min range: " + Float.MIN_VALUE);
        System.out.println("float max range: " + Float.MAX_VALUE);
        System.out.println("float size: " + Float.BYTES);
        System.out.println("");

        System.out.println("double min range: " + Double.MIN_VALUE);
        System.out.println("double max range: " + Double.MAX_VALUE);
        System.out.println("double size: " + Double.BYTES);

        
    }
}

Output:

Java data type range check program

We can use the above variables to check the range and size of all primitive Java types, except Boolean. As Boolean have constants for True/False.


Latest Topics: