NBKRIST JavaHub – Java Data Types & Variables

Java Fundamentals – Data Types, Variables & More

This page helps beginners quickly understand one of the most essential areas of Core Java—variables, data types, constants, and formatted output.

1. Introduction

Java programs store and manipulate data using variables. Each variable has a type, which decides what value it can hold.

Java is strongly typed—meaning every variable must have a clearly defined type.

2. Data Types in Java

Java provides primitive and reference data types. Primitive types store simple values, while objects store complex data.

Primitive types include: int, float, char, boolean, byte, short, long, double.

class TypesDemo { public static void main(String[] args) { int age = 20; double salary = 45000.75; char grade = 'A'; boolean pass = true; System.out.println(age + " " + salary + " " + grade + " " + pass); } }
20 45000.75 A true

3. Declaration of Variables

Variables must be declared before use. Declaration tells Java what type of data will be stored.

Multiple variables can be declared together if they share the same type.

int a = 10; int x, y, z;

4. Type Casting

Type casting converts one data type to another. Two types: widening and narrowing.

Widening happens automatically; narrowing requires explicit conversion.

double d = 10.5; int x = (int)d; // narrowing System.out.println(x);
10

5. Scope of Variables & Identifiers

Scope defines where a variable is accessible: local, instance, static.

Local variables exist inside methods; instance variables belong to objects.

class Scope { int a = 10; // instance void show(){ int x = 5; // local System.out.println(a + x); } }
15

6. Literal Constants

Literals are fixed values stored in variables. Examples: numbers, characters, strings.

Java supports integer, floating, boolean, and string literals.

int age = 21; char grade = 'A'; String name = "NBKRIST";

7. Symbolic Constants (final)

Symbolic constants are defined using the final keyword. Their value cannot change once assigned.

Used for fixed values like PI, TAX_RATE, MAX_SPEED.

final int MAX_SPEED = 180; System.out.println(MAX_SPEED);

8. Formatted Output using printf()

printf() prints formatted output similar to C language.

Useful for aligning values and formatting decimals.

System.out.printf("Salary: %.2f", 25000.678);
Salary: 25000.68

9. Static Variables and Methods

Static members belong to the class, not objects. All objects share the same static variable.

Static methods can be called using class name.

class Demo { static int count = 0; static void show(){ System.out.println(count); } } Demo.show();
0

10. Attribute final

final variables cannot be modified after assignment.

Used for constants that must not change.

final int ROLL_NO = 101; System.out.println(ROLL_NO);
101

Practice Questions – Click to Reveal Answers

1️⃣ What are primitive data types in Java?
byte, short, int, long, float, double, char, boolean.
2️⃣ What is a variable?
A named memory location to store data.
3️⃣ What is narrowing casting?
Converting larger type to smaller: double → int.
4️⃣ What is a literal?
A fixed value directly written in code.
5️⃣ What is the use of final keyword?
It creates constants whose values cannot change.
6️⃣ What is scope?
Defines where a variable is accessible.
7️⃣ What is a static method?
A method that belongs to class, not objects.
8️⃣ Why use printf()?
For formatted printing.
9️⃣ Give an example of a symbolic constant.
final int MAX = 100;
🔟 Define type casting.
Converting one data type into another.

Quick Summary