Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Primitive Data Types in Java – Everything You Need to Know

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

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.

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.

public class ByteExample { public static void main(String[] args) { // Declaring and initialising byte variables byte byteVar1 = 100; byte byteVar2 = -50; // Printing byte variables System.out.println("Byte Variable 1: " + byteVar1); // Prints: Byte Variable 1: 100 System.out.println("Byte Variable 2: " + byteVar2); // Prints: Byte Variable 2: -50 } }

Output:

Byte Variable 1: 100 Byte Variable 2: -50

short

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.

public class FloatExample { public static void main(String[] args) { // Declaring and initialising float variables float floatVar1 = 5.75f; // The 'f' suffix indicates a float literal float floatVar2 = -3.14f; // Printing float variables System.out.println("Float Variable 1: " + floatVar1); // Prints: Float Variable 1: 5.75 System.out.println("Float Variable 2: " + floatVar2); // Prints: Float Variable 2: -3.14 } }

Output:

Float Variable 1: 5.75 Float Variable 2: -3.14

double

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.

public class DoubleExample { public static void main(String[] args) { // Declaring and initialising double variables double doubleVar1 = 19.99; double doubleVar2 = -123.456; // Printing double variables System.out.println("Double Variable 1: " + doubleVar1); // Prints: Double Variable 1: 19.99 System.out.println("Double Variable 2: " + doubleVar2); // Prints: Double Variable 2: -123.456 } }

Output:

Double Variable 1: 19.99 Double Variable 2: -123.456

 

Here’s a comprehensive table for all primitive data types in Java:

 

Type Description Default Size Range of Values Example Literals
byte 8-bit signed integer 0 8-bit -128 to 127 100, -50
short 16-bit signed integer 0 16-bit -32,768 to 32,767 30000, -15000
int 32-bit signed integer 0 32-bit -2^31 to 2^31-1 100000, -200000
long 64-bit signed integer 0L 64-bit -2^63 to 2^63-1 10000000000L, -20000000000L
float Single-precision 32-bit IEEE 754 floating point 0.0f 32-bit ±3.40282347E+38F (6-7 significant decimal digits) 5.75f, -3.14f
double Double-precision 64-bit IEEE 754 floating point 0.0d 64-bit ±1.79769313486231570E+308 (15 significant decimal digits) 19.99, -123.456
boolean Represents one bit of information false 1-bit true or false true, false
char 16-bit Unicode character ‘u0000’ 16-bit ‘u0000’ (0) to ‘uffff’ (65,535) ‘A’, ‘B’, ‘u263A’
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Conclusion

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
Primitive data types are the basic types that hold simple values, such as int, char, float, and boolean.
The default value of an int is 0.
A float uses 32 bits of memory.
The range of a byte is -128 to 127.
Yes, char can hold numeric values represented by Unicode characters.
A long is 64 bits in size.
boolean is used to store true/false values, often for flags or conditions.
The range of a double is approximately ±1.79769313486231570E+308.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
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.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved