Professional Documents
Culture Documents
1.java Programming - Module 1
1.java Programming - Module 1
Module 1
1 Evolution of Java
2 Java Architecture
3 Langugage Basics
4
Flow Control
Objectives
• The origin of Java can be traced back to the fall of 1992, and was initially
called Oak
• Oak was renamed as Java in 1995
Design Goal
• The goal was to move away from platform and OS-specific compilers that
would compile source for a particular target platform to a language that
would be portable, and platform-independent
step2 Java
Bytecodes Just in step5
Java
move locally Time
Java Compiler Interpreter Java
or through Compiler
Virtual
network
machine
Runtime System
Java
Bytecode
(.class ) Operating System
Hardware
Java Architecture (Contd.).
Step1:
Create a java source code with .java extension
Step2:
Compile the source code using java compiler, which will create bytecode file
with .class extension
Step3:
Class loader reads both the user defined and library classes into the memory
for execution
Java Architecture (Contd.).
Step4:
Bytecode verifier validates all the bytecodes are valid and do not violate Java’s
security restrictions
Step5:
JVM reads bytecodes and translates into machine code for execution. While
execution of the program the code will interact to the operating system and
hardware
The 5 phases of Java Programs
Java programs can typically be developed in five stages:
1. Edit
Use an editor to type Java program (Welcome.java)
2. Compile
• Use a compiler to translate Java program into an intermediate
language called bytecodes, understood by Java interpreter (javac
Welcome.java)
4. Verify
Use a Bytecode verifier to make sure bytecodes are valid and do not
violate security restrictions
5. Execute
• Java Virtual Machine (JVM) uses a combination of interpretation and
just-in-time compilation to translate bytecodes into machine language
• Applications are run on user's machine, i.e. executed by interpreter
with java command (java Welcome)
The Java Architecture – The JVM
Execution Engine
The Java Architecture – The JVM (Contd.).
• Most modern languages are designed to be compiled
• Once the Java runtime package exists for a given system, any
Java program can run on it
• The JVM will differ from platform to platform, and is, platform-
specific
•It also keeps a tab on the code that is running and identifies only
the heavily used areas
•The JVM compiles these heavily used areas of code into native
code
• The class loader loads a compiled Java source file (.class files
represented as bytecode) into the Java Virtual Machine (JVM)
• The Java class file is a binary file that has the capability to run on
any platform
Quiz
1. Write the correct order of the Java program execution
A. Class Loader
B. Interpretation
C. Compilation
D. Byte Code Verification
E. Java Source Code
F. Execution
4. Click
1. Select Path
on OK
2. Click on Edit
5. Click
on OK
CLASSPATH
5. Click
on OK
OurAfirst
Simple Java Program
Java Program:
Sample.java
class A {
void m1() { }
}
class B {
void m2() { }
}
class C {
void m3() { }
}
Quiz
What will be the result if you try to compile and execute the following
program ?
Sample.java
class Sample {
public static void main() {
System.out.println(“Welcome”);
}
}
a. Compilation Error
b. Runtime Error
c. The program compiles and executes successfully but prints nothing.
d. It will print “Welcome”
Accessing command line arguments
class Simple {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
When we compile the above code successfully and execute
it as Java Simple Wipro, the output will be
Wipro
Accessing numeric command line arguments
class Simple {
public static void main(String[] args) {
int i1 = Integer.parseInt(args[0]);
int i2 = Integer.parseInt(args[1]);
System.out.println(i1+i2);
}
}
When we compile the above code successfully and execute it as
Java Simple 10 20, the output will be
30
Finding length of an Array
6
And if you execute it as java FindLength Tom John Lee, the result
will be
3
Quiz
What will be the result if you try to compile and execute the following
code without passing any command line argument?
class Sample {
public static void main(String [ ] args) {
int len = args.length;
System.out.println(len);
}
}
a. Compilation Error
b. Runtime Error
c. The program compiles and executes successfully but prints nothing.
d. The program compiles and executes successfully & prints 0.
The Java API
• An application programming interface(API), in the framework of java, is a
collection of prewritten packages, classes, and interfaces with their
respective methods, fields and constructors
• The Java API, included with the JDK, describes the function of each of its
components
• Simple
• Object-Oriented
• Supports encapsulation, inheritance, abstraction, and polymorphism
• Distributed
• Libraries for network programming
• Remote Method Invocation
• Architecture neutral
• Java Bytecodes are interpreted by the JVM
The Java Buzzwords (Contd.).
• Secure
• Difficult to break Java security mechanisms
• Java Bytecode verification
• Signed Applets
• Portable
• Primitive data type sizes and their arithmetic behavior specified by the
language
• Libraries define portable interfaces
• Multithreaded
• Threads are easy to create and use
Language Basics
• Keywords
• Data Types
• Variables
• Operators
• Conditional Statements
• Loops
Java Keywords
char 4
byte 2
int 1
double 8
Output:
/* Example to understand Arithmetic operator */a + b = 13
a-b=7
class Sample{ a * b = 30
public static void main(String[] args){ a/b=3
int a = 10; a%b=1
int b = 3;
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("a / b = " + (a / b) );
System.out.println("a % b = " + (a % b) );
}
}
Unary Operators
The following table lists the unary operators
Output:
/* Example for Unary Operators*/
a++ = 11
class Sample{ b-- = 19
public static void main(String args[]) {
int a = 10;
int b = 20;
== Two values are checked, and if equal, then the condition becomes true (A == B)
!= Two values are checked to determine whether they are equal or not, and if not (A != B)
equal, then the condition becomes true
> Two values are checked and if the value on the left is greater than the value on (A > B)
the right, then the condition becomes true.
< Two values are checked and if the value on the left is less than the value on the (A < B)
right, then the condition becomes true
>= Two values are checked and if the value on the left is greater than equal to the (A >= B)
value on the right, then the condition becomes true
<= Two values are checked and if the value on the left is less than equal to the value (A <= B)
on the right, then the condition becomes true
Relational Operators - Example
/* Example to understand Relational operator */
Output:
class Sample{
public static void main(String[] args){ a == b = false
a != b = true
int a = 10; a > b = false
int b = 20; a < b = true
System.out.println("a == b = " + (a == b) ); b >= a = true
System.out.println("a != b = " + (a != b) ); b <= a = false
&& This is known as Logical AND & it combines two variables or (A && B) is false
expressions and if and only if both the operands are true, then it
will return true
! Called Logical NOT Operator. It reverses the value of a Boolean !(A && B) is true
expression
Logical Operators - Example
Output:
/* Example to understand logical operator */
a && b = false
a || b = true
class Sample{ !(a && b) = true
public static void main(String[] args){
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b) );
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b) );
}
}
Simple Assignment Operator
Which assigns right hand side value to left hand side variable
Ex:
int a;
a = 10;
Shift Operators << and >>
int x = 16;
x = x >> 3;
When we apply the right shift operator >>, the value gets divided
by 2 to the power of number specified after the operator. In
this case, we have 3 as the value after the right shift operator.
So, 16 will be divided by the value 2 to the power of 3, which is
8.
The result is 2.
Right Shift Operator >> (Contd.).
When we represent 16 in binary form, we will get the following binary
value :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
When we apply >> which is the right shift operator, the bit represented
by 1 moves by 3 positions to the right(represented by the number after
the right shift operator).
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
10 01 1
00 010 00
x=2
Right
class Shift Operator
ShiftExample1 { >> Demo
public static void main(String[] args) {
int x = 16;
System.out.println("The original value of x is "+x);
x = x >> 3;
System.out.println("After using >> 3, the new value is "+x);
}
}
The original value of x is 16
After using >> 3, the new value is 2
Left
Let Shift Operator
us understand the use of<<
left shift operator with the following
example :
int x = 8;
x = x << 4;
When we apply the left shift operator <<, the value gets
multiplied by 2 to the power of number specified after the
operator. In this case, we have 4 as the value after the left shift
operator. So, 8 will be multiplied by the value 2 to the power of
4, which is 16.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
When we apply << which is the left shift operator, the bit represented by
1 moves by 4 positions to the left (represented by the number after the
right shift operator).
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 010 001 00
1 1
00 0
10 00 00 00
X=128
Left Shift Operator << Demo
class ShiftExample2 {
public static void main(String[] args) {
int x =8;
System.out.println("The original value of x is "+x);
x = x << 4;
System.out.println("After using << 4, the new value is
"+x);
} The original value of x is 8
After using << 4, the new value is 128
}
Bitwise Operators
The bitwise operators take two bit numbers, use
OR/AND to determine the result on a bit by bit basis.
7-> 0111
9-> 1001
-----------------
0001
-----------------
x & y results in 1.
Bitwise
class & Operator
BitwiseExample1 { Demo
public static void main(String[] args) {
int x = 7;
int y = 9;
int z = x & y;
System.out.println("z = "+z);
}
Output :
}
z=1
Bitwise | (inclusive OR) operator
The | operator will set the resulting bit to 1 if either one of them is
1. It will return 0 only if both the bits are 0.
Example :
int x = 5;
int y = 9;
What will be x | y ?
5-> 0101
9-> 1001
-----------------
1101
-----------------
x | y results in 13.
Bitwise
class | Operator
BitwiseExample2 { Demo
public static void main(String[] args) {
int x = 5;
int y = 9;
int z = x | y;
System.out.println("z = "+z);
}
Output :
}
z = 13
TheBitwise
^ operator^ compares
(exclusive OR)
two bits operator
to check whether these bits are
different. If they are different, the result is 1.Otherwise, the result
is 0. This operator is also known as XOR operator.
Example :
int x = 5;
int y = 9;
What will be x ^ y ?
5-> 0101
9-> 1001
-----------------
1100
-----------------
x ^ y results in 12.
Bitwise
class ^ Operator
BitwiseExample3 { Demo
public static void main(String[] args) {
int x = 5;
int y = 9;
int z = x ^ y;
System.out.println("z = "+z);
}
Output :
}
z = 12
Quiz
• Control statements are statements which alter the normal execution flow of
a program
if while break
if – else for continue
switch do – while return
Simple if statement
syntax :
if(boolean expression)
{ Entry
statement–block;
} True
Next statement; boolean expression ?
statement–block
False
Next Statement
If - Example
Syntax, Entry
• Syntax:
if (condition1)
{
statement-1
}
….
else if(conditio-n)
{
statement-n
}
else
{
default statement
}
next statement
else - if Example
/* program to print seasons for a month input using else--if */
If args[0] is 6 then the Output:
public class ElseIfDemo { Summer
public static void main(String[] args) {
int month = Integer.parseInt(args[0]);
if(month == 12 || month == 1 || month == 2)
System.out.println("Winter");
else if(month == 3 || month == 4 || month == 5)
System.out.println("Spring");
else if(month == 6 || month == 7 || month == 8)
System.out.println("Summer");
else if(month == 9 || month == 10 || month == 11)
System.out.println("Autumn");
else
System.out.println("invalid month");
}
}
Switch Case
• The switch-case conditional construct is a more structured way of testing for
multiple conditions rather than resorting to a multiple if statement
Syntax:
switch (expression) {
case value-1 : case-1 block
break;
case value-2 : case-2 block
break;
default : default block
break;
}
statement-x;
Switch Case - Example
/* This is an example of a switch case statement*/
public class SwitchDemo {
public static void main(String[] args) { If args[0] is 6 then
int weekday = Integer.parseInt(args[0]); the Output:
switch(weekday) { Friday
case 1: System.out.println(“ Sunday");
break;
case 2: System.out.println(“ Monday");
break;
case 3: System.out.println(“ Tuesday");
break;
case 4: System.out.println(“ Wednesday");
break;
case 5: System.out.println(“ Thursday");
break;
case 6: System.out.println(“ Friday");
break;
case 7: System.out.println(“ Saturday");
break;
default: System.out.println(“ Invalid day");
} } }
While loop
• Syntax
false
while(condition) Boolean Condition
{
true
Body of the loop
}
Body of the loop
true
while loop – Example
do
{
Body of the loop
Boolean expression
} while(boolean expression); false
true
do…while loop – Example
• Syntax
for(initialization;condition;increment/decrement)
{
Body of the loop
}
For loop - Example
• Syntax:
for(declaration : expression) {
Body of loop
}
Enhanced for loop - Example
• When the loops are nested, the break will only terminate the
corresponding loop body
break - Example
/* This is an example of a break statement */
class Sample{
public static void main(String[]args){
boolean b = true;
if(b){
System.out.println(" if block ");
}
else{
System.out.println(“ else block
"); }
}
}
Quiz
• What will be the result, if we try to compile and execute the
following codes
1. class Sample{
public static void main(String[] args){
while(false){
System.out.println("while loop");
}}}
2. class Sample{
public static void main(String[] args){
for(;;){
System.out.println("For loop");
}}}
Summary
In this session, you were able to :
• Learn about Evolution of Java and forces that shaped it
• Understand Java Architecture along with JVM Concepts
• Write the first Java Program with understanding of
Language Basics and Keywords
• Learn about how control the program execution flow
using branching and looping constructs
References
1. Oracle (2012). Java Tutorials: Primitive Data Types. Retrieved on May 12,
2012, from, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/
datatypes.html
2. Oracle (2012). Java Tutorials: Assignment, Arithmetic, and Unary
Operators. Retrieved on May 12, 2012, from, http://docs.oracle.com/
javase/tutorial/java/nutsandbolts/op1.html
3. Schildt, H. Java: The Complete Reference. J2SETM. Ed 5. New Delhi:
McGraw Hill-Osborne, 2005.
4. Tutorialpoint TP (2012). C-Operator Types. Retrieved on May 12, 2012,
from, http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
Thank You