Data types in Java are used to indicate what kind of data a variable is able to hold. They tell the compiler or interpreter about how the programmer plans to make use of the data. The types that people use in Java will guide them toward applying variables appropriately and managing memory effectively.
In this blog post, we will focus on learning primitive data types in Java with code examples. Each characteristic of each primitive data type will be studied, and we shall learn how they are applied in Java programming language.
What are Data Types in Java?
Data types in Java define what kind of data a variable can store. They help the compiler understand how the programmer intends to use the data. Using the correct data type ensures proper memory management and error-free code execution.
Types of Data Types in Java
1. Primitive Data Types:
These are the basic data types provided by Java.
Examples include int, char, float, and boolean.
They store simple values and are not objects.
2. Non-Primitive Data Types:
Also known as reference data types.
Examples include String, arrays, classes, and interfaces.
They store references to objects, not the actual data.
Primitive data types are essential for basic operations, while non-primitive data types are used for more complex data handling. In the next section, we will dive deeper into primitive data types and their specific uses in Java programming.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Primitive Data Types in Java
Primitive data types are the most basic types in Java. They are predefined by the language and named by a keyword. Primitive data types serve as the building blocks for data manipulation in Java programs. Understanding these types is crucial for efficient coding and memory management.
In this section, we will cover below non-primitive data types.
Boolean – boolean
Char – char
Int – byte, short, int, long
Float – float, double
Boolean
The boolean data type in Java is used to store two possible values: true or false. This data type is often used for simple flags that track true/false conditions, and it is essential for control flow in Java programs, such as in if statements and loops.
Size: The boolean data type represents one bit of information.
Values: It can only hold two possible values: true or false.
Usage: Commonly used for conditional checks, logical operations, and decision-making.
Example:
In this example, the boolean variables ‘isJavaFun’ and ‘isFoodTasty’ are used to store true and false values, respectively. These variables are then used in if statements to control the flow of the program based on their values.
public class BooleanExample {
public static void main(String[] args) {
// Declaring and initialising boolean variables
boolean isJavaFun = true;
boolean isFoodTasty = false;
// Using boolean variables in conditional statements
if (isJavaFun) {
System.out.println("Java is fun!"); // This will be printed
} else {
System.out.println("Java is not fun.");
}
if (isFoodTasty) {
System.out.println("Food is tasty.");
} else {
System.out.println("Food is not tasty!"); // This will be printed
}
// Printing the boolean values directly
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Is food tasty? " + isFoodTasty);
}
}
Output:
Java is fun!
Food is not tasty!
Is Java fun? true
Is food tasty? false
char
The char data type in Java is used to store a single 16-bit Unicode character. This data type is essential for handling characters and text in Java programs. Each char value represents a single character from the Unicode character set.
Size: The char data type is a 16-bit Unicode character.
Range: It has a minimum value of u0000 (or 0) and a maximum value of uffff (or 65,535 inclusive).
Usage: Commonly used to represent characters in text, such as letters, digits, and symbols.
Example:
In this example, the char variables ‘letterA’, ‘letterB’, ‘digit1’, and symbol are used to store different types of characters, including letters, digits, and symbols.
public class CharExample {
public static void main(String[] args) {
// Declaring and initialising char variables
char letterA = 'A';
char letterB = 'B';
char digit1 = '1';
char symbol = '@';
// Printing char variables
System.out.println("Letter A: " + letterA); // Prints: Letter A: A
System.out.println("Letter B: " + letterB); // Prints: Letter B: B
System.out.println("Digit 1: " + digit1); // Prints: Digit 1: 1
System.out.println("Symbol @: " + symbol); // Prints: Symbol @: @
}
}
Output:
Letter A: A
Letter B: B
Digit 1: 1
Symbol @: @
Int
The int data type in Java is used to store 32-bit signed integer values. It is one of the most commonly used data types for numeric values. Java also provides other integer data types such as byte, short, and long to handle various ranges and storage needs.
byte
The byte data type is an 8-bit signed integer. It is the smallest integer data type and is often used for saving memory in large arrays where memory savings are most needed.
Size: 8-bit
Range: -128 to 127
Usage: Useful for saving memory in large arrays.
Example:
In this example, ‘byteVar1’ and ‘byteVar2’ are declared and initialised with values 100 and -50, respectively.
The short data type is a 16-bit signed integer. It is used to save memory in large arrays when the range of values is known to be small.
Size: 16-bit
Range: -32,768 to 32,767
Usage: Useful for saving memory in large arrays when int is too large.
Example:
In this example, ‘shortVar1’ and ‘shortVar2’ are declared and initialised with values 30000 and -15000, respectively.
public class ShortExample {
public static void main(String[] args) {
// Declaring and initialising short variables
short shortVar1 = 30000;
short shortVar2 = -15000;
// Printing short variables
System.out.println("Short Variable 1: " + shortVar1); // Prints: Short Variable 1: 30000
System.out.println("Short Variable 2: " + shortVar2); // Prints: Short Variable 2: -15000
}
}
Output:
Short Variable 1: 30000
Short Variable 2: -15000
int
The int data type is a 32-bit signed integer. It is the default choice for integer values unless there is a reason to use another type due to memory constraints or specific range requirements.
Size: 32-bit
Range: -2^31 to 2^31-1
Usage: General-purpose integer arithmetic.
Example:
In this example, ‘intVar1’ and ‘intVar2’ are declared and initialised with values 100000 and -200000, respectively.
public class IntExample {
public static void main(String[] args) {
// Declaring and initialising int variables
int intVar1 = 100000;
int intVar2 = -200000;
// Printing int variables
System.out.println("Int Variable 1: " + intVar1); // Prints: Int Variable 1: 100000
System.out.println("Int Variable 2: " + intVar2); // Prints: Int Variable 2: -200000
}
}
Output:
Int Variable 1: 100000
Int Variable 2: -200000
long
The long data type is a 64-bit signed integer. It is used when a wider range than int is needed.
Size: 64-bit
Range: -2^63 to 2^63-1
Usage: When a wider range of values than int is required.
Example:
In this example, ‘longVar1’ and ‘longVar2’ are declared and initialised with values 10000000000L and -20000000000L, respectively. The ‘L’ suffix is used to indicate a long literal.
public class LongExample {
public static void main(String[] args) {
// Declaring and initialising long variables
long longVar1 = 10000000000L;
long longVar2 = -20000000000L;
// Printing long variables
System.out.println("Long Variable 1: " + longVar1); // Prints: Long Variable 1: 10000000000
System.out.println("Long Variable 2: " + longVar2); // Prints: Long Variable 2: -20000000000
}
}
Output:
Long Variable 1: 10000000000
Long Variable 2: -20000000000
These integer data types (byte, short, int, and long) provide flexibility in handling numeric data with varying ranges and storage requirements. Understanding their differences and uses helps in writing efficient Java programs.
Float
The float data type in Java is a single-precision 32-bit IEEE 754 floating point. It is used to save memory in large arrays of floating point numbers. The double data type is a double-precision 64-bit IEEE 754 floating point. It is the default choice for decimal values.
float
The float data type is used for floating-point numbers that require less precision and consume less memory.
Size: 32-bit
Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits)
Usage: Useful for saving memory in large arrays of floating-point numbers.
Example:
In this example, floatVar1 and floatVar2 are declared and initialised with values 5.75f and -3.14f, respectively. The ‘f’ suffix is used to indicate a float literal.
The double data type is used for decimal values with higher precision and is the default choice for floating-point numbers in Java.
Size: 64-bit
Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits)
Usage: Used when more precision is needed for floating-point calculations.
Example:
In the example below, ‘doubleVar1’ and ‘doubleVar2’ are declared and initialise with values 19.99 and -123.456, respectively. No suffix is needed for double literals.
Java has several primitive data types that must be understood by every single programmer. These data types provide building blocks for handling information and preserving it within Java. When you become experienced with them, you can type less buggy codes which perform better.
As a result, this blog post gives an overview of the eight primitive data types in Java like their sizes, ranges, and some examples used. Hence knowing when and how each one is used will enable better memory management and improved optimization of your Java applications.
FAQs
What are primitive data types in Java?
Primitive data types are the basic types that hold simple values, such as int, char, float, and boolean.
What is the default value of an int in Java?
The default value of an int is 0.
How many bits does a float use?
A float uses 32 bits of memory.
What is the range of a byte in Java?
The range of a byte is -128 to 127.
Can char hold a numeric value?
Yes, char can hold numeric values represented by Unicode characters.
What is the size of a long in Java?
A long is 64 bits in size.
Why use boolean in Java?
boolean is used to store true/false values, often for flags or conditions.
What is the range of a double in Java?
The range of a double is approximately ±1.79769313486231570E+308.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.