Java From Beginner To Advanced

0% completed

Previous
Next
Primitive Data Types

In Java, primitive data types are the most basic data types provided by the language. They directly store the value and are not objects. Java supports eight primitive data types, and each type has a specific size and range. Understanding these data types is crucial as they form the foundation of data handling in Java.

Syntax for Using Primitive Data Types

The general syntax for declaring and initializing a variable with a primitive data type is as follows:

type variableName = value;
  • type: Defines the data type of the variable (e.g., int, char, double).
  • variableName: The name of the variable.
  • value: The initial value assigned to the variable (optional during declaration).

1. byte

The byte data type is an 8-bit signed integer. It is typically used to save memory in large arrays where the memory savings matter.

  • Size: 8 bits
  • Range: -128 to 127

Example: Declaring and Initializing a byte Variable

In this example, we declare a byte variable and print its value.

Java
Java

. . . .

2. short

The short data type is a 16-bit signed integer. It is used when memory savings are important, and the range of values is sufficient.

  • Size: 16 bits
  • Range: -32,768 to 32,767

Example: Declaring and Initializing a short Variable

In this example, we declare a short variable myShort and assign it the value 20000.

Java
Java

. . . .

3. int

The int data type is a 32-bit signed integer. It is one of the most commonly used data types for numeric operations.

  • Size: 32 bits
  • Range: -2,147,483,648 to 2,147,483,647

Example: Declaring and Initializing an int Variable

In this example, we declare an int variable named myInt and initialize it with 100000.

Java
Java

. . . .

4. long

The long data type is a 64-bit signed integer. It is used for values that exceed the range of int.

  • Size: 64 bits
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example: Declaring and Initializing a long Variable

In this example, we declare a long variable named myLong and assign it the value 10000000000L. The L at the end indicates it’s a long literal.

Java
Java

. . . .

5. float

The float data type is a 32-bit floating-point number. It is used for precise calculations with fractional numbers.

  • Size: 32 bits
  • Precision: Approximately 6-7 decimal digits

Example: Declaring and Initializing a float Variable

In this example, we declare a float variable myFloat and assign it the value 5.75f. The f indicates it’s a float literal.

Java
Java

. . . .

6. double

The double data type is a 64-bit floating-point number. It is the default data type for decimal values and is used for precise calculations.

  • Size: 64 bits
  • Precision: Approximately 15 decimal digits

Example: Declaring and Initializing a double Variable

In this example, we declare a double variable named myDouble and assign it the value 19.99.

Java
Java

. . . .

7. char

The char data type is a 16-bit Unicode character. It is used to store a single character.

  • Size: 16 bits
  • Range: '\u0000' (0) to '\uffff' (65,535)

Example: Declaring and Initializing a char Variable

In this example, we declare a char variable named myChar and assign it the value 'A'.

Java
Java

. . . .

8. boolean

The boolean data type has only two possible values: true or false. It is used for conditional statements.

  • Values: true or false

Example: Declaring and Initializing a boolean Variable

In this example, we declare a boolean variable named myBoolean and assign it the value true.

Java
Java

. . . .

All Primitive Data Types

Data TypeSize (bits)Range/PrecisionExample Value
byte8-128 to 127100
short16-32,768 to 32,76730000
int32-2,147,483,648 to 2,147,483,647100000
long64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,80710000000000L
float326-7 decimal digits (approx.)5.75f
double6415 decimal digits (approx.)19.99
char16'\u0000' to '\uffff' (Single Unicode character)'A'
booleanOnly true or falsetrue

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next