Lecture 4
Lecture 4
Session No.: 4
Course Name: OOPS using Java
Course Code:R1UC201C
Duration: 50 minutes
1
Opening Question?
Can you write a Java program without declaring
any variables?
2
Learning Outcome
3
1. Introduction of Java Variables
2. Data Types in Java Program
Session 3. Activity-1
5
Java Variables – What Are They?
Definition & Purpose:
- A variable is a named memory location for storing data that can change during program execution.
- They allow programs to handle dynamic data.
Syntax:
Examples:
dataType variableName = value;
1. Simple Integer Variable:
int age = 25; Examples:
2. String Variable: 1. int count = 10;
String name = "Alice"; 2. double price = 19.99;
3. Multiple Declaration: 3. char letter = 'A';
int x = 10, y = 20, z = 30; 4. boolean isActive = true;
5. String greeting = "Hello, World!";
Additional Example: Additional Example:
int a = 5, b = 3; int number;
int sum = a + b; // sum is 8 number = 50; // Initialized later
6
Data Types
1. Primitive Data Types:
- byte, short, int, long (whole numbers) Primitive Data Types:
- float, double (decimals) int wholeNumber = 42;
- char (single characters) double fraction = 3.14;
- boolean (true/false values) boolean isAvailable = false;
2. Non-Primitive Data Types: char initial = 'B';
- String (sequence of characters)
- Arrays, Classes, Interfaces (complex structures) Non-Primitive Data Types:
String message = "Hello, Java!";
Examples: int[] scores = {90, 85, 95};
int num = 100;
double pi = 3.14159;
Type Conversion Examples:
boolean isJavaFun = true;
int a = 5;
String language = "Java";
double b = a; // Implicit conversion
double c = 5.99;
int[] numbers = {1, 2, 3, 4, 5};
int d = (int)c; // Explicit casting, d becomes 5
char grade = 'A';
7
Think-Pair-Share Activity –
Variables and Data Types
Activity Instructions:
1. Think: Read the given problem carefully use hints also.
2. Pair: Discuss with all peers in your table
3. Share: Present your ideas to the group/classroom.
8
Use cases for Activity-1
Library Book Inventory System: Weather Monitoring System: Personal Budget Tracker:
This system is designed to maintain In this scenario, the system captures and This case involves managing a record of
detailed records of books in a library. records weather information from monthly financial data. The system
It includes storing each book's title, multiple locations. Data points include stores details such as total income,
author, publication year, unique the location name, a brief weather various expenses, and the number of
identifier, and its availability status. description, temperature readings, transactions in different spending
The focus is on choosing appropriate humidity levels, and wind speed. The categories. The challenge lies in
variable types to represent textual emphasis is on the correct identification accurately representing monetary values
information, numerical data, and a and storage of diverse data, ensuring and countable items, ensuring that the
simple true/false flag for availability. that text data is used for names and financial figures are stored in a format
descriptions, while numerical data that supports fractional amounts while
supports precise values for the transaction counts remain whole.
measurements.
9
Reflection
• Submit your code to LMS
10
Solutions
Solution-1 Solutiuon-2 Solution-3
String title; String location; String category;
String author; String description; double monthlyIncome;
int publicationYear; double double monthlyExpenses;
temperature;
String isbn; int transactionCount;
double humidity;
boolean isAvailable; double windSpeed;
11
Understanding Java Keywords
Definition: Reserved words with special meanings that cannot be redefined
or can not be used as variable.
12
Introduction to Operators
Which of the following Java expressions will be evaluated first according to operator
precedence?
int result = 10 + 5 * 2 - 8 / 4;
a) 10 + 5
b) 5 * 2
c) 8 / 4
d) 10 + 5 * 2
18
Questions & Discussion
Discussion Prompts:
- What pitfalls have you experienced with variable naming or
type conversion?
- How does operator precedence affect your code outcomes?
- In what ways do keywords enforce structure in your programs?