⚡0 XP🔥0

Variables and Data Types

Lesson 2 of 15 ⏱ 6 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Java

The 8 primitive types, variables and literals, var, constants with final, type casting (widening and narrowing), wrapper classes and a first look at String.

Variables#

A variable is a named memory location. Java is statically typed: every variable has a fixed type, declared before use.

int age;            // declaration
age = 21;           // assignment
int marks = 95;     // declaration + initialisation
▶ Try it YourselfJava
Ctrl + Enter to run
Output
Aditi is 21 years old
CGPA 9.47, grade A, placed: true

+ with a String joins (concatenates) values into text.

The 8 primitive types#

Type Size Range / precision Example
byte 1 byte -128 to 127 byte b = 100;
short 2 bytes -32,768 to 32,767 short s = 3000;
int 4 bytes about ±2.1 billion int n = 150000;
long 8 bytes about ±9.2 × 10¹⁸ long pop = 1_400_000_000L;
float 4 bytes ~7 decimal digits float f = 3.14f;
double 8 bytes ~15 decimal digits double d = 3.14159;
char 2 bytes one Unicode character char c = 'अ';
boolean - true or false boolean ok = false;

Unlike C, these sizes are the same on every platform - part of Java's portability.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
int max: 2147483647
long max: 9223372036854775807
byte range: -128 to 127
India: 1428627663
Unicode char code: 2309
Overflow wraps around: -2147483648
Common Mistake

Integer overflow does not give an error in Java - the value silently wraps around. Use long when numbers can exceed about 2 billion (money in paise, populations, factorials).

Literals and var#

  • long literals end with L, float literals with f: 5000000000L, 2.5f.
  • Decimal literals are double by default.
  • Since Java 10, var lets the compiler infer the type of a local variable:
var count = 10;          // int
var price = 99.5;        // double
var city = "Indore";     // String

The type is still fixed - var only saves typing.

Constants: final#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Area: 153.93791
Seats: 60

By convention, constant names are written in UPPER_SNAKE_CASE.

Type casting#

Widening (small type → bigger type) happens automatically because no data is lost: byte → short → int → long → float → double.

Narrowing (big → small) needs an explicit cast and may lose data:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
87.0 vs 87.4
Rupees: 99
(byte) 300 = 44
65 B
हिंदी में समझें

छोटे type से बड़े type में बदलना (int → double) अपने आप हो जाता है - इसे widening कहते हैं। बड़े से छोटे में (double → int) बदलने के लिए (int) लिखकर cast करना पड़ता है, और दशमलव वाला हिस्सा कट जाता है। याद रखें: 437 / 5 पहले int में भाग होता है (87), फिर double बनता है।

Wrapper classes#

Each primitive has an object version (a wrapper class) with useful methods. Collections like ArrayList can only hold objects, so Java converts automatically (autoboxing):

Primitive Wrapper
int Integer
double Double
char Character
boolean Boolean
long Long
▶ Try it YourselfJava
Ctrl + Enter to run
Output
2027
91.5
2027!
1010 ff
true Q
42

Primitive vs reference types#

Primitives (int, double...) store the value itself. Everything else - String, arrays, objects - is a reference type: the variable stores a reference (address) to an object on the heap. A reference can be null (pointing to nothing). You will see why this matters when comparing Strings.

Summary#

  • Java has 8 primitives with fixed sizes on every platform; everything else is a reference type.
  • Use L for long and f for float literals; var infers local types.
  • final makes constants; widening is automatic, narrowing needs (type).
  • Wrapper classes (Integer, Double...) convert between text and numbers.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Bill calculation

An item costs 1299.50 and the customer buys 3. Add 18% GST. Print with printf:

Subtotal: 3898.50
GST: 701.73
Total: 4600.23
Expected output:
Subtotal: 3898.50
GST: 701.73
Total: 4600.23
2

Casting practice

Given double marks = 87.65;, print the integer part using a cast, the value rounded to the nearest int with Math.round, and the ASCII code of the char 'J':

Integer part: 87
Rounded: 88
ASCII of J: 74
Expected output:
Integer part: 87
Rounded: 88
ASCII of J: 74

Quick Quiz

Q1.

How many primitive data types does Java have?

Q2.

What is the size of a char in Java?

Q3.

What is printed by System.out.println(10 / 4);?

Q4.

Which line does NOT compile?

Q5.

What does final do to a variable?