Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 06

Data Types in Java

In this lesson: All Java data types — byte, short, int, long, float, double, char, boolean — sizes, ranges and examples. In Hindi & English.

What are data types

A data type tells Java what kind of value a variable holds — a whole number, a decimal, a character, true/false, or text. Java is strongly typed, so every variable has a type. Java's types fall into two groups: primitive types and reference types.

💡 8 primitive types store simple values directly. Reference types (like String) point to objects.

Primitive vs reference

Primitive types store the actual value in the variable — fast and simple. There are exactly eight. Reference types (String, arrays, objects) store a reference that points to data elsewhere in memory. Understanding this split explains a lot of Java behaviour later.

Integer types

TypeSizeRange (approx)
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytesabout ±2 billion
long8 bytesvery large
int  count = 100;
long big   = 10000000000L;   // L marks a long

int is the everyday choice for whole numbers. Use long for very big values, with an L suffix.

Decimal types

float  temp  = 36.6f;   // f marks a float
double price = 99.99;   // default for decimals
36.6 99.99

double is the default and most precise choice for decimals. float uses less memory but is less precise, and needs an f suffix.

char and boolean

char    grade = 'A';    // one character, single quotes
boolean pass  = true;   // only true or false
A true

char holds a single character in single quotes. boolean holds only true or false — perfect for conditions and yes/no flags.

Reference types

String name = "Aman";           // text (a class)
int[]  marks = {90, 85, 78};    // an array

System.out.println(name.length());   // 4
4

String and arrays are reference types — they point to objects. That is why String is capitalised and comes with methods like length(). Primitives have no methods.

Common mistakes

  • Overflowing int. Very large numbers need long with an L suffix.
  • Forgetting the f on float. float x = 1.5; errors — write 1.5f.
  • Using double quotes for char. char uses single quotes: 'A', not "A".
  • Thinking String is primitive. It is a class (reference type), written with a capital S.

Summary

  • Data types define what kind of value a variable holds.
  • Java has 8 primitive types and reference types like String and arrays.
  • Whole numbers: byte, short, int, long; decimals: float, double.
  • char is one character; boolean is true/false.
  • Reference types point to objects and have methods; primitives do not.

Next, store values in Java variables, or work with them using Java operators.

Frequently Asked Questions

How many primitive data types does Java have?
Java has eight primitive types: byte, short, int, long (whole numbers), float, double (decimals), char (a character), and boolean (true/false). Everything else is a reference type built from these.
What is the difference between primitive and reference types?
Primitive types store the actual value directly (like int x = 5). Reference types (like String and arrays) store a reference pointing to an object in memory. Primitives are simple values; references point to bigger objects.
What is the difference between int and long?
Both store whole numbers, but int holds up to about 2 billion, while long holds much larger numbers. Use long when a value may exceed the int range, adding an L suffix like 10000000000L.
When should I use float vs double?
Both store decimals, but double is more precise and is the default choice. Use float only to save memory in large arrays; for most maths, use double.
Is String a primitive type in Java?
No. String is a reference type (a class), not a primitive. That is why it is written with a capital S. It holds text and comes with many useful methods like length() and substring().
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOperators in Java Your progress: 6 / 61 (10%) · Lesson 7 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free