Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

12
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang [email protected] Lecture No. 8

description

XuanTung Hoang 3 Introduction to Computers Computer :  Hardware 6 logical units: Input, output, CPU, ALU, memory, secondary storage  Software: Computer programs that command computer hardware to for solving computing tasks Programming languages: For writing computer software  Machine language  assembly language  High-level languages  High level language needs translator or compiler

Transcript of Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

Page 1: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

Spring 2009Programming Fundamentals I

Java Programming

XuanTung [email protected]

Lecture No. 8

Page 2: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

Review

Page 3: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 3

Introduction to Computers Computer :

Hardware 6 logical units: Input, output, CPU, ALU, memory, secondary

storage Software:

Computer programs that command computer hardware to for solving computing tasks

Programming languages: For writing computer software Machine language assembly language High-level

languages High level language needs translator or compiler

Page 4: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 4

Java Programming Language A high level language

Use both interpreter and compiler Compiler: Source code byte code Interpreter: byte code machine language

J2SE Java Platform JRE – Java Runtime Environment:

Java Virtual Machine (JVM): interpreter, java.exe Libraries

JDK – Java Development Kits JRE Compiler: javac.exe Library + other tools

Page 5: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 5

Java Language: Things to remember Java is case-sensitive Statements end with “ ; ” Comments: //, /* … */ Block of code: { … } Array: [ ] Keywords: class, this, public, private, static, final,

int, double, float, short, long, boolean, true, false, if, for, while, do, switch, case, break, continue, new, return, …

Page 6: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 6

Java Language: Things to remember One file = one class

File name = class name Compile: javac <java file name> Run: java <class name with main method> Identifiers (class/method/variable name):

Comprise of: lower case chars (a – z), upper-case char (A – Z), numeric char (0 – 9), _ int number_Of_people1 = 0; // valid int people@home = 5; // invalid

Page 7: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 7

Java Language: Important concepts Data type:

Primitive data type: int, short, long, float, double, boolean, char, byte Built-int data type

class (object/reference): Provided by class libraries (E.g.: String) or made by users

Object: instance of a class Created with new Accessed through reference

Using “.”

Page 8: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 8

Java Language: Important concepts Variable:

4 properties: Name: identifier Type: Primitive data type or reference (class, object reference) Size: depends on type Value: specific value in a certain context

Two type of variables Instance variable: member of a class

Non-static instance variable is associated with a specific object Static instance variable is associated with a class (do not need a

specific object) Local variable

Declaration and initialization Non-static instance variable can ONLY be initialized in constructor

Scope of a variable Variable can be shadowed by other variable (with the same name)

Page 9: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 9

Java Language: Important concepts Class:

Instance variables Static/non-static Constants: define with final

Methods Static/non-static 3 ways to call a methods Static methods cannot “directly call” non-static methods Return type and return expression agreement Constructors: special method for initializing object’s instance

variables (no return type, name = class name) Get/set methods Why main method should be static?

Page 10: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 10

Java Language: Operator Arithmetic: + , - , * , / , %, ++, --, +=, -=, *=, /=,

%= For integers For floating point numbers

Relational: >, <, >=, <=, ==, != Be careful when using = = with floating point numbers

Logical: &, &&, |, ||, ^^, ! !(2 >4) (3 < 4) && (!(4>3))

new operator

Page 11: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 11

Java language: Control statements if, if … else switch … case

Only for integer expression for, while, do … while

Empty statement can be allowed Be careful when putting “;”

if( x < 2) ; { … } for (int k ; k < 10; k++) ; { … } …

Page 12: Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.

XuanTung Hoang 12

Arrays Declaration / creation / initialization Array of primitive type Array of object references

When an array of references is created, each element of the array will be a reference that is pointing to null

Multi dimensional array Array of arrays