Primitive Data Types in Java with Size and Default Values

Primitive Data Types in Java

Primitive data types are basic data types in Java that determine the type and amount of memory required to store a value. Java provides a fixed set of primitive data types for efficient memory usage.

There are two main categories of primitive data types:
Numeric
Non-Numeric

1. Numeric Data Types

Numeric data types are used to store numerical values. They are further divided into two types:

a) Integer Data Types

Integer data types store whole numbers (without decimal points).
byte- 8 bits
short- 16 bits
int- 32 bits
long- 64 bits

b) Floating-Point Data Types

Floating-point data types store decimal numbers.
float- 32 bits
double- 64 bits

2. Non-Numeric Data Types

Non-numeric data types are used to store non-numerical values.
char – Stores a single character and occupies 16 bits of memory
boolean – Stores true or false (Memory size is JVM-dependent)

Default Values in Java

A default value is a value automatically assigned by the compiler when a data type is declared but not initialized.

Default Values of Some Primitive Data Types

  • int → 0
  • float → 0.0
  • boolean → false
  • char → '\u0000'

Character Data Type and Unicode

In Java, the char data type stores characters using the Unicode system.
Each character has a unique Unicode value
Unicode values are represented in hexadecimal format
The default value of char is \u0000, which represents a null (blank) character
Java uses Unicode so that it can support characters from multiple languages worldwide.

Comments

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Inheritance in Java: Types, Examples, and Explanation

Java Programming Features Every Beginner Should Know