1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written...

Post on 04-Jan-2016

240 views 6 download

Transcript of 1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written...

11

Programming Java

Java Basics

22

Java ProgramJava Program Java Application Program

Application Program written in general programming languag

e

Applet Program running in Web Browser Environment Can be viewed by appletviewer or Web browser wi

th JVM

33

Classes and ObjectsClasses and Objects Object

Memory Space to Define State and Operation Instance of Class

Class Template of Creating Object

44

The Java Class LibrariesThe Java Class Libraries

java.applet : Applet related java.awt : Abstract Window Toolkit java.awt.event : Event process from awt component java.awt.image : Image processing java.beans : JavaBeans Component java.io : File or Network I/O Support java.lang : Java Language Support java.net : Network related functions java.util : Utility function

55

Variables and AssignmentsVariables and Assignments

Variables Types

char 16bits Unicode character data boolean Boolean Variable byte 8 bits signed integer short 16 bits signed integer int 32 bits signed integer long 64 bits signed integer float 32 bits signed floating point number double 64 bits signed floating point number

66

Primitive data typesPrimitive data types

Byte 8 -27 27-1

Short 16 -215 215-1

Int 32 -231 231-1

Long 64

Float 32

Double 64

Boolean 1 0 1

Char 16

77

VariablesVariables

Variables: Name Type Value

Naming: May contain numbers,underscore,dollar sign,

or letters Can not start with number Can be any length Reserved keywords Case sensitive

88

Variables and AssignmentsVariables and Assignments

int num = 100;

long m = 21234234L

double type : .5 0.8 9e-2 -9.3e-5

float type : 1.5e-3f;

Variable Declarationtype varName;

Value assignments

varName = value;

float x, y, x;

99

Type Conversions in ExpressionType Conversions in Expression

char ch; int i; float f; double outcome;

ch = ‘0’; i = 10; f = 10.2f;

outcome = ch * i / f;

intfloat

double

1010

Type Conversions in AssignmentType Conversions in Assignment

Widening Conversion byte b = 127; int i; i = b; Narrowing Conversion byte b; int i = 258; b = (byte) i; Wrong Conversion byte b; int i = 127; b = i; Right Conversion (But data may be lost) byte b; int i = 127; b = (byte) i;

Type Casting

1111

AssignmentAssignment

= Example:int n;n = 10;orint n = 10; //same

1212

AssignmentAssignment

+= -= *= /= %=

1313

Arithmetic OperatorsArithmetic Operators

Operators + - * / % += -= *= /= %= ++ --

5 / 2 2 Why isn’t it 2.5 ?

5 % 2 1

4 / 2 2

4 % 2 0 count * num + 88 / val – 19 % count j += 6; j = j + 6;

1414

Boolean ExpressionsBoolean Expressions

boolean bb will be either true (1) or false (0)

Logical operations: !(not), && (and) || (or) boolean a,b;

a = true;b = false;System.out.println (“a && b is “ + (a && b));

1515

Relational OperatorsRelational Operators

== equality != inequality > greater than < less than >= greater than or equal to <= less than or equal to

1616

IncrementingIncrementing

Increment and Decrement i++ equivalent to i = i + 1; Can also do ++i, which uses i before incrementing

it. Decrementing: i--;

1717

The if - branching statementThe if - branching statement

if ( x < y) {x = y;}

if ( x < y ) {x = y;

}else { x = 88;}

1818

If/ElseIf/Else

if (logic condition) {something}else if (logic condition) { something}else {something else}

1919

Nested IFNested IF

if ( x < 0 ) {System.out.println( “ x is negative “ );}

else {if ( x > 0 ) {

System.out.println ( “x is positive” );}//end if x > 0else {System.out.println ( “x is zero “ );}

} //end else x >=0

2020

2121

Switch/CaseSwitch/Case

Switch(variable){case(1): something;

break;case(23): something;

break;default: something;}

2222

2323

CommentsComments

Single Line Comment int i = 10 ; // i is counter

Multiple Line Comment

/*

Some comments

*/ Documentation Comment

/**

Documentation Comment

*/

Using “javadoc” Tool,

make document

2424

Arrays ( One Dimensional)Arrays ( One Dimensional)

type VarName[]

Definition of One Dimensional Array

int ia[];

varName = new type[size]

Assign Range to One Dimensional Array

ia = new int[10];

type varName[] = new type[size]

Declaration of One Dimensional Array with Range

int ia[] = new int[10];

2525

Arrays ( One Dimensional)Arrays ( One Dimensional)

varName.length

Number of elements in Array

Type varName[] = {e0, … , en};

Initialization of One Dimensional Array

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

int k[];

K = j;

Example 1.15

2626

Arrays ( Arrays ( Multi-Multi-Dimensional)Dimensional)

type VarName[][];

Definition of Two Dimensional Array

float fa[][];

varName = new type[size1][size2];

Assign Range to Two Dimensional Array

fa = new float[2][3];

type varName[][] = new type[size1][size2];

Declaration of Two Dimensional Array with Range

float fa[] = new float[2][3];

2727

Arrays ( Arrays ( TwoTwo Dimensional) Dimensional)

varName.length

Number of elements in Multi-Dimensional Array

Type varName[][] = {{e00, … , e0n}, {e10,…,e1y}, {e20, …, e2z}};

Initialization of Multi-Dimensional Array

Example 1.16

varName[index].length

Number of elements in Each elements of Multi-Dimensional Array

Number of Raw

2828

Java KeywordsJava Keywords

50 Java Keywordsabstract double int superboolean else interface switchbreak extends long synchronizedbyte final native thiscase finally new throwcatch float package throwschar for private transient*class goto* protected tryconst* if public voidcontinue implements return volatiledefault import short whiledo instanceof static strictfp

assert (New in 1.5) enum (New in 1.5)The “assert” is recognized as keyword in JDK1.4 compiler, but we could not

use it.It can be used it from 1.5.

2929

First ApplicationFirst Application

/***Hello World, first application, only output.*/import java.io.*;

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

System.out.println(“Hello World\n”);} //end main

}//end class

3030

How to get it runningHow to get it running

Text in hello.java file Why?

To compile: javac hello.java

To run: java hello

3131

Notice:Notice:

Java is CASE SENSITIVE!! Whitespace is ignored by compiler Whitespace makes things easier to read…hence it

affects your grade File name has to be the same as class name in

file. Need to import necessary class definitions