Java Lecture
Java Lecture
What is Java?
Java is created in 1991 by James Gosling et al. of Sun
Microsystem
What is Java Technology?
A programming language
A development environment
An application Environment
Features of Java
Java Virtual Machine
An imaginary machine that is implemented by emulating
software on a real machine. The JVM provides the
hardware platform specifications to which you compile all
Java technology code
Features of Java
Garbage Collection
The garbage collection thread is responsible for freeing any
memory that can be freed. This happens automatically
during the lifetime of Java programs.
Features of Java
Code Security
The code security is attained in Java through the
implementation of its Java Runtime Environment (JRE). The
JRE runs code compiled for a JVM and performs class
loading through the class loader, code verification and
finally code execution.
Java Terminologies
JDK
Allow Developer to Create Java programs that
can be executed by Java Run time
Environment (JRE)
Java Terminologies
JRE
It is supplied by the Java 2 Software development Kit
(SDK) contains the complete set of class files for all
Java technology packages, which includes basic
languages classes, GUI component classes and so
on.
Java Terminologies
IDE
A software used by a developer so
they can easily code in a certain
programming language, IDE has
tons of features that makes
programming easier
Java Terminologies
Packages
This is like a folder in a directory where we can throw our related
codes in the same directory.
Java Comments
Comments are notes written to a code for documentation purposes.
Three types of comments:
C++ style comments – known as a single line comments. Its starts with //. All text after
// are treated as comments.
For example, consider the number 12. Its decimal representation is 12, while in
hexadecimal, it is 0xC, and in octal, it is equivalent to 014. it is default to the
data type of int which have 32 bit value.
Java literals
Floating-point literals represent decimals with fractional parts. It can be
expressed in a standard or scientific notations.
For example, 583.45 is in standard notation, while 5.8345e2 is in scientific
notations.
Floating-point literals default to the data type double which has 64-bit value. To
use a smaller precision 32-bit float just append “f” or “F” character.
Floating-point
float -32 bits
double -64 bits
Check Your Understanding
1. Notes written for a code for documentation purposes
A. Comments
B. Block
C. Statement
D. Code
2. Also known as multiline comments
A. C++ style
B. C style
C. Javadoc
D. Block
Check Your Understanding
1. A delimiter for C++ style Comments
A. { }
B. /* */
C. /** */
D. //
2. Statements are terminated by _____ delimiter
A. :
B. [ ]
C. ?
D. ;
Check Your Understanding
1. Character literals are enclosed in _____ delimiters
A. { }
B. ‘ ‘
C. “ “
D. [ ]
2. Blocks are terminated by _____ delimiter
A. { }
B. [ ]
C. ?
D. ;
Check Your Understanding
1. String literals are enclosed in _____ delimiters
A. { }
B. ‘ ‘
C. “ “
D. [ ]
2. Primitive data types that holds decimals and fractional value
A. String and char
B. float and double
C. int and long
D. Boolean
Variables
Variables is an item of data used to store state of objects.
A variable has a data type and a name. The data type indicates the type of
value that a variable can hold. The variable names must follow the rules for
identifiers.
To Declare and Initialize Variables: Tips:
1. It is always good to initialize
<data type><name>[=initial value]; variables
2. Use descriptive name for
Note: Values enclosed in <> are required values, while those values enclosed in [] are optional. variables
3. Declare one variable per line
of code
Outputting Variable Data
System.out.print(); System.out.println();
System.out.print(“Hello”); System.out.println(“Hello”);
System.out.print(“World”), System.out.println(“World”);
Output: Output:
Variables with primitive data types, Variables that stores the address in
they store data in the actual the memory location
memory location of where the It points to another memory location
variable is of where the actual data is.
Java Operators
Arithmetic Operators
Relational Operators
Increment and Decrement Operators
Logical Operators
Conditional Operators
Arithmetic Operators
The basic difference between && and & operators is that && support short-circuit evaluations (or
partial evaluations), while & doesn’t.
Syntax:
exp1 && exp2
exp1 & exp2
x1 x2 Result
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
Check your Understanding
Evaluate the following code and determine its output:
The basic difference between || and | operators is that | support short-circuit evaluations (or
partial evaluations), while | doesn’t.
Syntax:
exp1 || exp2
exp1 | exp2
x1 x2 Result
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
Check your Understanding
Evaluate the following code and determine its output:
The result of an exclusive OR operation is TRUE, if and only if one operand is true and the other is
false. Note that both operands must always be evaluated in order to calculate the result of an
exclusive OR.
Syntax:
exp1 ^ exp2
x1 x2 Result
TRUE TRUE FALSE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
Check your Understanding
Evaluate the following code and determine its output:
The logical NOT takes in one argument, wherein that argument can be an expression, variable or
constant.
Syntax:
!val1
x1 Result
TRUE FALSE
FALSE TRUE
Conditional Operators (?:)
The conditional operator ?: is a ternary operator. This means that it takes in three
arguments that together form a conditional expression.
Syntax:
exp1?exp2:exp3
Wherein exp1 is a boolean expression whose result must either be true or false.
If exp1 is true, exp2 is the value returned. If it is false, then exp3 is returned.
2. WAP that will return the largest number among the three given integers.
Operator Precedence
Operator precedence defines the compiler’s order of evaluation of operators so as to come up with an
unambigous result.
The following is the list of Java operators from hihgest to lowest precedence.
• . [] ()
• ++ -- ! ~
• / %
• << >> >>> <<<
• < > <= >=
• == !=
• & |
• ^
• &&
• ||
• ?:
• =
The highest precedence is on the top row and the lowest precendence is on the bottom row
Getting Input from the keyboard(BufferedReader)
Package gettingIn;
import java.io.*;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.io.IOException;
public class gettingIn{
public static void main(String[]args){
BufferedReader dataIn = new BufferedReader(newInputStreamReader(System.in));
String name=“”;
System.out.println(“Please Enter your Name”
try{
name=dataIn.readLine();
}catch (IOException e){
System.println(“Error!”);
}
System.out.println(“Hello” +name);
}
}
Getting Input (BufferedReader)
• Create a program that will ask for the password from the user, if the password
is equals to "COMPUTER" it will display "ACESS GRANTED" otherwise
"ACCESS DENIED
• WAP that will ask three integer from the user and will return the largest
number.
Getting Input from the keyboard (Scanner)
• The Scanner class is used to get user input, and it is found in
the java.util package.
• To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation. In our example,
we will use the nextLine() method, which is used to read Strings:
Example #1
if (20 > 18) { System.out.println("20 is greater than 18"); }
Example #2
int x = 20;
int y = 18;
if (x < y) { System.out.println("x is greater than y"); }
Control Structures (Decision Control Structure)
The if-else Statement
Use the else statement to specify a block of code to be executed if the condition
is false
Syntax
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 }
Example
int time = 20; if (time < 18) {
System.out.println("Good day."); }
else { System.out.println("Good evening.");
}
Control Structures (Decision Control Structure)
The if-else-if statement
Use the else if statement to specify a new condition if the first condition is false
Syntax
if (condition1) {
// block of code to be executed if 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 is false and condition2 is false }
Control Structures (Decision Control Structure)
int time = 22;
if (time < 10) {
System.out.println("Good morning."); }
else if (time < 20) {
System.out.println("Good day."); }
else {
System.out.println("Good evening."); }
Control Structures (Decision Control Structure)
• WAP that will ask for the password from the user and will display “ACCESS
GRANTED” if the password is equal to “COMPUTER” otherwise it will display
“ACCESS DENIED”
• WAP that will evaluate the letter input by the user and will display “VOWEL” or
“CONSONANT” at runtime.
• WAP that will as for the age of the user and will display “YOU ARE ALLOWED
to VOTE” if the age is greater than or equal to 18.