Coding/Learn/java/data types
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnPrimitive Data Types
6 / 31 0%
Lesson 06 of 31
7 mins read

Primitive Data Types

PreviousNext Lesson
In Plain English

Java data types are divided into two categories: Primitive Types (which store raw values directly in memory) and Reference Types (like String, Arrays, and Objects). Java has 8 built-in primitive types optimized for speed and memory efficiency.

The Mental Model

"Primitive types are like physical coins in your pocket: you hold the exact value. Reference types are like a bank account slip: they point to where the money is stored."

Deep Dive: How It Works

Whole numbers: `byte` (8-bit), `short` (16-bit), `int` (32-bit, default), `long` (64-bit, suffix L).

Floating points: `float` (32-bit, suffix f), `double` (64-bit, default decimal type).

Boolean: `boolean` (true or false).

Character: `char` (16-bit Unicode character, enclosed in single quotes 'A').

Syntax Blueprint

int count = 42;
double price = 19.99;
char grade = 'A';
boolean active = true;

Use single quotes for char ('Z') and double quotes for String ("Hello").

PointCore Rules to Remember

Point star
Default Types: int is the standard for whole numbers; double is the standard for decimals.
Point star
Char vs String: char uses single quotes ('X'); String is an object using double quotes ("X").
Point star
Long & Float Suffixes: Use 10000000000L for long and 5.75f for float.

Common Beginner Traps & How to Fix Them

Mistake:Using double quotes for char: char c = "A";

Why it happens: "A" is a String object, not a primitive char.

How to fix: Use single quotes: char c = 'A';

Pro Tip: For 95% of daily tasks, int, double, boolean, char, and String are all you need.

Live Interactive Example

Hit Run Code to see it live
The Core Java Primitives
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Each primitive type stores its value with precise byte size in memory.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Store product details

  • PointDeclare an int variable "items" with value 5.
  • PointDeclare a double variable "totalCost" with value 49.95.
  • PointPrint "Items: " + items and "Total: $" + totalCost on separate lines.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates