Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Text lesson

Introduction to Java Data Types

In this lesson, you will learn.

  • Java Data Types

 

Java Data Types

In Java, data types specify the type of data that a variable can hold. Java has two categories of data types: primitive data types and reference data types.

 

 

1. Primitive Data Types

  • These are basic or predefined data types and are directly supported by the language.
  • They represent simple values and are stored directly in memory.
  • There are eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean.

 

1. Integral Data Types:

  • byte: 8-bit signed integer.
  • short: 16-bit signed integer.
  • int: 32-bit signed integer.
  • long: 64-bit signed integer.

Example

byte byteVar = 127;
short shortVar = 32767;
int intVar = 2147483647;
long longVar = 9223372036854775807L;  // Note the 'L' suffix for long literals

 

2. Floating-Point Data Types:

  • float: 32-bit floating-point.
  • double: 64-bit floating-point.

Example

float floatVar = 3.14f;  // Note the 'f' suffix for float literals
double doubleVar = 3.14159;

 

3. Character Data Type:

  • char: 16-bit Unicode character.

Example

char charVar = 'A';

 

4. Boolean Data Type:

  • boolean: Represents true or false.

Example

boolean boolVar = true;

 

Here’s a summary table of all primitive data types in Java:

 

Data TypeSize (in bits)Default ValueExample
byte80byte byteVar = 127;
short160short shortVar = 32767;
int320int intVar = 2147483647;
long640Llong longVar = 9223372036854775807L;
float320.0ffloat floatVar = 3.14f;
double640.0double doubleVar = 3.14159;
char16‘u0000’char charVar = 'A';
booleanfalseboolean boolVar = true;
Summary of Primitive Data Types in Java

 

2. Non-primitive Data Types (Reference Types):

  • These are more complex data types not predefined by the language but created by the programmer.
  • They include objects, arrays, and interfaces.
  • Non-primitive data types store references to the memory location where the actual data is stored.

 

Example

// Non-primitive data type (Object)
String stringValue = "Hello, Java!";  // String is a class in Java

// Non-primitive data type (Array)
int[] intArray = {1, 2, 3, 4, 5};     // Array is a reference type

// Non-primitive data type (Interface)
interface Printable {
    void print();
}

class MyClass implements Printable {
    public void print() {
        System.out.println("Printing...");
    }
}

Printable printableObj = new MyClass();  // Interface type

 

Summary

In summary, primitive data types are the basic building blocks directly representing simple values, while non-primitive data types are more complex and represent references to objects, arrays, or interfaces.

Understanding the distinction between these two types is important for effective Java programming and memory management.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent
Very good
Average
Poor
Terrible

 

5 April 2025

It was very helpful .thank you sir

 

 

Submit a Review