Day Two: Variables, Data Types & Operators
Learn about variables, data types, reading user input, type casting, and operators in Java.
01Variables & Data Types
Learn how to declare variables, understand primitive and reference data types, and store different kinds of data.
02User Input & Type Casting
Read input from users using the Scanner class and convert between different data types with type casting.
03Operators
Master arithmetic, assignment, comparison, logical, and bitwise operators to perform operations on variables.
Learning Materials
Variables in Java
Variables are containers for storing data values. In Java, there are different types of variables:
- Primitive variables - store simple values
- Reference variables - store addresses of objects
Variable Declaration Syntax:
dataType variableName = value;
Example:
int age = 25; double salary = 50000.50; boolean isEmployed = true; char grade = 'A'; String name = "John Doe";
Data Types in Java
Primitive Data Types:
| Data Type | Size | Description | Example |
|---|---|---|---|
| byte | 1 byte | Stores whole numbers from -128 to 127 | byte b = 100; |
| short | 2 bytes | Stores whole numbers from -32,768 to 32,767 | short s = 5000; |
| int | 4 bytes | Stores whole numbers from -2^31 to 2^31-1 | int i = 100000; |
| long | 8 bytes | Stores whole numbers from -2^63 to 2^63-1 | long l = 15000000000L; |
| float | 4 bytes | Stores fractional numbers with 6-7 decimal digits | float f = 5.75f; |
| double | 8 bytes | Stores fractional numbers with 15 decimal digits | double d = 19.99; |
| boolean | 1 bit | Stores true or false values | boolean b = true; |
| char | 2 bytes | Stores a single character/letter | char c = 'A'; |
Reference Data Types:
Reference types store references to objects. The most common reference type is:
- String - stores text, e.g.,
String name = "John"; - Arrays - stores collections of values, e.g.,
int[] numbers = {1, 2, 3}; - Classes - user-defined types, e.g.,
Person person = new Person();
Practice Quiz
Variables, Data Types & Operators - Practice Quiz
Test your knowledge of Java variables, data types, and operators • 30 min
00:00
Progress0/8 questions answered
1.
Which of the following is a primitive data type in Java?1 point
Homework Assignments
Complete these assignments to reinforce your understanding of variables, data types, and operators
Due: Apr 9, 2025
0/4 Completed
Progress0/4 tasks completed
Variable Declaration and Initialization
easy
Not Started
Apr 910 ptsUser Input Calculator
medium
Not Started
Apr 915 ptsType Casting Exercise
medium
Not Started
Apr 915 ptsOperator Precedence
hard
Not Started
Apr 1120 pts