Java - Trang ď Chap01

13 113 0
Java - Trang ď Chap01

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming Java Java Basics Incheon Paik Java Computer Industry Lab Contents „ „ „ „ „ „ „ „ „ „ „ „ „ Java Background The Java Virtual Machine Applications & Applets Classes & Objects The Principles of Object Oriented Programming Start up Java Variables & Assignments Strings & Characters Arithmetic Operators & Expressions Type Conversion Comments Arrays Keywords Computer Industry Lab Background / History „ Sun Microsystems Inc „ James Gosling „ 1990, “Green Project” 1991, Develop the Language for Household Products => To General Developing Language „ „ „ „ Java „ James Gosling, Arthur Van Hoff, Andy Bechtolsheim „ Coffee Materials in Indonesia Write once, Run everywhere Spread Rapidly through WWW and Internet Java Computer Industry Lab Byte Code and Java Virtual Machine „ Existing Development Environment Binary File Compiler(Pentium) Source Code Binary File Compiler(PowerPC) Run Java PowerPC Binary File %cc Hello.c –o Hello % Hello Pentium Compiler(SPARC) SPARC Binary Code Computer Industry Lab Bytecodes and the Java Virtual Machine Java Development Environment „ Java Interpreter Java Compiler (Pentium) Pentium Java Code Java ByteCode Java Interpreter (Independent on Platform) Java Compiler PowerPC (PowerPC) Java Interpreter %javac Hello.java Hello.class created JVM Run SPARC Java Compiler % java Hello (SPARC) Byte Code Java Computer Industry Lab Java Program „ Java Application Program „ Application „ Program written in general programming language „ Java Applet „ Program running in Web Browser Environment „ Can be viewed by appletviewer or Web browser with JVM Computer Industry Lab Classes and Objects „ Object „ Memory Space to Define State and Operation „ Instance of Class „ Class „ Template of Creating Object Java Computer Industry Lab The Principles of Object Oriented Programming „ „ „ Encapsulation „ Control Access the data and method in program code Inheritance „ Inherit the data and methods of parent class Polymorphism getArea() Shape Shape Obj = new Ellipse(); Obj.getArea(); Ellipse Rectangle Triangle Which Area? getArea() Java getArea() getArea() Computer Industry Lab The 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 Java Computer Industry Lab Java SDK „ Java Software Development Kit (Java SDK) „ Java Application or Applet Development and Running Environment „ Java Runtime Environment (JRE) „ Java Running Environment „ Java Standard Edition Java Micro Edition Java Enterprise Edition „ „ Java 10 Computer Industry Lab Start a Java Application Java C Class Name “Hello.java” Main method class Hello { public static void main(String args[]) { System.out.println(“Hello World”); } % javac Hello.java Compile Run % java Hello Result % cc hello.c –o hello % hello Hello World 11 Java Main Function “hello.c” void main() { printf(“Hello World¥n”); } Computer Industry Lab Variables and Assignments „ „ Java Variables Types „ char „ boolean „ byte „ short „ int „ long „ float „ double 16bits Unicode character data Boolean Variable bits signed integer 16 bits signed integer 32 bits signed integer 64 bits signed integer 32 bits signed floating point number 64 bits signed floating point number 12 Computer Industry Lab Variables and Assignments Variable Declaration type varName; float x, y, x; Value assignments varName = value; int num = 100; long m = 21234234L double type : 0.8 9e-2 -9.3e-5 float type : 1.5e-3f; 13 Java Computer Industry Lab Strings and Characters „ String : sequence of character String s = “Enter an integer value: ” ; Char c = ‘A’; Unicode „ Concatenation Operator ‘+’ String s = “Lincoln said: ” + “¥” Four score and seven years ago¥”” ; Result : Lincoln said: “Four score and seven years ago” Java 14 Computer Industry Lab Arithmetic Operators „ Operators „ + - * / % „ += -= *= /= %= „ ++ -5/2 Ỉ Why isn’t it 2.5 ? 5%2 Ỉ 4/2 Ỉ 4%2 Ỉ „ count * num + 88 / val – 19 % count „ j += 6; Ỵ j = j + 6; 15 Java Computer Industry Lab Type Conversions in Expression char ch; int i; float f; double outcome; ch = ‘0’; i = 10; f = 10.2f; outcome = ch * i / f; int float double double Yes No double float Yes No float long Yes long Java 16 No int Computer Industry Lab Type 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 17 Java Computer Industry Lab Comments „ „ „ Java Single Line Comment „ int i = 10 ; // i is counter Multiple Line Comment /* Some comments */ Documentation Comment /** Documentation Comment */ 18 Using “javadoc” Tool, make document Computer Industry Lab Arrays ( One Dimensional) Definition of One Dimensional Array type VarName[] int ia[]; Assign Range to One Dimensional Array varName = new type[size] ia = new int[10]; Declaration of One Dimensional Array with Range type varName[] = new type[size] int ia[] = new int[10]; 19 Java Computer Industry Lab Arrays ( One Dimensional) Number of elements in Array varName.length Initialization of One Dimensional Array Type varName[] = {e0, … , en}; int j[] = {0, 1, 2, 3, 4, 5}; int k[]; K = j; Example 1.15 Java 20 Computer Industry Lab Arrays ( Multi-Dimensional) Definition of Two Dimensional Array type VarName[][]; float fa[][]; Assign Range to Two Dimensional Array varName = new type[size1][size2]; fa = new float[2][3]; Declaration of Two Dimensional Array with Range type varName[][] = new type[size1][size2]; float fa[] = new float[2][3]; 21 Java Computer Industry Lab Arrays ( Two Dimensional) Number of elements in Multi-Dimensional Array varName.length Number of Raw Number of elements in Each elements of Multi-Dimensional Array varName[index].length Initialization of Multi-Dimensional Array Type varName[][] = {{e00, … , e0n}, {e10,…,e1y}, {e20, …, e2z}}; Example 1.16 Java 22 Computer Industry Lab Java Keywords „ 50 Java Keywords abstract boolean break byte case catch char class const* continue default assert (New in 1.5) double int else interface extends long final native finally new float package for private goto* protected if public implements return import short instanceof static enum (New in 1.5) super switch synchronized this throw throws transient* try void volatile while strictfp The “assert” was recognized as keyword in JDK1.4 compiler, but we could not use it It can be used it from 1.5 23 Java Computer Industry Lab Exercise „ „ Step „ Refer to this lecture (slide #11) Step (How to make & run Applet) „ (1) Make Applet Program import java.awt.Graphics; public class HelloWorldApplet extends java.applet.Applet { public void paint (Graphics g) { g.drawString(“Hello World!”,5,25); } } // end of Applet „ „ (2) Compile(using javac) to create HelloWorldApplet.class (3) Create HTML file (ex; HelloWorld.html) to include applet „ Java (4) Run Applet using Appletviewer or Applet enabling Web browser % appletviewer HelloWorld.html 24 Computer Industry Lab Exercise „ „ Java Step (Type Conversion) „ Refer to this lecture (slides #16,17) Step (Array) „ Refer to this lecture (slide #22) 25 Computer Industry Lab ... Bytecodes and the Java Virtual Machine Java Development Environment „ Java Interpreter Java Compiler (Pentium) Pentium Java Code Java ByteCode Java Interpreter (Independent on Platform) Java Compiler... PowerPC (PowerPC) Java Interpreter %javac Hello .java Hello.class created JVM Run SPARC Java Compiler % java Hello (SPARC) Byte Code Java Computer Industry Lab Java Program „ Java Application Program... 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

Ngày đăng: 09/12/2017, 02:06

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan