Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "A Java Syntax Primer Yong Choi School of Business CSU, Bakersfield."— Presentation transcript:

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

2 Class Names  Descriptive  Case sensitive  Camelback: OrderLineItem  Start with upper case letter Each Class must be stored in a file of the same name with the suffix ‘.java’: OrderLineItem.java

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

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

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

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

7 Code Blocks Method code blocks can contain 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

8 Code blocks Code blocks inside of declarations can in turn contain: 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

9 Code Blocks and Data Variables 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

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

11 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 */

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

13 Expressions Expressions are used constantly in Java. 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.

14 Expressions Expressions can be nested Nested expressions are evaluated from the inside out Example: intBookID = ((Integer.valueOf((String)htFields.get(” BookID"))).intValue()); Above is fine (see bold and underlined) But below is not OK. There is a syntax error. intBookID = ((Integer.valueOf((String)htFields.get( ”BookID"))).intValue());


Download ppt "A Java Syntax Primer Yong Choi School of Business CSU, Bakersfield."

Similar presentations


Ads by Google