Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Week 3 - Lecture 5 and 6 Java Programming Basics Part 2

The document outlines key concepts in Object Oriented Programming, specifically focusing on variables, their types (local, instance, static), and their scope and lifetime in Java. It also discusses constants, arithmetic expression evaluation, and the importance of operator precedence and associativity. Additionally, it includes examples of Java class libraries and user input handling using the Scanner class.

Uploaded by

uw-24-cs-bs-100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 3 - Lecture 5 and 6 Java Programming Basics Part 2

The document outlines key concepts in Object Oriented Programming, specifically focusing on variables, their types (local, instance, static), and their scope and lifetime in Java. It also discusses constants, arithmetic expression evaluation, and the importance of operator precedence and associativity. Additionally, it includes examples of Java class libraries and user input handling using the Scanner class.

Uploaded by

uw-24-cs-bs-100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS-112

Object Oriented Programming


DEPARTMENT OF COMPUTER SCIENCE
COURSE INSTRUCTOR : MS. HABIBA ARSHAD
Topics To be Covered Today

❑ Variables
❑ Scope and Lifetime of variable
❑ Constants
❑ Arithmetic Expression
❑ Java Classes
❑ Control Structure
Variables
Variable is a named location where data can be stored. It is a location
in computer’s memory with a specific address, where a value can be
stored and retrieved when required.
A variable is assigned with a data type.

int data=50;//Here data is variable


Types:
There are three types of variables in java:
•local variable
•instance variable
•static variable
Variables Declaration and initialization in
Java
Format:
<data type> <name of variable>;
Example:
char var1;

Variables can be initialized (set to a starting value) as they’re


declared:
char var1 = ‘j’;
int age = 30;
float x=3.1f;
double y=3.145d;
Local Variables
A variable declared inside the body of the method is called local
variable.

You can use this variable only within that method and the other
methods in the class aren't even aware that the variable exists.

Create multiple copies each time when the class object is


created. Means it does NOT retain its value across method calls
(it gets reinitialized each time).
Static Variable
A variable that is declared using static keyword at the class level.
It cannot be local.
Only single copy of the static or class variable is created and shared
among all the instances of the class.
Memory allocation for static variables happens only once when the
class is loaded in the memory.
They are created when the class is loaded and destroyed when it is
unloaded.
Static vs Local Variables
Class A{ public static void main (String [] args)
static int a = 10; // static variable {
void method(){ A ref = new A();
int b = 10; // local variable ref.method();
system.out.println(a+ “ “ +b); ref.method();
++a;
}
++b;
}
}
Instance Variable
A variable declared inside the class but outside the body of the
method, is called an instance variable.
It is not declared as static.
It is called an instance variable because its value is instance-specific
and is not shared among instances.
Create multiple copies each time when the class object is created.
Example to understand the types of variables in
java
public class A {
static int m=100;//static variable
int n=90;//instance variable
public static void main(String[] args)
{
int data=50;//local variable
System.out.println(data);
System.out.println(A.m); // Static variables are class members directly
call through class name no need for object
A ref = new A();
System.out.println(ref.n); //To access the instance variable object is required.
} }//end of class
Scope and Lifetime of Variable
Scope of a variable determines the area or a region of code where a
variable is available to use.
Lifetime of a variable is defined by the time for which a variable
occupies some valid space in the system's memory.
Example
Java Class Libraries
The Java environment relies on several built-in class libraries that
contain many built-in methods that provide support for such things
as I/O, string handling, networking, and graphics.
The standard classes also provide support for windowed
Input/output.
Thus, Java as a totality is a combination of the Java language itself,
plus its standard classes.
Java (class Scanner)
import java.util.Scanner; System.out.print("Enter a double value: ");
double doubleValue = scanner.nextDouble();
public class UserInputExample {
public static void main(String[] args) { scanner.nextLine(); // Consume newline left-over
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
// Taking different types of input from user String stringValue = scanner.nextLine();
System.out.print("Enter a byte value: ");
byte byteValue = scanner.nextByte(); // Displaying user inputs
System.out.println("\nYou entered the following
System.out.print("Enter a character: "); values:");
char charValue = scanner.next().charAt(0); System.out.println("Byte: " + byteValue);
System.out.println("Character: " + charValue);
System.out.print("Enter an integer: "); System.out.println("Integer: " + intValue);
int intValue = scanner.nextInt(); System.out.println("Long: " + longValue);
System.out.println("Float: " + floatValue);
System.out.print("Enter a long value: "); System.out.println("Double: " + doubleValue);
long longValue = scanner.nextLong(); System.out.println("String: " + stringValue);

System.out.print("Enter a float value: "); scanner.close(); // Closing scanner


float floatValue = scanner.nextFloat(); }
Constants
Constants are like variables their value CANNOT change.
If the constant is referred to several times throughout the program, changing the value of the
constant once will change it throughout the program.
Format:
final <type ><CONSTANT NAME> = <value>;

Example:
final int var = 100;
final double pi= 3.14159;
private static final double pi= 3.14159;
Arithmetic Expression Evaluation
To evaluate an arithmetic expression two concepts needs to be
understood
◦Operator Precedence
◦ Operator precedence controls the order in which operations are performed
◦Operator Associativity
◦ The associativity of an operator specifies the order in which operations of
the same precedence are performed
Operator Precedence and Associativity
Operators Precedence and Associativity for Java is following

1. *, /, % Do all multiplications, divisions and remainders from left to


right.
2. +, - Do additions and subtractions from left to right.
Evaluating an Expression
6+2*3/6
Three operators are in this expression.
However, * and / both have the same precedence and + has lower precedence
then these two.
* and / will be evaluated first but both have the same precedence level.
Therefore, operator associativity will be used here to determine the first to get
evaluated i.e. left to right.
The right most sub expression will be evaluated followed by the next right one
and so on.
Class Participation

What will happen?

float f=65/10+38/10;
System.out.println(f);
Task
List down Java Compile –time and Run-time errors.

You might also like