A Java Syntax Primer Yong Choi School of

  • Slides: 14
Download presentation
A Java Syntax Primer Yong Choi School of Business CSU, Bakersfield

A Java Syntax Primer Yong Choi School of Business CSU, Bakersfield

Class Names Descriptive è Case sensitive è Camelback: Order. Line. Item è Start with

Class Names Descriptive è Case sensitive è Camelback: Order. Line. Item è Start with upper case letter • Each Class must be stored in a file of the same name with the suffix ‘. java’: Order. Line. Item. java è

Basic Layout of a Class Package statement Import Statements Class declaration { Class-level variable

Basic Layout of a Class Package statement Import Statements Class declaration { Class-level variable declarations Method declaration { Method statements } }

Statements n n All the work in a Java class happens in statements. Every

Statements n n All the work in a Java class happens in statements. Every statement ends in a semicolon. A statement can cross multiple lines (but cannot break in the middle of a string literal). Statements live inside of code blocks.

Code Blocks n Code blocks live inside of various kinds of declarations: n n

Code Blocks n Code blocks live inside of various kinds of declarations: n n n Class Method Try Catch Conditional Iteration

Code Blocks n n Each code block is delimited by curly brackets Note the

Code Blocks n n Each code block is delimited by curly brackets Note the position of opening and closing brackets in sample code

Code Blocks Method code blocks can contain n n Local variable declarations Statements Conditional

Code Blocks Method code blocks can contain n n Local variable declarations Statements Conditional declarations, each with its own code block Iteration (loop) declarations, each with its own code block Try and Catch declarations, each with its own code block

Code blocks n Code blocks inside of declarations can in turn contain: n n

Code blocks n Code blocks inside of declarations can in turn contain: n n n Local variable declarations Statements Conditional declarations, each with its own code block Iteration (loop) declarations, each with its own code block Try and Catch declarations, each with its own code block

Code Blocks and Data Variables n n n A variable is accessible only within

Code Blocks and Data Variables n n n A variable is accessible only within its own code block (which includes any other code blocks nested within it) A local variable overrides like-named variables in a higher level code block A variable is deleted at the end of the code block within which it was declared

Comments /* This is a comment that can span multiple lines */ // This

Comments /* This is a comment that can span multiple lines */ // This comment lasts till end of line

Comments /* This nested comment won’t work /* Because the second start symbol is

Comments /* This nested comment won’t work /* Because the second start symbol is ignored and the first end symbol ends the whole comment */ so this is not part of the comment */

Expressions An expression is any part of a statement that can be evaluated into

Expressions An expression is any part of a statement that can be evaluated into a single value An example would be an arithmetical expression: eg, 2*3 evaluates to 6.

Expressions are used constantly in Java. n n n Every variable reference is really

Expressions are used constantly in Java. n n n Every variable reference is really an expression that evaluates to the value of the variable. Any operation (manipulation of variables using operators) is an expression. Any method invocation that returns a value can be considered an expression.

Expressions n n Expressions can be nested Nested expressions are evaluated from the inside

Expressions n n Expressions can be nested Nested expressions are evaluated from the inside out Example: int. Book. ID = ((Integer. value. Of((String)ht. Fields. get(” Book. ID"))). int. Value()); n n Above is fine (see bold and underlined) But below is not OK. There is a syntax error. int. Book. ID = ((Integer. value. Of((String)ht. Fields. get( ”Book. ID"))). int. Value());