Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The...

26
Chapter 3 Basic Java structural components

Transcript of Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The...

Page 1: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Chapter 3Basic Java structural components

Page 2: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

This chapter discusses Some Java fundamentals. The high-level structure of a system

written in Java. packages compilation units

Some fundamental “tokens” that make up a Java program. identifiers literals

Page 3: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Creating a Software System Define the classes to which objects

belong. A class definition determines the

features and behavior of the objects that are instances of the class.

A program source is a collection of class definitions.

Page 4: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Packages A system definition is composed

of a number of modules called packages.

A package is a collection of one or more closely related classes.

public class: a class that is accessible throughout the entire system.

Page 5: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Packages (cont.)

Page 6: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Compilation unit A source file containing the

definition of one or more classes of a package.

It can contain the definition of at most one public class.

Page 7: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Compilation unit (cont.)

Page 8: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Identifiers Sequences of characters that can

be used to name things in a Java program. packages classes objects features

Page 9: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Identifiers (cont.) A sequence of letters, digits, ‘$’s,

and/or ‘_’s. Cannot begin with a digit. Case sensitive (‘A’ and ‘a’ are

considered different!!).

Page 10: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Identifiers (cont.)

Legal: X Abc A_a_x b$2aVeryLongIdentifier b29 a2b $_ $$$ IXLR8

Illegal: 2BRnot2B a.b Hello! A-a A+a

All different identifiers: total Total TOTAL tOtAl

Page 11: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Identifiers used already

Page 12: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Choosing identifiers

Choose descriptive names.

Student or Textbook

not S or Thing Avoid overly long identifiers.HoldsTheNumberOfIterationsOfLoop Avoid abbreviations; if you

abbreviate, be consistent.Inconsistent: clientRec and studentRecord

Page 13: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Choosing identifiers (cont.) Be as specific as possible. Take particular care to distinguish

closely related entities.

Effective Less-EffectivenewMasterRecord masterRecord1

oldMasterRecord masterRecord2 Don’t incorporate the name of its

syntactic category in its name.

Less-Effective:StudentClass

Page 14: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Literals Sequences of characters that

denote particular values. We write literals in our programs to

denote specific values.

Page 15: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

int

Numbers -- both positive and negative.

Commas, periods, and leading zeros are not allowed in ‘int’s.

Legal:

25 0 123456 -289765 7

Illegal:

123,456 25.0 014765

Page 16: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Double

Numbers including decimal points.

0.5

-2.67

0.00123

2.

.6 Digits before and after the decimal

point are preferred.

Page 17: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Exponential Notation

Can be used to represent doubles. 0.5e+3 0.5e-3

-0.5E3

5e4

2.0E-27

The “e” can be upper or lower case. The mantissa need not contain a

decimal point.

Page 18: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Character literals

Single characters between apostrophes (single quotes).

‘A’ ‘a’ ‘2’ ‘;’ ‘ ’ 3 characters not represented by

themselves:

‘ -> ‘\’’ (apostrophe)

“ -> ‘\”’ (quotation mark)

\ -> ‘\\’ (backslash)

Page 19: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Only 2 possible literals: true (Not TRUE or True) false

Boolean

Page 20: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

General lexical rules

Files are made up of tokens -- identifiers, keywords, literals, and punctuation marks.

Spaces and line ends are somewhat arbitrary.

Spaces are required between words:Wrong: publicclass Student

Page 21: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

General lexical rules (cont.) Spaces are not required, but are

permitted, around punctuation.

All correct examples:

public class Student{

public class Student {

a+b

a + b Extra spaces and line endings are

allowed.

public class Student {

Page 22: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

General lexical practices

Be consistent in spacing and line endings to make your programs as readable as possible.

Page 23: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Comments

Explanatory remarks are included in a program for the benefit of a human reader, and are ignored by the compiler.

Use // to treat the rest of the line as a comment.

Use /* and */ to begin and end a section of comments.

/* This is a comment */

Page 24: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

We’ve covered

Fundamental structure of a Java program. Packages Class definitions and compilation units Instances of classes

Lexical structure Identifiers Literals

Comments

Page 25: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Glossary

Page 26: Chapter 3 Basic Java structural components. This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u.

Glossary (cont.)