The primitive data types in Java are the basic data types that specify the size and type of standard values. There are eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. Java primitive data types serve as the foundation for all java data types. Primitive data types in Java are used to hold numeric values, strings, and boolean values.
In the Java programming language, primitive data types serve as the building block of non-primitive data types and data manipulation. These are the most basic data types in the Java programming language. Java is a computer language that is statically typed. This implies that all variables must be declared before they may be used. As a result, we must define the type and name of variables.
When a primitive data type is saved, the values are assigned to the stack. When a variable is copied, a duplicate of the variable is created, and changes made to the copied variable do not reflect changes made to the original variable.
Java Primitive Datatypes are the basic data types. Java support eight primitive data types such as byte, short, int, long, float, double, char, and boolean.
Java primitive data types
A data type is nothing but type of data. The broad classification of data are integer numbers, decimal numbers, text data, boolean data. Data types have predefined size in memory, each data limit is set based on those sizes. This memory is created while java program executes and destroyed at the end.
Below is the list of eight primitive data types in java
- byte data type
- short data type
- int data type
- long data type
- float data type
- double data type
- char data type
- boolean data type
Java primitive data type default values
In Java, primitive datatypes do not have a default value of null. When the primitive data type values are copied, two distinct variables are formed, each with a unique assignment. When modifications are made to the copied variables, the changes do not appear in the original content.
Sr. No | Data Type | Default Value |
1 | byte | 0 |
2 | short | 0 |
3 | int | 0 |
4 | long | 0 |
5 | float | 0.0f |
6 | double | 0.0d |
7 | char | \u0000 |
8 | boolean | false |
9 | String & Java Objects | null |
Java primitive data type size
The size of a primitive data type is always the same. It is platform independent. Bits and bytes are used to calculate size. The data types can be chosen based on the range of values to be used. We cannot change the data type of a variable once it has been declared as a variable of a certain primitive data type. However, they may be converted to other types using Java’s explicit type casting.
Here is a Java program that shows all of the primitive data types in Java as well as the sizes of each primitive data type.
package com.yawintutor;
public class JavaDataTypes {
public static void main(String[] args) {
// integer data types
System.out.println("Integer data types");
System.out.println("------------------");
System.out.println("Size of byte : " + (Byte.SIZE / 8) + " bytes.");
System.out.println("Size of short : " + (Short.SIZE / 8) + " bytes.");
System.out.println("Size of int : " + (Integer.SIZE / 8) + " bytes.");
System.out.println("Size of long : " + (Long.SIZE / 8) + " bytes.");
// decimal number data types
System.out.println("\nDecimal number data types");
System.out.println("---------------------------");
System.out.println("Size of float : " + (Float.SIZE / 8) + " bytes.");
System.out.println("Size of double : " + (Double.SIZE / 8) + " bytes.");
// text data types
System.out.println("\nCharacter data types");
System.out.println("----------------------");
System.out.println("Size of char : " + (Character.SIZE / 8) + " bytes.");
// boolean data types
System.out.println("\nBoolean data types");
System.out.println("----------------------");
System.out.println("Size of boolean : " + "1 bit.");
}
}
Output
The below output shows the size of primitive data types in java
Integer data types
------------------
Size of byte : 1 bytes.
Size of short : 2 bytes.
Size of int : 4 bytes.
Size of long : 8 bytes.
Decimal number data types
---------------------------
Size of float : 4 bytes.
Size of double : 8 bytes.
Character data types
----------------------
Size of char : 2 bytes.
Boolean data types
----------------------
Size of boolean : 1 bit.
Numeric Primitive Data Types
Number values such as integers, decimal numbers, negative numbers, and so on will be stored in the numeric primitive data type. Variables of the numeric primitive data type can be used in mathematical operations such as arithmetic, relational, and logical operations, among others.
byte, short, int, long, float, double
Character Primitive Data Types
The Character primitive data types are used to hold characters and strings. Sentences are built with the character primitive data types. String operations such as substring, concatenate, and so on may be used to manipulate the characters and strings. Character primitive data types are commonly used to store real-time data in a readable manner.
char, String
Boolean Primitive Data Types
Boolean data types hold true or false, yes or no, and other values. The boolean data type is used in decision making in Java.
boolean
1. int
In Java, int is a primitive data type for dealing with integer values. The integer number can have both positive and negative integer values. Integer values do not support fractional numbers. An integer value is stored in 4 bytes of memory by Integer. An integer’s default value is 0. Because the int data type is a numeric data type, it may be used in mathematical operations such as arithmetic, relational, and so on.
2. byte
Like the int data type, the byte is a primitive data type. The byte will take up one byte of memory when storing a number. The byte data type may hold both positive and negative integers. The byte data type does not support the storage of fractional numeric values. The default value of a byte data type is 0.
3. short
In Java, the short is a numeric primitive data type. The short values take up two bytes of memory. Positive and negative numbers can be stored in the short data type. The fractional component of an integer cannot be stored in a short. A short value’s default value is 0. In a decimal value, the fractional component will be truncated. The short data type may hold values between the byte and int data types.
4. long
The long data type is a primitive data type that can store values in memory up to 8 bytes long. The long data type can store a large number of numeric values. Fractional values are not supported by the long data type. The long value might have a positive or negative value. A long data type’s default value is 0. In Java, the long data type may hold huge integer values.
5. float
The float data type is a decimal primitive data type that may hold a number’s fractional value. A decimal value can be stored in the float data type using up to 4 bytes of memory. The float value may hold both positive and negative decimal values. The default value for the float data type is 0.0. The float data type stores a single precision decimal number with a maximum of six fractional decimal point values.
6. double
The double data type is a primitive decimal data type that can hold huge decimal values. The double data type has a memory capacity of 8 bytes. The double can hold positive and negative decimal numbers over a broad range of values. A double data type’s default value is 0.0. The double data type is a decimal data type with double precision that may contain a wide range of fractional decimal points.
7. char
The char data type is a primitive data type that may hold characters such as ascii and unicode. A character is stored in 2 bytes of memory using the char data type. The characters and values are defined by the ascii and unicode standards. A char data type’s default value is null, represented as ‘\0’.
8. bool
The bool data type is the simplest version of a java primitive data type. The bool may only store two values: 0 and 1. It is utilised to make decisions such as yes or no, true or false. The bool data type stores a value in one bit of data. The bool value is set to false by default.