Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Introduction to Java Data Types

[post-views]

 

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 Type Size (in bits) Default Value Example
byte 8 0 byte byteVar = 127;
short 16 0 short shortVar = 32767;
int 32 0 int intVar = 2147483647;
long 64 0L long longVar = 9223372036854775807L;
float 32 0.0f float floatVar = 3.14f;
double 64 0.0 double doubleVar = 3.14159;
char 16 ‘u0000’ char charVar = 'A';
boolean false boolean 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

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 


Warning: Undefined array key "student_url_profile" in /var/www/wp-content/plugins/masterstudy-lms-learning-management-system/_core/lms/helpers.php on line 1340

Warning: Undefined array key "student_url_profile" in /var/www/wp-content/plugins/masterstudy-lms-learning-management-system/_core/lms/helpers.php on line 1345