Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang [email protected]...

31
Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang [email protected] Lecture No. 2

Transcript of Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang [email protected]...

Page 1: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

Spring 09- ICE0124

Programming Fundamentals I

Java Programming

XuanTung [email protected]

Lecture No. 2

Page 2: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

Introduction to Java Programming and Applications

1. Compiler/Interpreter2. Java language3. Java Platforms, JRE and JDK4. First programs: Hello world, and Addition5. Syntax: Things to remember …6. … Other experiences7. Memory concepts8. Primitive Data Types9. Expression, Arithmetic and relation operators

Page 3: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 3

Interpreter and Complier Computers need translators to translate high-level

language to machine 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 before execution Turn program’s text into executable file (in machine

language format) When user wants to run the program, he/she loads the

executable file into computer memory to execute. Portability issue: Different computer hardwares use

different instruction sets (machine language) We need appropriate interpreter/compiler for each type

of machine

Page 4: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 4

History of Java language Initially, Java language is developed for

consumer-electronic devices (embedded systems) (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 attractive for embedded systems (its primary purpose)

Page 5: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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) …

Page 6: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Page 7: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Page 8: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 8

First Application

Hello.java

1 /* The first program in Java

2 It simply prints out a message */

3 class Hello {

4 public static void main( String[] args ) {

5 // print message

6 System.out.println("Hello world\n");

7 }

8 } Output: Hello world

Page 9: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 { … }

Hello.java

1 /* The first program in Java

2 It simply prints out a message */

3 class Hello {

4 public static void main( String[] args ) {

5 // print message

6 System.out.println("Hello world\n");

7 }

8 }

Page 10: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Hello.java

1 /* The first program in Java

2 It simply prints out a message */

3 class Hello {

4 public static void main( String[] args ) {

5 // print message

6 System.out.println("Hello world\n");

7 }

8 }

Page 11: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Hello.java

1 /* The first program in Java

2 It simply prints out a message */

3 class Hello {

4 public static void main( String[] args ) {

5 // print message

6 System.out.println("Hello world\n");

7 }

8 }

Page 12: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Hello.java

1 /* The first program in Java

2 It simply prints out a message */

3 class Hello {

4 public static void main( String[] args ) {

5 // print message

6 System.out.println("Hello world\n");

7 }

8 }

Page 13: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 13

More complex program: AdditionAddition.java 1 // Program that computes the sum of two numbers 2 import java.util.Scanner; 3 public class Addition { 4 public static void main( String args[] ) { 5 Scanner input = new Scanner( System.in ); 6 int number1; // the 1st number 7 int number2; // the 2nd number 8 int sum; // sum of the two numbers 9 10 System.out.print("Enter the 1st number:"); 11 number1 = input.nextInt();//read the 1st number 12 13 System.out.print("Enter the 2st number:"); 14 number2 = input.nextInt();//read the 2nd number 15 16 sum = number1 + number2; // add numbers 17 // display result 18 System.out.printf( "Sum is %d\n", sum ); 19 20 // display the result in another way 21 System.out.printf( "Sum is (again) %d\n", 22 number1 + number2 ); // display result 23 } 24 }

Page 14: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 14

More complex program: AdditionAddition.java 1 // Program that computes the sum of two numbers 2 import java.util.Scanner; 3 public class Addition { ... ...

24 }

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

Page 15: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 15

More complex program: AdditionAddition.java 1 // Program that computes the sum of two numbers 2 import java.util.Scanner; 3 public class Addition { 4 public static void main( String args[] ) { 5 Scanner input = new Scanner( System.in ); 6 int number1; // the 1st number 7 int number2; // the 2nd number 8 int sum; // sum of the two numbers 9 10 System.out.print("Enter the 1st number:"); 11 number1 = input.nextInt();//read the 1st number 12 13 System.out.print("Enter the 2st number:"); 14 number2 = input.nextInt();//read the 2nd number 15 16 sum = number1 + number2; // add numbers 17 // display result 18 System.out.printf( "Sum is %d\n", sum ); 19 20 // display the result in another way 21 System.out.printf( "Sum is (again) %d\n", 22 number1 + number2 ); // display result 23 } 24 }

Variables declaration

Using Scanner object to read input

Do computation & display result

Page 16: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 goes to the next line System.out.printf(“result is %d”, sum): output a

string with format specifiers Read textbook section 2.4

Page 17: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 method

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

Page 18: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Page 19: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 according to type of the variable Value: is stored in binary format at the memory

location

Scanner input = new Scanner( System.in ); int number1; // the 1st number int number2; // the 2nd number int sum; // sum of the two numbers type

name

Page 20: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Page 21: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 ?

Scanner input; int number1; // the 1st number int number2; // the 2nd number int sum; // sum of the two numbers

Integral numbers Floating point numbers

characters Logical (True/false)

Page 22: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 22

Number data typesInteger Primitive Data Types

type Size Range

byte 8 bits -128 to +127

short 16 bits -32,768 to +32,767

int 32 bits (about)-2 billion to +2 billion

long 64 bits (about)-10E18 to +10E18

Floating Point Primitive Data Types

Type Size Range

float 32 bits -3.4E+38 to +3.4E+38

double 64 bits -1.7E+308 to 1.7E+308

Number literals Valid integers:

2007 2007L (long integer)

Valid floating point numbers:

2007, 2007.02007f, 2007.1f (float)2007d, 2007.1d (double)3.4E+3 (scientific notation)

Can we use byte data type to store the number 2007 ?

Page 23: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 to letters (Unicode) Character literal:

Valid characters:‘a’, ‘b’, ‘A’, ‘\n’, ‘\t’

Is it a character “C” ?

Page 24: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 24

boolean data type

Accept only two values: true and false They are Java keywords

Page 25: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 25

Expressions and Arithmetic Expressions

Expression is a combination of literals, operators and parenthesis to produce a value Example:

a + b * c (a, b, c are variables already declared) Expressions dealing with number variables

and operators 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 numbers ?

Page 26: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 26

Order of calculation and Precedence

Order of calculations: Parenthesis *, /, % +/- Left to right

Examples

Page 27: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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 and 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?

Page 28: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

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

Logically we expect true but the computer report false

Avoid using equality with floating point numbers

Page 29: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 29

Two’s complement notation(Appendix E6, textbook) Assume 4-bit signed integers

0000=0

0001=1

0010=2

0011=3

0111=7

1000= - 8

1001= - 7

1010= - 6

1111= - 1

1110= - 2

1101= - 3

1100= - 4

1011= - 5

0100=4

0101=5

0110=6

Bit complement (~)+1 Arithmetic negation "-" is equivalent to "~" then

+ 1

Page 30: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 30

Floating-point number precision

Two data type (primitive) for floating point numbers: 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

approximate

Page 31: Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2.

XuanTung Hoang 31

Assignment Assignment operator, =

Stores the value evaluated on “the right hand side” into the variable on “the left hand side”

Example:

int number1; int number2; int sum; number1 = 4; // stores value 4 into variable number1 number2 = input.nextInt(); //get a number from user and stores it into variable number2 sum = number1 + number2; // add number1 and number2 and stores it into sum