Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Java structural components

Similar presentations


Presentation on theme: "Basic Java structural components"— Presentation transcript:

1 Basic Java structural components
Chapter 3 Basic Java structural components

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

3 Creating a Software System
Define the classes that will provide the objects. 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.

4 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.

5 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.

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

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

8 Identifiers (cont.) Legal: Illegal:
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

9 Identifiers already in “use”
Called reserved or key words For example: package if class else public for int float interface abstract private boolean char import implements extends

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

11 Choosing identifiers (cont.)
Be as specific as possible. Take particular care to distinguish closely related entities. Effective Less-Effective newMasterRecord masterRecord1 oldMasterRecord masterRecord2 Don’t incorporate the name of its syntactic category in its name. Not Recommended: StudentClass

12 Literals Sequences of characters that denote particular values.
We include literals in our programs to denote specific values. They represent values in a “literal” sense

13 Type int Literals Whole numbers -- both positive and negative.
Commas, periods, and leading zeros are not allowed in ‘int’s. Legal: Illegal: 123,

14 Type double Literals Numbers that involve decimal points. 0.5 -2.67
2. .6 Digits before and after the decimal point are preferred.

15 Exponential Notation Used to represent double values.
The “e” can be upper or lower case The mantissa need not contain a decimal point

16 Type char literals Single characters between apostrophes (single quotes). ‘A’ ‘a’ ‘2’ ‘;’ ‘ ’ 3 characters cannot be represented as themselves: ‘  ‘\’’ (apostrophe) “  ‘\”’ (quotation mark) \  ‘\\’ (backslash)

17 Type boolean literals Only 2 possible literals true (Not TRUE or True)
false

18 General lexical rules A source file is made up of tokens: identifiers, keywords, literals, and punctuation marks. Spaces and line ends are somewhat arbitrary. “White” spaces (tabs, carriage returns, spaces) are required between words: Wrong: publicclass Student

19 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 {

20 General lexical practices
Be consistent in spacing and line endings to make your programs as readable as possible. Use uniform indentation levels to indicate logical levels in code. Adhere to accepted industry style conventions for added readability.

21 Comments Explanatory remarks are included in a program for the benefit of human readers, but are ignored by the compiler. Use // to treat the remainder of the line as a comment. Use /* and */ to begin and end a section or multi-line comment. /* This is a multi-line or section comment */

22 To recap Fundamental structure of a Java program. Packages
Class definitions and compilation units Instances of classes Lexical structure Identifiers Literals Comments


Download ppt "Basic Java structural components"

Similar presentations


Ads by Google