JAVA Chapter3 Lecture Notes
JAVA Chapter3 Lecture Notes
This document does not claim any originality and cannot be used as
substitute for prescribed textbooks. The information presented here is
purely a collection by the course lecturer for teaching assignments.
Various textbooks as well as freely available materials from the internet
were consulted for preparing this document. The ownership of the
information lies with the respective authors or institutions.
Java
Java programming language was
originally developed by Sun
Microsystems which was initiated
by James Gosling and released in
1995 as core component of Sun
Microsystems' Java platform (Java
1.0 [J2SE]).
Java Virtual Machine (JVM) which is Java Development Environment JDK Development Tools
}
Every line of code that runs in Java must be inside a class. In the
above example, we named the class as Java. A class should always
start with an uppercase first letter.
Include a cover page with your Name, Registration Number and Programme of
Study.
Variables are memory locations that stores and access data while a program
is in execution.
Each variable in java has a specific data type, which determines the size,
layout and range of values that can be stored within that memory location.
Java Variables
Variable Declaration
To declare a variable we follow this syntax: data_type variable_name = value;
Local Variable
A variable declared inside the body of the method is called local variable. You
can use this variable only within that method.
A local variable cannot be defined with "static" keyword.
Java Variables
Instance Variable
A variable declared inside the class but outside the body of the method, is
called instance variable. It is not declared as static.
It is called instance variable because its value is instance specific and is not
shared among instances.
Java Variables
Static Variable
Static variables are also known as class variable because they are associated
with the class and common for all the instances of the class.
For example, if we create three objects of a class and access this static variable,
it would be common for all, the changes made to the variable using one of the
object would reflect when you access it through other objects.
Java Variables
Example
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Java Operators
Example of Assignment Operators
public class Assignment {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
Java Operators
Example of Logical Operators
public class Logical {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Java Operators
Example of Unary Operators
public class Unary {
public static void main(String[] args) {
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
Java Operators
Example of Bitwise Operators
public class Bitwise {
public static void main(String[] args) {
// declare variables
int x = 9, y = 8;
// bitwise and
// 1001 & 1000 = 1000 = 8
System.out.println("x & y = " + (x & y));
// bitwise XOR
// 1001 ^ 1000 = 0001 = 1
System.out.println("x ^ y = " + (x ^ y));
// bitwise inclusive OR
// 1001 | 1000 = 1001 = 9
System.out.println("x | y = " + (x | y));
// bitwise compliment
// ~0010= 1101 = -3
System.out.println("~x = " + (~x));
}
}
Java Conditions and If Statements
Java supports the usual logical conditions from mathematics such as:
Less than: <
Less than or equal to: <=
Greater than: >
Greater than or equal to: >=
Equal to: ==
Not Equal to: !=
if (condition) {
// block of code to be executed if the condition is true
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
int time = 20;
if (time < 15) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
Java else Statement
Java Ternary Operator
int time = 20;
Java has a short-hand if else, if (time < 15) {
which is known as the ternary System.out.println("Good day.");
operator, it consists of three } else {
System.out.println("Good evening.");
operands.
}
// Outputs "Good evening."
It can be used to replace
multiple lines of code with a int time = 20;
single line. It is often used to String result = (time < 18) ? "Good day." : "Good
evening.";
replace simple if else System.out.println(result);
statements
variable = (condition) ? expressionTrue : expressionFalse;
Java else if Statement
Java uses the else statement to specify a block of code to be executed if
the given condition is false.
if (condition1) {
// block of code to be executed if the condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false
and condition2 is true
}
else{
// block of code to be executed if the condition1 and
condition2 are false
}
Java else if Statement
Loops are handy because they save time, reduce errors, they enhance the
readability of the code.
We use for loop when we know exactly how many times we want to loop
through a block of code.
Statement 3 is executed (every time) after the execution of the code block.
Java For Loop
Statement 2 defines the condition for the loop to run (i must be less than 10). If
the condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has
been executed.
Java For Loop
Methods are used to perform certain actions, and they are also known as
functions.
We use methods to reuse code (define the code once, and use it many
times).
It is defined with the name of the method, followed by parentheses ().
modifier − It defines the access type of the method and it is optional to use.
Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.
method body − The method body defines what the method does with the
statements.
Method Calling
To call a method in Java, we write the method's name followed by two
parentheses () and a semicolon ( ; ).
Instead of defining two methods that should do the same thing, it is better to
overload one. In the example above, we overload the myMethod method to work
for both int and double.