Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Data Types

Java Data Types

As previously mentioned, every variable in Java must have a defined data type.

Example

int myNum = 5; // Integer (whole number)

float myFloatNum = 5.99f; // Floating point number

char myLetter = ‘D’; // Character

boolean myBool = true; // Boolean

String myText = “Hello”; // String

Data types are categorized into two groups:

  1. Primitive data types – comprise byte, short, int, long, float, double, boolean, and char.
  2. Non-primitive data types – encompass String, Arrays, and Classes (further details on these will be covered in subsequent chapters).

Primitive Data Types

Primitive data types in Java define the size and type of variable values and do not possess additional methods.

Java encompasses eight primitive data types:

Data Type

Size

Description

Byte

1 byte

Stores integers ranging from -128 to 127

Short

2 bytes

Stores integers ranging from – 32,768 to 32,767

Int

4 bytes

Stores integers ranging from – 2,147,483,648 to 2,147,483,647

Long

8 bytes

Stores integers ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Float

4 bytes

Stores fractional numbers with accuracy up to 6 to 7 decimal digits

Double

8 bytes

Stores fractional numbers with precision up to 15 decimal digits

Boolean

1 bit

Store values representing true or false

char

2 bytes

Stores a single character or ASCII values