Download presentation
Presentation is loading. Please wait.
Published byClare Farmer Modified over 9 years ago
1
Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2
2
Introduction to Java Programming and Applications 1. Compiler/Interpreter 2. Java language 3. Java Platforms, JRE and JDK 4. First programs: Hello world, and Addition 5. Syntax: Things to remember … 6. … Other experiences 7. Memory concepts 8. Primitive Data Types 9. Expression, Arithmetic and relation operators
3
XuanTung Hoang 3 Interpreter and Complier Computers need translators to translate high-level language to m achine instructions Interpreter translates programs (written in high-level languages) online : Translate and execute at the same time Statement by statement Compiler translates the whole program into machine language be fore execution Turn program’s text into executable file (in machine language for mat) When user wants to run the program, he/she loads the executabl e file into computer memory to execute. Portability issue: Different computer hardwares use different instr uction sets (machine language) We need appropriate interpreter/compiler for each type of machin e
4
XuanTung Hoang 4 History of Java language Initially, Java language is developed for consumer-electronic devices (embedded syst ems) (in 1991 by Sun Microsystems) Also, It turned out to be good for Web and Internet applications Add dynamic contents (Java applet) Rapid development of distributed applications Ease of deployment … now Java becomes more and more attracti ve for embedded systems (its primary purpos e)
5
XuanTung Hoang 5 Java Platforms Sun Microsystems provides most of the utilities for users in “Java Platform” products Text editor Bytecode compiler Class loader Bytecode Verifier JVM (interpreter) … Plain text editor: GVIM, Crimson editor, Ultraedit, … Rich text editor: MSWord,… Java Platforms: J2SE, J2EE, J2ME
6
XuanTung Hoang 6 Java Platforms J2SE: Standard Edition J2EE: Enterprise Edition J2ME: Micro Edition JDK v.s. JRE + bytecode compiler + Class libraries + … + JVM (interpreter) + … JRE JDK
7
XuanTung Hoang 7 Java Development Process 5 phases Creating a program: use text editor to create program source Compiling: use compiler to turn program source into bytecode (.class files) Loading: use class loader to load.class files into memory Bytecode verification: bytecode verifier examines the bytecode for validity and security concerns Execution: Java Virtual Machine (is a kind of interpreter for bytecodes) executes the bytecodes Combination of compiling and interpreting Enhance portability of programs: Write Once – Run Anywhere Compact bytecode files are easy to exchange Bytecode verifier guarantees security Just-In-Time compiler enhances performance
8
XuanTung Hoang 8 First Application
9
XuanTung Hoang 9 First Application: Structure Hello.java defines one class: class Hello Keyword class is used for defining a class Class name follows keyword class Class body is surrounded by braces { … }
10
XuanTung Hoang 10 First Application: Structure Class body contains methods (or class functions) and variables: In the example we have only one method (method main) and no variables Method has return type (void) and parameters ( args ) Parameters are given within parenthesis ( … ) If the method have no parameters, parenthesis are still required Modifiers public and static characterize the method Braces surrounds method body main method is the program entry Every java program must have one main function
11
XuanTung Hoang 11 First Application: Structure Inside method body, there are statements. There is only one statement in the example. Each statement should be ended with a semicolon “;” One statement may span multiple lines
12
XuanTung Hoang 12 First Application: Comments Multi-line comments with: /* … */ Single-line comments with: // Comments can be placed anywhere. Comments are for human (compiler ignores comments) Comments should explain the code
13
XuanTung Hoang 13 More complex program: Addition
14
XuanTung Hoang 14 More complex program: Addition import statement specifies external classes that we use in our program In the example: Scanner is the class we use Scanner is a class in class libraries provided by JDK
15
XuanTung Hoang 15 More complex program: Addition Variables declaration Using Scanner object to read i nput Do computation & di splay result
16
XuanTung Hoang 16 Read input and display output Read input with Scanner class Import Scanner class import java.util.Scanner; Create Scanner object Scanner input = new Scanner( System.in ); Read input from user using nextInt() method number1 = input.nextInt(); Read textbook, section 2.5 Display input with print, println, printf System.out.print(“abc”): output the string “abc” System.out.println(“abc”): outputs the string “abc” then goe s to the next line System.out.printf(“result is %d”, sum): output a string with f ormat specifiers Read textbook section 2.4
17
XuanTung Hoang 17 Things to remember … Java is case-sensitive: class and Class are different Skeleton of a class Skeleton of a class function (method) Skeleton of a simple java program Simple Java program has one class that contains main met hod Name of the source file must be identical to the class name (class identifier) Statements end with “;” Braces enclose blocks of code Braces should go in pairs (Good practice: Open and close brace together) Read input from user and output result to screen
18
XuanTung Hoang 18 Other experiences… Observe outputs of java compiler (javac.exe) when compiling java program (.java file) When the compilation is successful What files are produced by javac.exe What is stored inside those files (files produced by javac) When the compilation failed Specify the problem in your source code
19
XuanTung Hoang 19 Variable and Memory Example of variables: A variable is associated with a location in memory A variable has: Name: is used to access the memory location Type (data type): specifies how the variable is treated by the computer Size: The size of the memory location; is determined acc ording to type of the variable Value: is stored in binary format at the memory location type name
20
XuanTung Hoang 20 Variable and Memory: Illustration What does the following 16-bit string represent ? 00010100100000000 If you don’t understand, please read textbook, section 2.6
21
XuanTung Hoang 21 Primitive data types and Objects There are many data types We can even define our own data type Data types are classified into primitive data types and reference types (objects) Primitives data types: … are most fundamental … use small, fixed number of bytes … are built into Java … are used as building block for developing more complex data types (objects) There are 8 primitive data types: byte, short, int, long, fload, double, char, boolean Is Int a Java primitive data type ? Integral numbers Floating point n umbers characters Logical (True/false)
22
XuanTung Hoang 22 Number data types Number literals Valid integers: 2007 2007L (long integer) Valid floating point numbers: 2007, 2007.0 2007f, 2007.1f (float) 2007d, 2007.1d (double) 3.4E+3 (scientific notation) Can we use byte data type to store the number 2 007 ?
23
XuanTung Hoang 23 char data type and Character table Size of char is 2 bytes (16 bits) Character table is used to map 16-bit codes t o letters (Unicode) Character literal: Valid characters: ‘a’, ‘b’, ‘A’, ‘\n’, ‘\t’ Is it a character “C” ?
24
XuanTung Hoang 24 boolean data type Accept only two values: true and false They are Java keywords
25
XuanTung Hoang 25 Expressions and Arithmetic Expressions Expression is a combination of literals, operators an d parenthesis to produce a value Example: a + b * c (a, b, c are variables already declared) Expressions dealing with number variables and oper ators are arithmetic expressions Arithmetic operators in Java: +, -, *, /, % Be careful with division operator /: Integer division is different from floating point division 3 / 2 1 3.0 / 2 1.5 How about remainder operator with floating point nu mbers ?
26
XuanTung Hoang 26 Order of calculation and Precedence Order of calculations: Parenthesis *, /, % +/- Left to right Examples
27
XuanTung Hoang 27 Equality and Relation operators Equality operators : ==, != Relational operators:, = See fig 2.14, section 2.8, textbook Questions: Assume that we declare integer variable a and b a nd assign values 3 to a, and 4 to b. Is a < b a valid expression ? What values are produced by a < b, a < b, a == b?
28
XuanTung Hoang 28 Equality and relation operators Be careful when using == with float/double variables Does this produce true ? 4.0/3.0 == 1.0 + 1.0/3.0 How about this ? 1.0/3.0 = 0.333333333333333 Floating point arithmetic is NOT exact Exact equality sometimes impossible to achieve Logicall y we expect true but the computer report false Avoid using equality with floating point numbers
29
XuanTung Hoang 29 Two’s complement notation (Appendix E6, textbook) Assume 4-bit signed integers Arithmetic negation "-" is equivalent to "~" then + 1
30
XuanTung Hoang 30 Floating-point number precision Two data type (primitive) for floating point nu mbers: float and double float: 32-bit (4-byte). Ex: 0.12f double: 64-bit (8-byte). Ex: 4.15d Floating-point numbers are treated as double by default Example: 9.02 and 9.02d are all double numbers Floating-point number arithmetic are approxi mate
31
XuanTung Hoang 31 Assignment Assignment operator, = Stores the value evaluated on “the right hand side” into the variable on “the left hand side” Example:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.