390 likes | 555 Views
Introduction to Java Programming Session 2. Course : T0974-Algorithm & Object-Oriented Programming I Year : 2011. Learning Outcome. After taking this course, students should be expected to explain and discuss structure and element of java programming. Outline Materi.
E N D
Introduction to Java ProgrammingSession 2 Course : T0974-Algorithm & Object-Oriented Programming I Year : 2011
Learning Outcome After taking this course, students should be expected to explain and discuss structure and element of java programming
Outline Materi • History of Java Programming Language • Characteristics • API, JDK, JRE • Java Platform • Simple Program using Java • Anatomy of Java Programming • Escape Sequence • Unicode
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.
Characteristics • Simple • Object-oriented • Distributed • Interpreted • Robust • Secure • Architecture-neutral • Portable • High-performance • Multi-threaded • Dynamic
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
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)
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
Java Runtime Environment (JRE) • A software that execute Java based application • Java Virtual Machine (JVM) is a collection of programs to execute java byte code on any computer platform • Java byte code is a collection of machine-specific instruction that be interpreted and executed by JVM. Its length is 1 byte per instruction
Simple Program // This application program prints Welcome to Java! public class Welcome { public static void main(String[] args){ System.out.println(“Welcome to Java!”); } }
Simple Program // This application program prints Welcome to Java! public class Welcome { public static void main(String[] args){ System.out.println(“Welcome to Java!”); } } Class name Filename: Welcome.java Comments Class heading, Main method signature String
Simple Class • Every Java Program should have 1 public class e.g. Welcome • To execute a class, it should have a main method • System.out.println is a statement to print string into console
Creating, Compiling, Executing Source code (developed by the programmer) Create/Modify Source Code public class Welcome { Public static void main(String [] args) { System.out.println(“Welcome to Java!”); } } Save on the disk Source Code Compile Source Code e.g., javac Welcome.java Bytecode (generated by the compiler for JVM to read and interpret, not for you to understand) If compilation errors Stored in the disk … 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 Run Bytecode e.g., java Welcome If runtime errors or Incorrect result Result
Java Anatomy • Whitespace • Identifiers • Literal • Comments • Separators • Reserved words (keyword) • Modifiers • Statements • Blocks • Classes • Methods • The main method
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
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
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”
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! */
Separators/Punctuations • Specific notations for spesific goals. • e.g:
ReservedWords • Keywords • Reserved words have a specific meaning to a compiler. • Couldn’t be used for another use in program. • e.g : • class when 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 isassumed as a keyword but Public isn’t.
Reserved Words * reserved for next version of Java.
Modifiers • Reserved Words. • Showing properties. • Showing data properties , methods, and classes. • Modifiers : • public • static • private • final • abstract • protected
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.
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!”); } } Method Block Class Block
Classes • Core of Java Programming • Every Java Program should have 1 public class. • Inside a class should has a data and method(s) (Encapsulation).
Methods • Collection of Statements that to be encapsulated. • e.q. : • System.out.println System.out standard output object println method dalam object The results will be appear into command prompt.
MainMethod • 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 }
EscapeSequence • Special characters. • Start from backslash ( \ ) before characters.
EscapeSequence Example of escape sequence.
Did YouKnow? • Java Versions • 19 Februari 1997, Java 1.1 • Just AWT • 08 Desember 1998, Java 1.2 • Codename: Playground, known as Java 2, platform J2SE, J2EE, J2ME • 08 Mei 2000, Java 1.3 • Codename: Kestrel, sound integrated • 06 Februari 2002, Java 1.4 • Codename: Merlin, XML integrated • 30 September 2004, Java 5.0 • Codename: Tiger, initially versioned 1.5 (but have omitted 1.x version) • 11 Desember 2006, Java 6 • Codename: Mustang, omitted x.0 version, support Visual Basic, GUI Vista • 7 Juli 2011, Java 7 • Codename: Dolphin, few fixed bugs • Expected to be released in Summer 2013, Java 8
Did You Know ? • Java Logo : • Java Mascot (Duke):
AdvancedLearning • Java supports Unicode • Encoding scheme developed by Unicode Consortium. • Support translating, processing, displaying language around the world. • e.g.: • Welcome in mandarin • I Love You in mandarin
Advanced Learning • Example of using a Unicode
Advanced Learning • import java.swing.JOptionPane; will be explained in next session. • To show message graphics box : JOptionPane.showMessageDialog (null,”…”,”…”,JOptionPane…); jenis pesan parent teks judul
Advanced Learning • Execution result,
Exercise • Write a pseudocode, flowchart and program for selling HP (disc 5% if sub total >=1.000.000) to produce an output : HP Store HP:500000 Quantity : 3 Disc : 5% Total : 1425000
Refereces • Introduction to Java Programming. 8ed. Liang. 2011. p32-41 • 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