Completed

Day 2: Variables, Data Types & Operators

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 TypeSizeDescriptionExample
byte1 byteStores whole numbers from -128 to 127byte b = 100;
short2 bytesStores whole numbers from -32,768 to 32,767short s = 5000;
int4 bytesStores whole numbers from -2^31 to 2^31-1int i = 100000;
long8 bytesStores whole numbers from -2^63 to 2^63-1long l = 15000000000L;
float4 bytesStores fractional numbers with 6-7 decimal digitsfloat f = 5.75f;
double8 bytesStores fractional numbers with 15 decimal digitsdouble d = 19.99;
boolean1 bitStores true or false valuesboolean b = true;
char2 bytesStores a single character/letterchar 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 pts

User Input Calculator

medium
Not Started
Apr 915 pts

Type Casting Exercise

medium
Not Started
Apr 915 pts

Operator Precedence

hard
Not Started
Apr 1120 pts