0% found this document useful (0 votes)
13 views10 pages

Java Variables and Data Types Guide

sds

Uploaded by

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

Java Variables and Data Types Guide

sds

Uploaded by

Mesele Berhanu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Programming Practical

Session
Worksheet 2: Variables, Data Types,
and Operators
Learning Objectives
• By the end of this session, students should be
able to:
– Identify and use different Java data types.
– Declare and initialize variables correctly.
– Perform operations using arithmetic, relational,
and logical operators.
– Write small programs applying operators and
expressions.
Variables in Java
• A variable is a container for storing data values.

Syntax:
dataType variableName = value;

Example:
int age = 20;
String name = "John";

Rules:
– Must begin with a letter.
– Cannot contain spaces or special symbols (except _ and $).
– Java is case-sensitive.
Data Types in Java
• Java data types are divided into two categories:

1. **Primitive Data Types**


- byte, short, int, long (integer types)
- float, double (floating-point types)
- char (character type)
- boolean (true/false)

2. **Non-Primitive Data Types**


• - String, Arrays, Classes, Interfaces
Example Program
public class VariableDemo {
public static void main(String[] args) {
int age = 22;
double gpa = 3.75;
char grade = 'A';
boolean passed = true;

[Link]("Age: " + age);


[Link]("GPA: " + gpa);
[Link]("Grade: " + grade);
[Link]("Passed: " + passed);
}
}
Operators in Java
• Java supports various types of operators:

• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
• Assignment: =, +=, -=, *=, /=
• Increment/Decrement: ++, --
Practical Exercise
• Task 1: Create a Java program that stores and
prints your age, GPA, and university name.

• Task 2: Write a program that calculates the


average of three numbers entered by the user.

• Task 3: Demonstrate the use of arithmetic and


logical operators in a short program.
Coding Challenge
• Write a Java program that determines whether a
student passes or fails based on the following rules:
• A student passes if GPA >= 2.0 and attendance >= 75%

Sample Input:
GPA = 2.5
Attendance = 80

Expected Output:
Student Passed
Reflection Questions
• What are the differences between primitive
and non-primitive data types?
• Why must you declare a data type when
creating a variable?
• How do logical operators help in decision-
making?
• What would happen if you use an uninitialized
variable?
Summary
• In this session, you learned how to:
• • Declare and use variables.
• • Understand Java data types.
• • Apply different operators in expressions.

• Next Session: Control Structures (if, else,


switch).

You might also like