Introduction to java (revised)

75
Hello Everyone! Welcome to TECHFORT’s Training

Transcript of Introduction to java (revised)

Page 1: Introduction to java (revised)

Hello Everyone!Welcome to TECHFORT’s

Training

Page 2: Introduction to java (revised)

Hello Everyone!

JAVASujit Majety,

Technical Head / Corporate Trainer

Page 3: Introduction to java (revised)

Day 1 AgendaIntroduction to Java

Page 4: Introduction to java (revised)

• Introduction

• Programming Paradigm

• History

• Features

• Sample Program

• Execution Steps

• JVM

• Data types

• Objects and Classes

• Variables

• Flow Control Statements

Java Introduction

Day 1 : Agenda

Page 5: Introduction to java (revised)

IntroductionLets start our exiting journey from now.

Page 6: Introduction to java (revised)

Introduction

•Java is one of the most powerful programming languages.

•Java is cross-platform, object-oriented, network-based,

reliable programming language.

•The slogan of java is “write once and run anywhere”, this

allowed java to gain enormous popularity. Its rapid

ascension and wide acceptance can be traced to its design

and programming features.

•The name “JAVA” came from the name of the coffee seed.

•JAVA released to market in different platforms.

Page 7: Introduction to java (revised)

Java Platforms

•A java platform or edition consists of a JRE with a specific set

of libraries for a specific application environment.

PLATFORM KEY CHARACTERISTICS

J2SE Core Java platform designed for applications running on desktop PCs.

J2EE Design, development, assembly, and deployment of business Applications.

J2ME Design of small, embedded applications in consumer devices (such as

mobile phones).

Java Card Design of small Java applications that run on smart cards.

JavaFX Design of Rich Internet Applications (RIAs).

Page 8: Introduction to java (revised)

Programming ParadigmA way of conceptualizing what it means to perform computation

Page 9: Introduction to java (revised)

Programming Paradigms

Procedure Oriented

• More emphasis on algorithms.

• Programs divided into functions.

• Most functions share data, less security

to data.

• Data move around the system

between functions, any function can

change the data.

• Adding new code is very difficult.

• Code cannot be re-used.

• E.g. C, VB, Perl, Basic, Fortran

Object Oriented

•More emphasis on data.

•Programs are divided into objects.

•Functions operate on data are tied

together in the data structure.

•Data is secured; it is hidden to external

functions, can be accessed by functions

tied to it.

•Adding new code is very easy.

•Code can be re-used.

•E.g. CPP, Java, VB.Net, C#.Net

Page 10: Introduction to java (revised)

HistoryWhere did this start?

Page 11: Introduction to java (revised)

History

•Developed by a team led by James Gosling at Sun

Microsystems.

•Sun Microsystems was a company best known for its

workstations.

•Java was originally OAK, designed for use in embedded

consumer electronic application in 1991.

•OAK was redesigned for developing internet applications

and renamed JAVA in 1995.

•Java is inherently object-oriented.

Page 12: Introduction to java (revised)

History

Major Release Date Key Characteristics

JDK 1.0 1996 First stable version of Java

JDK 1.1 1997 Inner classes; Java beans; JDBC; RMI; Just in Time (JIT) compiler for Windows

platforms

JDK 1.2 1998 Swing classes; Java IDL; Collections

JDK 1.3 2000 Java platform debugger architecture (JPDA); JavaSound; HotSpot JVM

JDK 1.4 2002 Regular expressions; IPv6 support; image I/O API; non-blocking I/O (nio); XML parser

and XSLT processor

JDK 5.0 2004 Generics; annotations; autoboxing; enumerations; varargs; for each loop

JAVA SE 6 2006 Improved GUI support; improved web service support

JAVA SE 7 2011 New file I/O capabilities; support for new network protocols

JAVA SE 8 2014 Lambda expressions; new date and time API

JAVA SE 9 2016

(expected)Money and currency API

Page 13: Introduction to java (revised)

Features

Page 14: Introduction to java (revised)

Features

There are eleven promising features in java

• Simple (no pointers, interfaces, rich set of API, friendly syntaxes)

• Secure (many number of security mechanisms to block stray programs)

• Portable (can run on any operating system)

• Robust (memory management, exceptional handling)

• Distributed (runs on multiple servers and can be accessed by no of clients)

• Interpreted (both compiled and interpreted)

• Multi Threading (multiple flow of controls)

• Dynamic (is always open for updating)

• Architecture Neutral (can run on any processor)

• High Performance (garbage collector, leaves resources wile waiting for inputs)

• Object oriented (everything is written in class)

Page 15: Introduction to java (revised)

Our First Java Program

Page 16: Introduction to java (revised)

First Java Program

public class Test {

public static void main(String[] args) {

System.out.println("HELLO WORLD");

}

}

javac Test.java

Java Test

HELLO WORLD

Page 17: Introduction to java (revised)

Execution Steps

Page 18: Introduction to java (revised)

Execution Steps

Text editor

Java

Virtual

Machine

Java

Compiler

Source Code

(.java)

Byte code(.class)

Output on

Console

Saves Java statements

Produces

Result in

Page 19: Introduction to java (revised)

JVM

Page 20: Introduction to java (revised)

JVM

•JVM stands for Java Virtual Machine.

• It is abstract machine.

• It is a specification that provides runtime environment in which

java byte code can be executed.

•JVM’s are available for many hardware and software platforms

(i.e. JVM is platform dependant).

•JVM performs four main tasks,

Loads code

Verifies code

Executes code

Provides runtime environment.

Page 21: Introduction to java (revised)

JVM

Classfile

Class LoaderSubsystem

RuntimeDataareas

Methodarea

Heap StackPC

Registers

Native Method

area

Execution Engine

JVM JIT

Page 22: Introduction to java (revised)

Data Types

Page 23: Introduction to java (revised)

Data Types

• Java supports UNICODE character set. Hence, each character is

represented in two bytes.

• In java every variable has a type, every expression has a type and all

assignments should be checked by the compiler for type compatibility.

Hence, java is treated as strongly typed language.

•We are having 8 primitive data types.

• These data types fall under 3 categories.

• Numeric Data types

• Character Data types

• Boolean Data types

Page 24: Introduction to java (revised)

Data Types

DATA TYPE SIZE RANGE DEFAULT VALUE

byte 8 bit -128 to 127 0

short 16 bit -32768 to 32767 0

int 32 bit -2,147,483,648 to 2,147,483, 647 0

long 64 bit -9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

0L

float 32 bit approximately ±3.40282347E+38F 0.0f

double 64 bit approximately

±1.79769313486231570E+308

0.0d

char 16 bit 0 to 65,536 (unsigned) ‘\u0000’

boolean not precisely

defined

true or false false

Page 25: Introduction to java (revised)

Object Oriented Programming

Page 26: Introduction to java (revised)

OOP

• Object oriented programming models the real world.

• Object oriented programming organizes a program around its data and set of well defined interfaces to that data.

• An object-oriented program can be characterized as data controlling access to

code.

• By switching the controlling entity to data, you can achieve several organizational

benefits.

• Object oriented programming, the first letter indicates object.

• What is an object?

Page 27: Introduction to java (revised)

Object

•An object is the basic unit of object orientation with behaviour and identity.

•An object is a real time entity. E.g.. Car, bike, book, me and you.

•It is a runtime entity that has a state and behaviour.

•The state of the object represents the data.

•The behaviour is represented by the methods.

•E.g. An object is created to represent a rabbit.It would have data: how hungry it is, where it is.And methods : eat, hide, run and dig.

Page 28: Introduction to java (revised)

Class

•A class is a way of binding the data and associated methods into a

single unit.

•A class can also be said as a blue print to create an object.

• If we want to develop a java program, then that should be developed

with respective of class only. i.e. without class there is no java program.

• Syntax:

class <class_name>

{

Variable declaration;

Methods definition;

};

Class Name

Data members/

properties/

Attributes

Behaviours/

Methods

Animal

Name,

Number of legs,

Colour

Eat(),

Walk(),

Sleep()

Page 29: Introduction to java (revised)

Example

class Bicycle {

int cadence = 0;

int speed = 0;

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:" +

cadence + " speed:" +

speed + " gear:" +

gear);

}

}

Page 30: Introduction to java (revised)

Variables

Page 31: Introduction to java (revised)

Variables

•A variable is a name given to a memory location.

•Generally there are three types of variables.

•Variable Types

Local Variables, are the variables that are declared inside a

method

Instance Variables, are the variables that are declared outside the

methods

Static Variables, are the variables that are declared outside the

methods with static keyword.

Page 32: Introduction to java (revised)

Flow Control Statements

Page 33: Introduction to java (revised)

Flow Control Statements

•A statement is an instruction that executes at runtime.

•Types of Statements

1. Sequential Statements

2. Conditional Statements

3. Iterative / Looping Statements

4. Branching Statements

Page 34: Introduction to java (revised)

Sequential Statements

Page 35: Introduction to java (revised)

Sequential Statements

•These statements execute

sequentially without

interruption one after the

other.

•All the statements we have

executed so far come under

sequential statements.

Page 36: Introduction to java (revised)

Conditional Statements

Page 37: Introduction to java (revised)

Conditional Statements

•These statements execute

based on a condition’s

success or failure.

•Types :

1. If

2. If-Else

3. Nested If-Else

4. IF-Else Ladder

Page 38: Introduction to java (revised)

If Condition

Page 39: Introduction to java (revised)

If Condition

•A simple if condition checks

for the condition and will

decide to process the

statements or not.

•Statement 1 and Statement 2

are conditional statements as

they depend on the

condition for executing.

Statement 1;

Statement 2;

Page 40: Introduction to java (revised)

If Syntax

if(condition){

statement 1;

statement 2;

statement 3;

}

Page 41: Introduction to java (revised)

If-Else Condition

Page 42: Introduction to java (revised)

If-Else Condition

•This construct consists of two

statements. (if and else).

• If the condition is success

some statements will get

executed, if failure other

statements will get executed. Statement 1;

Statement 2;

Statement 1;

Statement 2;

Page 43: Introduction to java (revised)

If-Else Syntax

if(condition){

statement 1;

statement 2;

}

else{

statement 3;

statement 4;

}

Page 44: Introduction to java (revised)

Nested If-Else

Page 45: Introduction to java (revised)

Nested If-Else Condition

• If else construct within either

the body of if statement or

the body of an else

statement or both. This

scenario is said to be Nested

If-else

Page 46: Introduction to java (revised)

Nested If-Else Syntax

if(condition){statement 1;

}else(condition2){

if(condition){statement 2;

}else{

statement 3;}

}

Page 47: Introduction to java (revised)

If-Else Ladder

Page 48: Introduction to java (revised)

If-Else Ladder

• If-Else ladder is used to check

multiple conditions.

•Here, every else is associated

with it’s previous if.

•The last else will go to work

only if all the conditions fail.

•Even in else if ladder the last

else is optional.

Page 49: Introduction to java (revised)

If-Else Ladder Syntax

if(condition){

statement 1;

}

else if(condition){

statement 3;

}

else{

statement 4;

}

Page 50: Introduction to java (revised)

Switch-Case Statement

Page 51: Introduction to java (revised)

Switch-Case Statement

•Java has a built-in multiple-

branch selection statement,

called switch, which

successively tests the value of

an expression against a list of

integer or character

constants

•When a match is found the

statements associated with

that constant are executed.

Page 52: Introduction to java (revised)

Switch-Case Syntax

switch(expression){

case constant1 : Statement1;

break;

case constant2 : Statement2;

break;

case constant3 : Statement3;

break;

default : Statement4;

}

Page 53: Introduction to java (revised)

Iterative Statements

Page 54: Introduction to java (revised)

Iterative Statements

•Some times there will be a need to

repeat the execution of certain

statements for certain number of times.

•This can be achieved using Loops.

1. While

2. Do-While

3. For

Page 55: Introduction to java (revised)

While Loop

Page 56: Introduction to java (revised)

While loop

•A while loop has a loop condition only

that is tested before each iteration to

decide whether to continue or

terminate the loop.

•Hence while loop is said to be entry

controlled loop.

Page 57: Introduction to java (revised)

While loop Syntax

while(condition){

Statement1;

Statement2;

Statement3;

---

---

---

}

Page 58: Introduction to java (revised)

Do-While Loop

Page 59: Introduction to java (revised)

Do-While loop

•A do-while loop has a loop condition

only that is tested after each iteration

to decide whether to continue or

terminate the loop.

•Hence, do-while loop is said to be exit

controlled loop.

•Even the condition fails every time the

contents will be executed at least

once.

Page 60: Introduction to java (revised)

Do-While Syntax

do{

Statement1;

Statement2;

---

---

---

}while(condition);

Page 61: Introduction to java (revised)

For Loop

Page 62: Introduction to java (revised)

For loop

•A For loop contains 3 parts.

• Initializer i.e. executed once at the start of

the loop.

• Loop Condition i.e. tested before iteration to

decide whether to continue or terminate the

loop.

• Increment/Decrement i.e. executed at the

end of each loop iteration.

•Hence, for loop is said to be entry controlled

loop.

Page 63: Introduction to java (revised)

For Loop Syntax

for(initializer; condition; increment/decrement){

Statement1;

Statement2;

---

---

---

---

}

Page 64: Introduction to java (revised)

Foreach Loop

Page 65: Introduction to java (revised)

Foreach Loop

•This is an advanced for loop, used to traverse array or

collection elements.

•The advantage of this for loop over traditional is it eliminates

the possibility of bugs and makes the code more readable.

•Syntax:

for( data_type variable : array|collection){}

•The variable will hold on to single value for each iteration

and stores the next value for the next iteration.

Page 66: Introduction to java (revised)

Jumping Control Statements

Page 67: Introduction to java (revised)

Jumping Control Statements

• Java provides support to some more control statements.

• These statements are used to move the control from one place of a

program to another place.

• These instructions are implemented by using the following statements.

1. Break

2. Continue

3. Go To

Page 68: Introduction to java (revised)

Break

• This keyword is used to stop the

loop and go to the statement

following he statement.

• Break can be written with or

without condition.

• Syntax:

break;

Page 69: Introduction to java (revised)

Continue

• Continue is a keyword used to skip

iterations.

• When continue is encountered in a loop

the control stops the current iteration i.e. it

will not execute the statements following

it.

• It can also be used with or without

condition.

• Syntax:

continue;

Page 70: Introduction to java (revised)

Go TO

• Goto is a specific control statement.

• It is used to jump the control from one part of the program to other part of the

program.

• It can be used for either as forward or as

backward.

• When forward goto is used the statements

between the goto statement and label

statement will be skipped.

• When backward goto is used the

statements between the goto and label

statements will be repeatedly executed

infinitely if not stopped with a condition.

Page 71: Introduction to java (revised)

Important Interview Questions

•What is the most widely used protocol on internet

•What is the difference between an executable file and .class file?

•Why java is suitable for internet?

•Why pointers are eliminated in java?

•What is the difference between a function and a method?

•Which part of the JVM will allocate the memory for java program?

•Which algorithm is used by garbage collector to remove the unused

variables or objects from memory?

Page 72: Introduction to java (revised)

Assignments

• Check the hello world program by removing each and every word.

• Shuffle the order of public static void main

• Try experimenting with print & println.

• Try giving different names for args.

• Try giving int args[] instead of String args[] in main.

• Give Different name for the filename and the class name

• Repeat the above problem with declaring class as public

• Copy the class file into different folder name and execute it.

• Write your own program, compile it and execute it.

• Try giving different data types ( the one you know like int x, double d ) in the println and see if it prints all of them.

• I have two Integers int x =1; and int x=2; I wish to print 12. find the ways to print.

• Repeat the same if I want to print given int x=1; double d=2.5; and print 12 instead of 12.5

Page 73: Introduction to java (revised)

Assignments

• Create new classes for each real-world object that you observed at the beginning of

this trail. Refer to the Bicycle class if you forget the required syntax.

• Create a small program that defines some fields. Try creating some illegal field names

and see what kind of error the compiler produces. Use the naming rules and

conventions as a guide.

• In the program you created in Exercise 1, try leaving the fields uninitialized and print

out their values. Try the same with a local variable and see what kind of compiler

errors you can produce. Becoming familiar with common compiler errors will make it

easier to recognize bugs in your code.

Page 74: Introduction to java (revised)

Thank You!

Sujit Majety, Technical Head/Software Trainer

Page 75: Introduction to java (revised)

SUJIT MAJETYTechnical Head / Corporate TrainerTechfort Software Solutions PVT. LTD.

Mail To : [email protected]

Call To : 040-40062299

View L ink : ht tp://t inyur l .com/techfort - java1

Feedback : ht tp ://t inyur l .com/mrcewfeedback