Java Programming_Module 1

57
Java Programming Evolution of Java Module 1

Transcript of Java Programming_Module 1

Page 1: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 1/57

Java Programming

Evolution of Java

Module 1

Page 2: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 2/57

Evolution of Java

Agenda

Java Architecture

Language Basics

Arrays

Page 3: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 3/57

Objectives

At the end of this module, you will be 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 ofLanguage Basics and Keywords

• Understand Array declaration, definition and access of

array elements

Page 4: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 4/57

Evolution of Java

Page 5: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 5/57

Page 6: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 6/57

Design Goal

• Java was originally meant to be a platform-neutral language for

embedded software in devices

• The goal was to move away from platform and OS-specific compilers

that would compile source for a particular target platform to a languagethat would be portable, and platform-independent

• The language could be used to produce platform-neutral code

Page 7: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 7/57

Java Architecture

Page 8: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 8/57

Java ArchitectureRun-time EnvironmentCompile-time

Environment

JavaBytecodes

move locallyor through

network

JavaSource(.java)

Java Compiler

JavaBytecode(.class )

JavaInterpreter

Just inTime

Compiler

Runtime System

JavaClassLibraries

Operating System

Hardware

JavaVirtualmachine

Class Loader

BytecodeVerifier

step1

step5

step4

step3

step2

Page 9: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 9/57

Java Architecture (Contd.).

Step1:

Create a java source code with .java extension

Step2:Compile the source code using java compiler, which will createbytecode file with .class extension

Step3:

Class loader reads both the user defined and library classes into thememory for execution

Page 10: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 10/57

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

Page 11: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 11/57

The 5 phases of Java Programs

Java programs can typically be developed in five stages:

1. EditUse 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 )

• Use a compiler to create .class file, containing bytecodes

(Welcome.class )

3. LoadingUse a class loader to read bytecodes from .class file intomemory

Page 12: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 12/57

The 5 phases of Java Programs (Contd.).

4. Verify

Use a Bytecode verifier to make sure bytecodes are valid and

do not violate security restrictions5. 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)

Page 13: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 13/57

The Java Architecture – The JVM

Program ClassFiles

ClassLoader

Java APIclasses

Execution Engine

Page 14: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 14/57

The Java Architecture – The JVM (Contd.).

• Most modern languages are designed to be compiled

• Compilation is a one-time exercise and executes faster

• Execution of compiled code over the Internet animpossibility

• Executable code always generated to a CPU-OScombination

• Interpreting a Java program into byte code facilitates itsexecution in a wide variety of environments

Page 15: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 15/57

The Java Architecture – The JVM (Contd.).

• Only the Java Virtual Machine (JVM) needs to be

implemented for each platform

• 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

• All versions of JVM interpret the same Java byte code

Page 16: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 16/57

Page 17: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 17/57

The Java Architecture – The Adaptive optimizer

• Another type of execution engine is an adaptive optimizer

• The virtual machine starts by interpreting bytecodes

• 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 intonative code

• The rest of the code, which is not heavily used continues tobe interpreted and executed

Page 18: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 18/57

The Java Architecture - The Class Loader

• The class loader is that part of the VM that is important

from: – A security standpoint – Network mobility

• The class loader loads a compiled Java source file ( .class files represented as bytecode) into the Java VirtualMachine (JVM)

• The bootstrap class loader is responsible for loading theclasses, programmer defined classes as well as Java's APIclasses

Page 19: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 19/57

The Java Architecture - The Java .class file

• The Java class file is designed for

– platform independence – network mobility

• The class file is compiled to a target JVM, but independentof underlying host platforms

• The Java class file is a binary file that has the capabilityto run on any platform

Page 20: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 20/57

1. Write the correct order of the Java program execution A. Class LoaderB. InterpretationC. CompilationD. Byte Code VerificationE. Java Source CodeF. Execution

2. A Sample.Java file contains, class A, B and C. How many.class files will be created after compiling Sample.java?

Quiz

Page 21: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 21/57

Langugage Basics

Page 22: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 22/57

A Simple Java Program

A First Java Program:

public class Welcome {public static void main(String args[]) {

System.out.println(“Welcome to JavaProgramming”); }

}

• Create source file: Welcome.java• Compile : javac Welcome.java• Execute : java Welcome

Page 23: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 23/57

The Java API

• An application programming interface(API), in theframework of java, is a collection of prewritten packages,classes, and interfaces with their respective methods, fieldsand constructors

•The Java API, included with the JDK, describes thefunction of each of its components

• In Java programming, many of these components are pre-created and commonly used

Page 24: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 24/57

The Java Buzzwords

• 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

Page 25: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 25/57

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

Page 26: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 26/57

Language Basics

• Keywords

• Data Types• Variables• Operators• Conditional Statements• Loops

Page 27: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 27/57

Java Keywords

abstract continue for new switch

assert default goto package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp volatile

const float native super while

Page 28: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 28/57

Primitive Data Types

DataType

Size(in bits) Minimum Range Maximum Range Default Value

(for fields)

byte 8 -128 +127 0

short 16 -32768 +32767 0

int 32 -2147483648 +2147483647 0

long 64 -9223372036854775808 +9223372036854775807 0L

float 32 1.40E-45 3.40282346638528860e+38 0.0f

double 64 4.94065645841246544e-324d 1.79769313486231570e+308d 0.0d

char 16 0 to 65,535 '\u0000'

boolean 1 NA NA false

Page 29: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 29/57

Types of Variables

The Java programming language defines the following kinds of

Variables:• Local Variables

– Tied to a method

– Scope of a local variable is within the method

• Instance Variables (Non-static) – Tied to an object

– Scope of an instance variable is the whole class• Static Variables

– Tied to a class – Shared by all instances of a class

Page 30: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 30/57

1. Match the followig table:

2. What will be the output for the below code ?public class Sample{public static void main(String a[]){int i_val;System.out.println("i_val value: "+i_val);}}

Quiz

DATA TYPES SIZE(bytes)char 4

byte 2

int 1

double 8

Page 31: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 31/57

Operators

• Java provides a set of operators to manipulateoperations.

• Types of operators in java are, – Arithmetic Operators – Unary Operator

– Relational Operators – Logical Operators – Simple Assignment Operator – Bitwise Operators

Page 32: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 32/57

Arithmetic Operators

Operator Description Example

+ Addition A + B

- Subtraction A - B

* Multiplication A * B

/ Division A/B

% Modulus A%B

The following table lists the arithmetic operators

Page 33: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 33/57

Arithmetic Operators - Example

/* Example to understand Arithmetic operator */

class Sample{public static void main(String[] args){

int a = 10;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) );}}

Output:a + b = 13a - b = 7a * b = 30

a / b = 3a % b = 1

Page 34: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 34/57

Unary Operators

Operator Description Example

+ Unary plus operator +A

- Unary minus operator -A

++ Increment operator ++A or A++-- Decrement operator --A or A--

The following table lists the unary operators

Page 35: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 35/57

Unary Operator - Example

/* Example for Unary Operators*/class Sample{public static void main(String args[]) {

int a = 10;

int b = 20;

System. out.println("a++ = " + (++a) );

System. out.println("b-- = " + (--b) );

}}

Output:

a++ = 11b-- = 19

Page 36: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 36/57

Relational Operators

Operator Description Example

== 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 ifnot equal, then the condition becomes true

(A != B)

> Two values are checked and if the value on the left is greater than thevalue on the right, then the condition becomes true.

(A > B)

< Two values are checked and if the value on the left is less than the valueon the right, then the condition becomes true

(A < B)

>= Two values are checked and if the value on the left is greater than equal tothe value on the right, then the condition becomes true

(A >= B)

<= Two values are checked and if the value on the left is less than equal to thevalue on the right, then the condition becomes true

(A <= B)

The following table lists the relational operators

Page 37: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 37/57

Relational Operators - Example

/* Example to understand Relational operator */

class Sample{public static void main(String[] args){

int a = 10;

int b = 20;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("b >= a = " + (b >= a) );

System. out.println("b <= a = " + (b <= a) );

}}

Output:

a == b = falsea != b = truea > b = false

a < b = trueb >= a = trueb <= a = false

Page 38: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 38/57

Logical Operators

Operator Description Example

&& This is known as Logical AND & it combines two variables orexpressions and if and only if both the operands are true,then it will return true

(A && B) is false

|| This is known as Logical OR & it combines two variables orexpressions and if either one is true or both the operandsare true, then it will return true

(A || B) is true

! Called Logical NOT Operator. It reverses the value of aBoolean expression

!(A && B) is true

The following table lists the logical operators

Page 39: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 39/57

Logical Operators - Example

/* Example to understand logical operator */

class Sample{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) );

}}

Output:

a && b = falsea || b = true

!(a && b) = true

Page 40: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 40/57

Simple Assignment Operator

= Simple assignment operator

Which assigns right hand side value to left hand sidevariable

Ex:

int a;a = 10;

Page 41: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 41/57

What will be the output for the below code ?public class Sample{

public static void main(){int i_val = 10, j_val = 20;

boolean chk;chk = i_val < j_val;System.out.println("chk value: "+chk);

}}

Quiz

Page 42: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 42/57

Arrays

Page 43: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 43/57

Arrays

• An array is a container object that holds a fixed number of values of a

single type

• When an array is created, the length of an array is fixed

• Array elements are automatically initialized with the default value of

their type, When an array is created• Array can be created using the new keyword

Ex:

int[] x = new int[5]; // defining an integer array

for 5 blocks

Page 44: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 44/57

Arrays (Contd.).

• Alternatively, we can create and initialize array as below format

int[] x = {10, 20, 30};int[] x = new int[]{10, 20, 30};

• Here the length of an array is determined by the number of values

provided between { and }

• The built-in length property determines the size of any array

Ex:

int[] x = new int[10];int x_len = x.length;

Page 45: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 45/57

Array - Example public class ArrayDemo {

public static void main(String[] args) {

int [] x; // declares an array of integersx = new int [5]; // allocates memory for 5integersx[0] = 11;X[4] = 22;System. out .println("Element at index 0: " + x[0]);

System. out .println("Element at index 1: " + x[1]);System. out .println("Element at index 4: " + x[4]);}

}

Output:Element at index 0: 11Element at index 1: 0Element at index 4: 22

Page 46: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 46/57

Array Bounds, Array Resizing

• Array subscripts begin with 0

• Can't access an array element beyond the range

• Can't resize an array. Can use the same reference variable to refer

new array

int x[] = new int [5];x= new int [10];

Page 47: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 47/57

Array copy

• To copy array elements from one array to another array, we can use

arraycopy static method from System class

• Syntax:public static void arraycopy(Object s,int

sIndex,Object d,int dIndex,int lngth)

• Ex:int source[] = {1, 2, 3, 4, 5, 6};

int dest[] = new int[10];

System.arraycopy(source,0, dest,0,source.length);

Page 48: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 48/57

Array Copy - Example

public class ArrayLengthDemo {

public static void main(String[] args) {// creates and initializes an array of integersint [] source = {100, 200, 300};// creates an integer array with 3 elementint [] dest = new int[3];// copying an elements from source to dest arraySystem.arrayCopy(source, 0, dest, 0, source.length);for ( int i =0; i < dest.length; i++)System. out .println("Element at index " + i + ": " +

dest[i]);}

}Output:Element at index 0: 100Element at index 1: 200Element at index 3: 300

T Di i l A

Page 49: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 49/57

Two-Dimensional Arrays• Two-dimensional arrays are arrays of arrays

• Initializing two-dimensional arrays:int[][] y = new int[3][3];

The 1 st dimension represent rows or number of one dimension, the 2 nd

dimension represent columns or number of elements in the each one

dimensions

• The curly braces { } may also be used to initialize two dimensional

arrays• Ex:

int[][] y = { {1,2,3}, {4,5,6}, {7,8,9} };

int[][] y = new int[3][] { {1,2,3}, {4,5,6}, {7,8,9} };

T Di i l A (C d )

Page 50: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 50/57

Two-Dimensional Arrays (Contd.).

• You can initialize the row dimension without initializing the columns

but not vice versa

int[][] x = new int[3][];

int[][] x = new int[][3]; //error

• The length of the columns can vary for each row and initializenumber of columns in each row

• Ex1:int [][]x = new int [2][];x[0] = new int[5];x[1] = new int [3]; 0

1

x 0 0 0 0

0 0 0

T Di i l A (C d )

Page 51: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 51/57

Two-Dimensional Arrays (Contd.).

Ex2:

int [][]x = new int [3][];

x[0] = new int[]{0,1,2,3};

x[1] = new int []{0,1,2};

x[2] = new

int[]{0,1,2,3,4};

T Di i l A E l

Page 52: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 52/57

Two-Dimensional Array - Example

/* Program to under stand two-dimensional arrays */

class TwoDimDemo { public static void main(String[] args) {

int [][] x = new int [3][]; // initialize number of rows/* define number of columns in each row */x[0] = new int [3];

x[1] = new int [2];x[2] = new int [5];

/* print array elements */for ( int i=0; i < x.length; i++) {

for ( int j=0; j < x[i].length; j++) {x[i][j] = i;

System. out .print(x[i][j]);}System. out .println();

}}

}

Output:

0001122222

Q i

Page 53: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 53/57

• Select which of the following are valid array definition

1. int[] a;a = new int[5];

2. int a[] = new int[5]3. int a[5] = new int[5];

4. int a[] = {1,2,3};5. int[] a = new int[]{1,2,3};6. int[] a = new int[5]{1,2,3,4};

Quiz

Q i (C d )

Page 54: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 54/57

Predict the outputclass Sample{

public static void main(String[]args){

int[] a = new int[5]{1,2,3};for(int i : a)

System.out.println(i);}

}

Quiz (Contd.).

Summary

Page 55: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 55/57

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 Basicsand Keywords•

Understand Array declaration, definition and access of array elements

Summary

References

Page 56: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 56/57

References

1. Oracle (2012). Java Tutorials: Primitive Data Types. Retrieved on May12, 2012, from,http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

2. Oracle (2012). Java Tutorials: Assignment, Arithmetic, and UnaryOperators. 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

Page 57: Java Programming_Module 1

8/13/2019 Java Programming_Module 1

http://slidepdf.com/reader/full/java-programmingmodule-1 57/57

Thank You