0% found this document useful (0 votes)
63 views19 pages

Lecture 4

This document outlines a Java programming session focused on variables, data types, keywords, and operators. It includes learning outcomes, definitions, examples, activities, and reflections related to these concepts. Key takeaways emphasize the importance of proper variable declaration, understanding data types, operator precedence, and the role of keywords in structuring Java programs.

Uploaded by

Yamini Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views19 pages

Lecture 4

This document outlines a Java programming session focused on variables, data types, keywords, and operators. It includes learning outcomes, definitions, examples, activities, and reflections related to these concepts. Key takeaways emphasize the importance of proper variable declaration, understanding data types, operator precedence, and the role of keywords in structuring Java programs.

Uploaded by

Yamini Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Fundamentals: Variables, Data

Types, Keywords & Operators

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

LO1: apply the concept of variables, data types, and


keywords in code to create well-structured and
syntactically correct programs using Java programming.

LO2: examine the concept of operators and their


precedence to solve real-time problems using
Java Programming.

3
1. Introduction of Java Variables
2. Data Types in Java Program
Session 3. Activity-1

Outline 4. Operators in Java


5. Keywords in Java
6. Activity-2
7. Conclusion
4 Galgotias University
Scenarios where without variable you can not handle
data processing

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

Problem-1: Problem-2: Problem-3:

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.

3. Declarations: class, public, static, void


Examples & Usage:
public class MyClass { ... }
1. Control Flow: if, else, switch, case 4. Others: new, return, break, continue
if (score > 50) { ... } else { ... } int[] nums = new int[5];
2. Looping: for, while, do Additional Example: Exception Handling
for (int i = 0; i < 5; i++) { ... } try { ... } catch (Exception e) { ... } finally { ... }

12
Introduction to Operators

Definition: Operators perform operations on variables and values.

Categories & Examples: 3. Logical:


1. Arithmetic: - AND: boolean andOp = (5 > 3) && (8 > 5);
- Addition: int total = 10 + 20;
- OR: boolean orOp = (5 > 10) || (8 > 5);
- Subtraction: int diff = 30 - 10;
- NOT: boolean notOp = !(5 == 5);
- Multiplication: int prod = 5 * 4;
4. Assignment:
- Division: int quo = 20 / 4;
- Simple: int num = 10;
- Modulus: int rem = 10 % 3;
- Compound: int score = 50; score += 10;
2. Relational:
5. Bitwise (Extra Example):
- Equal to: boolean eq = (5 == 5); - AND: int bitAnd = 5 & 3;
- Not equal: boolean neq = (5 != 3); - OR: int bitOr = 5 | 3;
- Greater than: boolean gt = (10 > 8);
- Less than: boolean lt = (3 < 5);
13
Operator Precedence Categories and Their Order
From Highest to Lowest:
7. Equality: ==, !=
1. Postfix: () [] . ++ -- 8. Bitwise AND: &
2. Unary: +, -, ++, --, !, ~ 9. Bitwise XOR: ^
3. Multiplicative: *, /, % 10. Bitwise OR: |
4. Additive: +, - 11. Logical AND: &&
5. Shift: <<, >>, >>> 12. Logical OR: ||
13. Ternary: ? :
6. Relational: <, >, <=, >=
14. Assignment: =, +=, -=, *=, /=, %=
Activity-2: Problem based activity

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

Which of the following statements about reserved keywords in Java is false?


a) A reserved keyword cannot be used as an identifier (variable, method, or class name).
b) const and goto are reserved in Java but are not used.
c) The true, false, and null literals are reserved words and can be used as variable names.
d) The instanceof keyword is used to check whether an object is an instance of a specific
class
15
Activity-2: Problem based activity

What will be the output of the program?


int a = 8, b = 3, c = 2, d = 5;
int result = a - b + c * d / c % b; System.out.println(result);
a) 7
b) 8
c) 9
d) 10

Which of the following is not a reserved keyword in Java?


a) static
b) boolean
c) delete
d) Volatile
16
Activity-2: Problem based activity

What will be the output of the following Java program?


java
int x = 6, y = 3, z = 2;
int result = x * y / z + x % y - z * y; System.out.println(result);
a) 2
b) 3
c) 4
d) 5

Which of the following is a reserved keyword in Java?


a) class
b) define
c) function
d) variable
17
Recap & Key Takeaways
Summary:
- Variables: Containers for changeable data; proper declaration and
naming are essential.
- Data Types: Primitive vs. non-primitive; select correctly for efficiency.
- Operators: Arithmetic, relational, logical, assignment, bitwise;
understand precedence.
- Keywords: Reserved words that structure the Java language.

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?

Feel free to ask questions or share experiences.


19

You might also like