Sei sulla pagina 1di 31
Introduction to Programming Some examples and/or figures are used with permission from slides prepared by

Introduction to Programming

Introduction to Programming Some examples and/or figures are used with permission from slides prepared by Prof.
Introduction to Programming Some examples and/or figures are used with permission from slides prepared by Prof.

Some examples and/or figures are used with permission from slides prepared by Prof. H. Roumani

 Use comments ◦ Communicate a higher-level understanding of code ◦ Comments are ignored by

Use comments

Communicate a higher-level understanding of code

Comments are ignored by Java

// single-line comments

/* Multi-line comments */

Follow Style Guide (Appendix C)

// single-line comments ◦ /* Multi-line comments */  Follow Style Guide (Appendix C) EECS1020 F14
// single-line comments ◦ /* Multi-line comments */  Follow Style Guide (Appendix C) EECS1020 F14
 Code block ◦ Defines program scope (i.e., boundaries) ◦ Delimited by { and }

Code block

Defines program scope (i.e., boundaries)

Delimited by { and } aligned vertically

Indent all content (including inner blocks)

Example:

{

 

{

 

.

.

.

 

}

}

inner blocks) ◦ Example: {   {   . . .   } } EECS1020 F14
inner blocks) ◦ Example: {   {   . . .   } } EECS1020 F14
 Class definition ◦ Defines your program’s name ◦ Represents outermost scope ◦ Class name

Class definition

Defines your program’s name

Represents outermost scope

Class name should always begin with a capital letter

public class ClassName

// class header

{

}

.

.

.

// class body

with a capital letter ◦ public class ClassName // class header { } . . .
with a capital letter ◦ public class ClassName // class header { } . . .
 Main method definition ◦ Entry point of your program ◦ Indented from class scope

Main method definition

Entry point of your program

Indented from class scope

public static void main(String[] args)

{

}

.

.

.

from class scope ◦ public static void main(String[] args) { } . . . // header
from class scope ◦ public static void main(String[] args) { } . . . // header

// header

// body

 Statements ◦ Instructions to declare variables, assign values, use classes, and control execution flow

Statements

Instructions to declare variables, assign values, use classes, and control execution flow

Example

System.out.println(“Hello”);

values, use classes, and control execution flow ◦ Example System.out.println(“Hello”); EECS1020 F14 (Steven C.) 6
values, use classes, and control execution flow ◦ Example System.out.println(“Hello”); EECS1020 F14 (Steven C.) 6
 Text file called First.java: public class First { public static void main(String[] args) {

Text file called First.java:

public class First

{

public static void main(String[] args)

{

System.out.println(“Hello”);

}

}

class First { public static void main(String[] args) { System.out.println(“Hello”); } } EECS1020 F14 (Steven C.)
class First { public static void main(String[] args) { System.out.println(“Hello”); } } EECS1020 F14 (Steven C.)
 Using a text editor, create a text file with the class name and the

Using a text editor, create a text file with the class name and the extension .java (e.g., First.java)

Compile the program using the javac command (e.g., javac First.java)

Correct any syntax (runtime) errors (remember to save changes) and recompile A successful compilation will generate a class file (e.g., First.class)

Enter the command java, followed by your program’s class name (e.g., java First)

 Enter the command java, followed by your program’s class name (e.g., java First) EECS1020 F14
 Enter the command java, followed by your program’s class name (e.g., java First) EECS1020 F14
C, C++, Fortran, etc. Program Compiler Machine Code Platform J a v a Program Compiler

C, C++, Fortran, etc.

Program

Compiler

Machine Code

Platform

C, C++, Fortran, etc. Program Compiler Machine Code Platform J a v a Program Compiler Bytecode
C, C++, Fortran, etc. Program Compiler Machine Code Platform J a v a Program Compiler Bytecode
C, C++, Fortran, etc. Program Compiler Machine Code Platform J a v a Program Compiler Bytecode

Java

Program

Compiler

Bytecode

JVM

Platform

 Integers ◦ int (4 bytes): ◦ long (8 bytes):  Reals ◦ float (4

Integers

int (4 bytes):

long (8 bytes):

Reals

float (4 bytes):

double (8 bytes):

Characters

char (2 bytes):

Boolean

boolean: (1byte):

[-2x10 9 [-9x10 18

2x10 9 ) 9x10 18 )

[-3.4x10 38 [-1.7x10 308

3.4x10 38 ), 7 sig. digits 1.7x10 308 ), 15 sig. digits

Unicode characters 0x0000 to 0xFFFF

true or false

1.7x10 3 0 8 ), 15 sig. digits Unicode characters 0x0000 to 0xFFFF true or false
1.7x10 3 0 8 ), 15 sig. digits Unicode characters 0x0000 to 0xFFFF true or false
 A variable’s value can change during execution  Declaration primType identifier = value ;

A variable’s value can change during execution

Declaration

primType identifier = value;

Where:

primType is int, long, float, double, etc. identifier is the name you choose for the variable value is the value you want the variable to have

Example int currentTemperature = 8;

value is the value you want the variable to have  Example int currentTemperature = 8;
value is the value you want the variable to have  Example int currentTemperature = 8;
 A constant’s value does not change during execution  Declaration final primType IDENTIFIER =

A constant’s value does not change during execution

Declaration

final primType IDENTIFIER = value;

Where:

primType is int, long, float, double, etc. IDENTIFIER is the name (all caps) you choose value is the value you want the constant to have

Example

final int INCHES_PER_FOOT = 12;

is the value you want the constant to have  Example final int INCHES_PER_FOOT = 12;
is the value you want the constant to have  Example final int INCHES_PER_FOOT = 12;
 Have special meanings in Java  Not to be used as identifiers, class names,

Have special meanings in Java

Not to be used as identifiers, class names, etc.

abstract

assert

 

boolean

break

byte

 

case

catch

char

class

const

continue

default

do

double

 

else

enum

extends

final

finally

float

for

 

goto

 

if

implements

import

instanceof

int

interface

long

 

native

new

 

package

private

protected

public

 

return

 

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

try

 

void

volatile

 

while

 

p. 8 in text

 AND &&  OR  Equal == ||  Not equal !=  Less

AND &&

OR

Equal ==

||

Not equal

!=

Less than

<

Less than or equal

Greater than >

Greater than or equal >=

<=

 Less than or equal  Greater than >  Greater than or equal >= <=
 Less than or equal  Greater than >  Greater than or equal >= <=
 Add  Subtract -  Multiply *  Divide  Remainder % + /

Add

Subtract -

Multiply *

Divide

Remainder %

+

/

 Add  Subtract -  Multiply *  Divide  Remainder % + / EECS1020
 Add  Subtract -  Multiply *  Divide  Remainder % + / EECS1020
 Satisfies the closure property  When dividing an integer by another, the result in

Satisfies the closure property

When dividing an integer by another, the result in also an integer

Ignore any remainder

Don’t confuse integer division with real division

Integer:

Real:

2 / 3 = 0,

2.0 / 3.0 = 2.0 / 3 = 2 / 3.0 = 0.66666…

5 / 3

= 1

◦ Integer: ◦ Real: 2 / 3 = 0, 2.0 / 3.0 = 2.0 / 3
◦ Integer: ◦ Real: 2 / 3 = 0, 2.0 / 3.0 = 2.0 / 3
 Arithmetic operator precedence similar to order of operations learned in high school ◦ Brackets

Arithmetic operator precedence similar to order of operations learned in high school

Brackets

Multiplication and division (including remainder)

Addition and subtraction

Left-to-right association

Full details in Appendix B

Also see Programming Tip 1.11 on p. 31 in text

 Full details in Appendix B  Also see Programming Tip 1.11 on p. 31 in
 Full details in Appendix B  Also see Programming Tip 1.11 on p. 31 in

Example

5 + (4

-

3) / 5

Example 5 + (4 - 3) / 5 - 2 * 3 % 4 EECS1020 F14
Example 5 + (4 - 3) / 5 - 2 * 3 % 4 EECS1020 F14

-

2 *

3

% 4

Example

 

5 + (4

-

3) / 5

2 *

-

2 *

3

% 4

=

5 +

1

/ 5

-

3

% 4

(4 - 3) / 5 2 * - 2 * 3 % 4 = 5 +
(4 - 3) / 5 2 * - 2 * 3 % 4 = 5 +

Example

 

5 + (4

-

3) / 5

2 *

-

2 *

3

% 4

=

5 +

1

/ 5

-

3

% 4

+ (4 - 3) / 5 2 * - 2 * 3 % 4 = 5
+ (4 - 3) / 5 2 * - 2 * 3 % 4 = 5
+ (4 - 3) / 5 2 * - 2 * 3 % 4 = 5
+ (4 - 3) / 5 2 * - 2 * 3 % 4 = 5

Example

5 + (4

-

3) / 5

2 *

-

2 *

3

% 4

= 5 +

1

/ 5

-

3

% 4

= 5 +

0

- 2

*

3 %

4

* 3 % 4 = 5 + 1 / 5 - 3 % 4 = 5
* 3 % 4 = 5 + 1 / 5 - 3 % 4 = 5

Example

5 + (4

-

3) / 5

2 *

-

2 *

3

% 4

= 5 +

1

/ 5

-

3

% 4

= 5 +

0

- 2

*

3 %

4

* 3 % 4 = 5 + 1 / 5 - 3 % 4 = 5
* 3 % 4 = 5 + 1 / 5 - 3 % 4 = 5
* 3 % 4 = 5 + 1 / 5 - 3 % 4 = 5

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

5 + = 5 + 1 0 / 5 - 2 - 3 % 3 4
5 + = 5 + 1 0 / 5 - 2 - 3 % 3 4

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

= 5 + = 5 + 1 0 / 5 - 2 - 3 % 3
= 5 + = 5 + 1 0 / 5 - 2 - 3 % 3
= 5 + = 5 + 1 0 / 5 - 2 - 3 % 3

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

= 5 +

0

- 2

/ 5 - 2 - 3 % 3 4 % 4 = 5 + 0 -
/ 5 - 2 - 3 % 3 4 % 4 = 5 + 0 -

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

= 5 +

0

- 2

0 / 5 - 2 - 3 % 3 4 % 4 = 5 + 0
0 / 5 - 2 - 3 % 3 4 % 4 = 5 + 0
0 / 5 - 2 - 3 % 3 4 % 4 = 5 + 0

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

= 5 +

0

- 2

= 5 -

2

- 3 % 3 4 % 4 = 5 + 0 - 6 % 4 =
- 3 % 3 4 % 4 = 5 + 0 - 6 % 4 =

Example

5 + (4

-

3) / 5

2 *

*

-

2 *

3

% 4

= 5 +

= 5 +

1

0

/ 5

- 2

-

3 %

3

4

% 4

= 5 +

0

- 6

% 4

= 5 +

0

- 2

= 5 -

2

= 3

3 % 3 4 % 4 = 5 + 0 - 6 % 4 = 5
3 % 3 4 % 4 = 5 + 0 - 6 % 4 = 5
double float long int EECS1020 F14 (Steven C.) 29

double

double float long int EECS1020 F14 (Steven C.) 29
float
float
float

float

double float long int EECS1020 F14 (Steven C.) 29
long
long
long

long

double float long int EECS1020 F14 (Steven C.) 29
int

int

double float long int EECS1020 F14 (Steven C.) 29
double float long int EECS1020 F14 (Steven C.) 29
double float long int EECS1020 F14 (Steven C.) 29
double float long int EECS1020 F14 (Steven C.) 29
double float long int EECS1020 F14 (Steven C.) 29
 Returns compile-time exception double aDbl = 5.0; int bInt = 2; int result =

Returns compile-time exception

double aDbl = 5.0; int bInt = 2; int result = aDbl * bInt;

Demotion accomplished with casting

double aDbl = 5.0; int bInt = 2; int result = (int) aDbl * bInt;

Promotion via casting to force real division

2 / 3 = 0,

0.66666…

(double) 2 / 3 = 2 / (double) 3 =

via casting to force real division 2 / 3 = 0, 0.66666… (double) 2 / 3
via casting to force real division 2 / 3 = 0, 0.66666… (double) 2 / 3
The reserved words are the keywords plus the literals: true , false , null Keywords

The reserved words are the keywords plus the literals:

true, false, null

are the keywords plus the literals: true , false , null Keywords Must not be a

Keywords

Must not be a reserved word, must begin with a letter, and its character set must be: {0-9, A-Z, a-z, , _}

Recognized by the presence of a number, 'character', "characters", or one of: true, false, null

 

The character set of operators:

 

= >

<

!

~

?

:

&

|

+

-

*

/

^ %

The separators are:

 

.

,

;

(

)

[

]

{

}

  . , ; … ( ) [ ] { } Identifiers Literals Operators Separators EECS1020

Identifiers

Literals

. , ; … ( ) [ ] { } Identifiers Literals Operators Separators EECS1020 F14

Operators

. , ; … ( ) [ ] { } Identifiers Literals Operators Separators EECS1020 F14

Separators

. , ; … ( ) [ ] { } Identifiers Literals Operators Separators EECS1020 F14