38

Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Embed Size (px)

Citation preview

Page 1: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010
Page 2: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Intorduction to Java ProgrammingSession 2

Course : T0974-Algorithm & Object-Oriented Programming IYear : 2010

Page 3: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara University

3

Learning Outcome

After taking this course, students should be expected to explain and discuss structure and

element of java programming

Page 4: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Outline Materi

• History of Java Programming Language• Characteristics• API, JDK, JRE• Java Platform• Simple Program using Java• Anatomy of Java Programming• Escape Sequence• Unicode

Page 5: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java History

• Developed by James Gosling from Sun Microsystem and sold to Oracle in 2009.

• Agustus 1991, It was named as Oak• Januari 1995, Change to Java• Based on “Write Once, Run Anywhere (WORA)”• Can be run in web browser using Applet

technology.

Page 6: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Characteristics• Simple• Object-oriented• Distributed• Interpreted• Robust• Secure• Architecture-neutral• Portable• High-performance• Multi-threaded• Dynamic

Page 7: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Application Program Interface (API)

• Collection of classes and interfaces for developing Java based application.

• Java API editions:– Java 2 Standard Edition (J2SE)

• Client-Side application, applet

– Java 2 Enterprise Edition (J2EE)• Servlet, JSP

– Java 2 Micro Edition (J2ME)• Mobile Phone

Page 8: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java Development Toolkit (JDK)• Collection of separated development tools to build and

test single Java programs to complex Java projects. • Java Development Tools is an IDE (Integrated

Development Tool) based application intended to build Java projects quickly.– JBuilder by Borland (www.borland.com)– NetBeans Open Source by Sun (www.netbeans.org)– Eclipse Open Source by IBM (www.eclipse.org)– Code Warrior by Metrowerks (www.metrowerks.com)– TextPad Editor (www.textpad.com)– JCreator LE (www.jcreator.com)– JEdit (www.jedit.org)

Page 9: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java Development Toolkit (JDK)

• Most commonly application programs in JDK– Compiler: javac

• Compiling a source code(.java) into bytecode(.class)

– Interpreter: java• Execute a bytecode (.class) as application.

– Debugger: jdb– Applet Viewer : appletviewer– Documentation: javadoc– Compressor: jar

Page 10: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java Runtime Environment (JRE)

• A Software that execute Java based application • Java Virtual Machine (JVM) is a collection of

programs to execute java bytecode on any computer platform.

• Java Bytecode is a collection of machine-specific instruction that be interpreted and executed by JVM. Its length is 1 byte per instruction.

Page 11: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java Platform

Page 12: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Simple Program

// This application program prints Welcome to Java!

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

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

}

Page 13: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Simple Program// This application program prints Welcome to Java!

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

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

}

Comments

Class name

Filename: Welcome.java

Class heading,

Main method signatureString

Page 14: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Simple Class

• Every Java Program should have 1 public class e.g. Welcome

• To execute a class, it should have a main mathod.

• System.out.println is a statement to print string into console.

Page 15: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Creating, Compiling, Executing

Source Code

Create/Modify Source Code

Compile Source Codee.g., javac Welcome.java

Bytecode

Run Bytecodee.g., java Welcome

Result

If compilation errors

If runtimeerrors orIncorrectresult

public class Welcome { Public static void main(String [] args) { System.out.println(“Welcome to Java!”); }}

Save on the disk

Source code (developed by the programmer)

…Method Welcome() 0 aload_0 …

Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String “Welcome to Java!”> 5 invokevirtual #4 8 return

Bytecode (generated by the compiler for JVM to read and interpret, not for you to understand)

Stored in the disk

Page 16: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Java Anatomy– Whitespace– Identifiers– Literal– Comments– Separators– Reserved words (keyword)– Modifiers– Statements– Blocks– Classes– Methods– The main method

Page 17: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Whitespace

• Free-Form Language• Doesn’t need Indentation rules• Can be made only 1 line of code. • Only one whitespace between token that doesn’t

have operator.• Example whitespace: space, tab, newline

Page 18: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Identifiers

• Use for naming a class, method, and variable• Uppercase/Lowercase character, number,

undercore, or $ sign can be used as identifiers.• Don’t be started using a number.• Case Sensitive• Example :

– AvgTemp, args, count, f4, $test, this_is_ok

Page 19: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Literals

• Constant number.• Should be Real Number, Decimal number,

Character, String, Boolean, depend on the type we are going to use.

• e.g.:– 100– 98.6– ‘X’– “This is a test”

Page 20: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Comments• Use notations,

– // --- line comment– /* … */ --paragraph comment

• Helping a programmer to communicate and understand a program.

• Usually as a internal program documentation• e.g:

// This application program prints Welcome to Java!

/* This application program prints Welcome to Java! */

/* This application program

prints Welcome to Java! */

Page 21: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Separators/Punctuations• Specific notations for spesific goals.• e.g:

Symbols Name Functions

( ) Parenthesis (-es plural)

List of parameters in methods

{ } Curly Brackets As a block or Array Initialization.

[ ] Brackets Array declaration.

; Semicolon Closing statement

, Comma Separator for variable or “for” statement.

. Period Package name and subpackage separator. Separating Variables or Methods with object/class.

Page 22: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Reserved Words

• keywords• Reserved words have a specific meaning to a compiler.• Couldn’t be used for another use in program.• e.g :

– classwhen compiler find a word class, then a word after the class is assumed as the name of the class.

– public, static, void

• Java is Case-Sensitive, so public is assumed as a keyword but Public isn’t.

Page 23: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Reserved Wordsabstract default goto * package this

assert do if private throw

boolean double implements protected throws

break else import public transient

byte enum instanceof return true

case extends int short try

catch false interface static void

char final long strictfp volatile

class finally native super while

const * float new switch

continue for null synchronized* reserved for next version of Java.

Page 24: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Modifiers

• Reserved Words.• Showing properties.• Showing data properties , methods, and classes.• Modifiers :

– public– static– private– final– abstract– protected

Page 25: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Statements

• Displaying its actions.• e.g.

– System.out.println(“Welcome to Java!”);Statement for showing a string into console.

• Every statement should be ended by semicolon.

Page 26: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Blocks

• Stated by using curly brackets ``{}”• Every class should have class block that

contains data and method(s).• Every method has a method block that

contains statement(s)• Block can possibility be written inside a

block (nested block).public class Test { public static void main(String[] args) { System.out.println(“Welcome to Java!”);

} }

MethodBlock

ClassBlock

Page 27: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Classes

• Core of Java Programming• Every Java Program should have 1 public class.• Inside a class should has a data and method(s)

(Encapsulation).

Page 28: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Methods

• Collection of Statements that to be encapsulated.• e.q. :

– System.out.printlnSystem.out standard output objectprintln method dalam objectThe results will be appear into command prompt.

Page 29: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Main Method

• Every Java application should have main methods.

• A place to start program execution.• JVM execute an application through main method.• e.g. :

public static void main(String[] args) {

// statements

}

Page 30: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Escape Sequence• Special characters.• Start from backslash ( \ ) before

characters.Character Escape Sequence

Name

\b Backspace

\t Tab

\n Linefeed

\f Formfeed

\r Carriage Return

\\ Backslash

\’ Single Quote

\” Double Quote

\ddd Octal (0 s/d 377)

\udddd Heksadesimal (dd= 0 s/d FF atau ff)

Page 31: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Escape Sequence

Example of escape sequence.

Page 32: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Did You Know?• Java Versions

– 19 Februari 1997, Java 1.1• hanya AWT

– 08 Desember 1998, Java 1.2• Codename: Playground, dikenal dengan Java 2, muncul platform J2SE, J2EE, J2ME

– 08 Mei 2000, Java 1.3• Codename: Kestrel, mengintegrasikan sound

– 06 Februari 2002, Java 1.4• Codename: Merlin, mengintegrasikan XML

– 30 September 2004, Java 5.0• Codename: Tiger, awalnya berversi 1.5 (tetapi sudah menghilangkan metode versi 1.x)

– 11 Desember 2006, Java 6• Codename: Mustang, menghilangkan metode versi x.0, mendukung Visual Basic, GUI

Vista– (belum rilis) 2008, Java 7

• Codename: Dolphin, perbaikan beberapa bug pada versi sebelumnya

Page 33: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Did You Know ?

• Java Logo :

• Java Mascot (Duke):

Page 34: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Advanced Learning

• Java supports Unicode• Encoding scheme developed by Unicode

Consortium.• Support translating, processing, displaying

language around the world. • e.g.:

– Welcome dalam mandarin – I Love You dalam mandarin

Page 35: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Advanced Learning

• Example of using a Unicode

Page 36: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Advanced Learning

• import java.swing.JOptionPane; will be explained in next session.

• To show message graphics box :JOptionPane.showMessageDialog(null,”…”,”…”,JOptionPane…);

parent teks judul jenis pesan

Page 37: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Advanced Learning

• Execution result,

Page 38: Intorduction to Java Programming Session 2 Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010

Bina Nusantara

Refereces• Introduction to Java Programming. 7ed. Liang. 2009. p40-48• Java Programming Language.

http://en.wikipedia.org/wiki/Java_(programming_language) • Java Software Platform. http://en.wikipedia.org/wiki/Java_(software_platform)• Java Bytecode. http://en.wikipedia.org/wiki/Java_bytecode• JDK. http://en.wikipedia.org/wiki/Java_Development_Kit• JVM. http://en.wikipedia.org/wiki/Java_Virtual_Machine• Logo Java. http://en.wikipedia.org/wiki/Image:Java_Logo.svg• Java Platform. http://en.wikipedia.org/wiki/Image:JavaPlatform.jpg• http://en.wikipedia.org/wiki/Image:Wave.svg• Java Characteristics.

http://www.cs.armstrong.edu/liang/intro6e/JavaCharacteristics.pdf• History of Java. http://java.sun.com/features/1998/birthday.html • Lexical Structure.

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html