Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 3 Basic Java structural components

2 This chapter discusses n Some Java fundamentals. n The high-level structure of a system written in Java. u packages u compilation units n Some fundamental tokens that make up a Java program. u identifiers u literals

3 Creating a Software System n Define the classes to which objects belong. n A class definition determines the features and behavior of the objects that are instances of the class. n A program source is a collection of class definitions.

4 Packages n A system definition is composed of a number of modules called packages. n A package is a collection of one or more closely related classes. n public class: a class that is accessible throughout the entire system.

5 Packages (cont.)

6 Compilation unit n A source file containing the definition of one or more classes of a package. n It can contain the definition of at most one public class.

7 Compilation unit (cont.)

8 Identifiers n Sequences of characters that can be used to name things in a Java program. u packages u classes u objects u features

9 Identifiers (cont.) A sequence of letters, digits, $ s, and/or _ s. n Cannot begin with a digit. Case sensitive ( A and a are considered different!!).

10 Identifiers (cont.) Legal: X Abc A_a_x b$2 aVeryLongIdentifier b29 a2b $_ $$$ IXLR8 Illegal: 2BRnot2B a.b Hello! A-a A+a All different identifiers: total Total TOTAL tOtAl

11 Identifiers used already

12 Choosing identifiers n Choose descriptive names. Student or Textbook not S or Thing n Avoid overly long identifiers. HoldsTheNumberOfIterationsOfLoop n Avoid abbreviations; if you abbreviate, be consistent. Inconsistent: clientRec and studentRecord

13 Choosing identifiers (cont.) n Be as specific as possible. n Take particular care to distinguish closely related entities. Effective Less-Effective newMasterRecord masterRecord1 oldMasterRecord masterRecord2 n Dont incorporate the name of its syntactic category in its name. Less-Effective: StudentClass

14 Literals n Sequences of characters that denote particular values. n We write literals in our programs to denote specific values.

15 int n Numbers -- both positive and negative. n Commas, periods, and leading zeros are not allowed in ints. Legal: 250 123456-2897657 Illegal: 123,45625.0014765

16 Double n Numbers including decimal points. 0.5 -2.67 0.00123 2..6 n Digits before and after the decimal point are preferred.

17 Exponential Notation n Can be used to represent doubles. 0.5e+3 0.5e-3 -0.5E3 5e4 2.0E-27 n The e can be upper or lower case. n The mantissa need not contain a decimal point.

18 Character literals n Single characters between apostrophes (single quotes). Aa2; n 3 characters not represented by themselves: -> \ (apostrophe) -> \ (quotation mark) \ -> \\ (backslash)

19 n Only 2 possible literals: true (Not TRUE or True) u false Boolean

20 General lexical rules n Files are made up of tokens -- identifiers, keywords, literals, and punctuation marks. n Spaces and line ends are somewhat arbitrary. n Spaces are required between words: Wrong: publicclass Student

21 General lexical rules (cont.) n Spaces are not required, but are permitted, around punctuation. All correct examples: public class Student{ a+b n Extra spaces and line endings are allowed. publicclassStudent{

22 General lexical practices n Be consistent in spacing and line endings to make your programs as readable as possible.

23 Comments n 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 */

24 Weve covered n Fundamental structure of a Java program. u Packages u Class definitions and compilation units u Instances of classes n Lexical structure u Identifiers u Literals n Comments

25 Glossary

26 Glossary (cont.)


Download ppt "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."

Similar presentations


Ads by Google