chapter 1 java fundamentals

47
Xiang Zhang [email protected] http://wds.ac.cn/java/ Chapter 1 Java Fundamentals

Upload: others

Post on 07-Apr-2022

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 1 Java Fundamentals

X i a n g Z h a n g

j a v a c o s e @ q q . c o m

h t t p : / / w d s . a c . c n / j a v a /

Chapter 1 Java Fundamentals

Page 2: Chapter 1 Java Fundamentals

Content

Evolution of Java

JDK and JRE

Java Operating Mechanism

Java Developing Environment

Java Primary Data Types

Java Basic Grammar

2

Page 3: Chapter 1 Java Fundamentals

Evolution of Java – Success of OOP

3

from《Beyond Java》

Ada Lovelace

Page 4: Chapter 1 Java Fundamentals

Ada Lovelace (历史上第一位程序媛)

4

10 December 1815 – 27 November 1852)

English mathematician and writer

The first to recognize that the machine

had applications beyond pure calculation

She published the first algorithm

mechanical general-purpose computer,

the Analytical Engine

Page 5: Chapter 1 Java Fundamentals

Evolution of Java – Life of Java

Past

Resource-limited

Device

C++

Green Project

Oak

Mosaic / Netscape /

Mark Andreessen

HotJava

Present

Internet / WWW

Enterprise

1st language in

industry

Future

Java vs. Dynamic

Language

Java and open

source

5Open Discussion:

Please list some

resource-limited

devices

Page 6: Chapter 1 Java Fundamentals

Today’s Java

6

Page 7: Chapter 1 Java Fundamentals

Today’s Java

7

Page 8: Chapter 1 Java Fundamentals

Today’s Java

8

Salary:

Page 9: Chapter 1 Java Fundamentals

JDK and JRE

JDK – Java Development Toolkit

J2SE – Java 2 Standard Edition

J2EE – Java 2 Enterprise Edition

J2ME – Java 2 Micro Edition

JRE – Java Runtime Environment

9

Page 10: Chapter 1 Java Fundamentals

Java Mechanism – Traditional

10

Page 11: Chapter 1 Java Fundamentals

Java Mechanism – Java

11

Page 12: Chapter 1 Java Fundamentals

Java Developing Environment

Text editor

IDE

Eclipse

IntelliJ IDEA

Netbeans

MyEclipse

12

Page 13: Chapter 1 Java Fundamentals

Eclipse

13

Page 14: Chapter 1 Java Fundamentals

Eclipse is not only an IDE

14

Page 15: Chapter 1 Java Fundamentals

Java Features

Simplicity: simple grammar, rich library

Pure OO: everything is object!

Security: memory access, garbage collection, exception

Portability: Java Virtual Machine

Interpreted execution: Bytecode

15

Page 16: Chapter 1 Java Fundamentals

Exploring Java

16

Page 17: Chapter 1 Java Fundamentals

The Structure Of Java Programs

17

package declaration

class declaration

variable declaration

and initialization

constructor

method

main method

Page 18: Chapter 1 Java Fundamentals

How many errors?

Page 19: Chapter 1 Java Fundamentals

Java Primary Data Types

19

Page 20: Chapter 1 Java Fundamentals

Java Primary Data Types

20

Type size(bit) range wrapper

boolean 1 true/false Boolean

char 16 Unicode Character

byte 8 [-128, 127] Byte

short 16 [-215, 215-1] Short

int 32 [-231,231-1] Integer

long 64 [-263,263-1] Long

float 32 3.4*1038 Float

double 64 1.7*10308 Double

void Void

Page 21: Chapter 1 Java Fundamentals

Conversion Between Values

From Low Accuracy to High Accuracy: Auto

double d = 10;

From High Accuracy to Low Accuracy: Cast

int t = (int)10.2;

21

Page 22: Chapter 1 Java Fundamentals

Primary Types and Wrapper

Values of Primary Types are NOT Objects!

Each Primary type has a corresponding wrapper to wrap a

value into an object:

Integer a = 473;

System.out.println(a.compareTo(new Integer(472)));

22

Page 23: Chapter 1 Java Fundamentals

More About This Statement

23

Class:java.lang.System

object:PrintStream, static

method,void

object:Integer

method,int

Page 24: Chapter 1 Java Fundamentals

Print and Format

System.out.println()

String Formatter

24

Page 25: Chapter 1 Java Fundamentals

Variables and Constants

Declare and use Lifecycle and Hidden Variables

25

Page 26: Chapter 1 Java Fundamentals

Notice!

Different with C++

26

Page 27: Chapter 1 Java Fundamentals

Naming

Basic Principle:

A names should reflect the meaning of a class/package/variable…

Different with Java keywords

**Different with java.lang.* // not restricted

Only English

27

1. letter

2. underline‘_’

3. $’

First letter

arbitrary

1. digit

2. letter

3. underline‘_’

4.‘$’

The remaining part

nam

e

Page 28: Chapter 1 Java Fundamentals

Naming

project

demo

package

package efrei.java;

class

public class Person;

28

variable

int age = 20;

method

void greet(){};

constant

final double PI =3.14;

Page 29: Chapter 1 Java Fundamentals

Java Operator

Mathematical operator

Relational operator

Logical operator

Bitwise operator

Assignment operator

Others

29

Page 30: Chapter 1 Java Fundamentals

Mathematical Operator

+、- 、* 、/ 、%

++ 、- -

30

Page 31: Chapter 1 Java Fundamentals

Relational Operator

> 、>=

< 、<=

== 、!=

instanceOf

31

Page 32: Chapter 1 Java Fundamentals

Logical Operator

&、|

&&、||

!

^

32

Page 33: Chapter 1 Java Fundamentals

Bitwise Operator

<<

>>

>>>

33

1-33

Page 34: Chapter 1 Java Fundamentals

Assignment Operator

=

+= 、-= 、*= 、/= 、% =

>>= 、<<= 、>>> =

34

Page 35: Chapter 1 Java Fundamentals

Others

? :

.

new

[]

35

Ternary if-else operator

Page 36: Chapter 1 Java Fundamentals

Java Grammar

Package

Import

Class

Field

Method

36

Page 37: Chapter 1 Java Fundamentals

Lab Work 0

Write a Java Class Student

id, name, gender

A sample greet() outputs: “Hello, I am Xiang Zhang. I am a male

student, and I am from Class 3.”

Open discussion:

Is is good to use a String for gender? Any better type?

Don’t put everything in main(), why?

37

Page 38: Chapter 1 Java Fundamentals

Java Statement

if-else

switch

while、do-while

for

break

continue

return

38

Page 39: Chapter 1 Java Fundamentals

Java Keywords

39

abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

enum

extends

false

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

null

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

true

try

void

volatile

while

Page 40: Chapter 1 Java Fundamentals

Java Comments

40

Page 41: Chapter 1 Java Fundamentals

Lab Work 1

41

Page 42: Chapter 1 Java Fundamentals

Lab Work 1

Two ways to avoid hard-coding

String[] args

Scanner

42

Page 43: Chapter 1 Java Fundamentals

String[] args

43

Page 44: Chapter 1 Java Fundamentals

Scanner

44

hint:

Page 45: Chapter 1 Java Fundamentals

Lab Work 2

A simple version of ATM

Single user

Deposit / Withdrawal / Query Balance

Using Scanner to get user request and amount of money

An user interface like this:

Try NOT to write all the codes in main()!!

45

Page 46: Chapter 1 Java Fundamentals

Self-teaching

Javadoc

What is Javadoc?

How to add comments in program for making a Javadoc?

How to generate Javadoc in HTML format?

How to search in Javadoc?

46

Page 47: Chapter 1 Java Fundamentals

Forecast

OO Concepts

Class and Objects

Package

Field

Method

Main method

Object

Construct and Initialization

Access Control

47