intorduction to java programming session 2
DESCRIPTION
Intorduction to Java Programming Session 2. Course: T0974-Algorithm & Object-Oriented Programming I Year: 2010. Learning Outcome. After taking this course, students should be expected to explain and discuss structure and element of java programming. Outline Materi. - PowerPoint PPT PresentationTRANSCRIPT
Intorduction to Java ProgrammingSession 2
Course : T0974-Algorithm & Object-Oriented Programming IYear : 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
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
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.
Bina Nusantara
Characteristics• Simple• Object-oriented• Distributed• Interpreted• Robust• Secure• Architecture-neutral• Portable• High-performance• Multi-threaded• Dynamic
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
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)
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
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.
Bina Nusantara
Java Platform
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!”);}
}
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
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.
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
Bina Nusantara
Java Anatomy– Whitespace– Identifiers– Literal– Comments– Separators– Reserved words (keyword)– Modifiers– Statements– Blocks– Classes– Methods– The main method
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
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
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”
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! */
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.
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.
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.
Bina Nusantara
Modifiers
• Reserved Words.• Showing properties.• Showing data properties , methods, and classes.• Modifiers :
– public– static– private– final– abstract– protected
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.
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
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).
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.
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
}
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)
Bina Nusantara
Escape Sequence
Example of escape sequence.
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
Bina Nusantara
Did You Know ?
• Java Logo :
• Java Mascot (Duke):
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
Bina Nusantara
Advanced Learning
• Example of using a Unicode
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
Bina Nusantara
Advanced Learning
• Execution result,
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