Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Lesson 1- Java Basic Elements

– Program Elements

Lecture B Slide 1 of 69.


Identifiers: Syntax

• Identifiers are the words a programmer uses in a program


• Identifier syntactic rules:
 Can be made up of any length of
• letters
• digits
• underscore character (_)
• dollar sign ($)
 Cannot begin with a digit
• Java is case sensitive
 User and user are completely different identifiers

Lecture
CECSD B Slide 2 of 69.
Identifiers: Semantics

• Identifiers names can come from the following sources


 Fixed in Java as reserved words
• public, class, static, void, method, ...
 Chosen by the programmer to denote something
• HelloWorld, main, args
 Chosen by a programmer whose code we use:
• String, System, out, println

Lecture
CECSD B Slide 3 of 69.
Naming style

• The correctness of the program is not affected by the


names used
public class X7_65Tx
{
// your code here
}
• Names play a central role in the readability of the program
• They are part of its documentation
• They should thus be chosen carefully
 BankAccount, size, numberOfElements
• Follow conventions in choosing names!

Lecture
CECSD B Slide 4 of 69.
White Space

• Spaces, blank lines, and tabs are collectively called white


space
• White space is used to separate words and symbols in a
program
• Extra white space is ignored
• A valid Java program can be formatted many different
ways
• Programs should be formatted to enhance readability,
using consistent indentation

Lecture
CECSD B Slide 5 of 69.
Valid, but bad Indentation

public class
HelloWorld { public static void

main(String[]
args) {
System.out.println(“Hello World!”)
;}}

Lecture
CECSD B Slide 6 of 69.
Comments

• Comments are ignored and are treated as white space


• They should be written to enhance readability
 Explain what a piece of code does (its interface)
 Explain any special tricks, limitations, …
• Java has three comment formats:
 // comment to end of line
 /* comment until
closing */
 /** API specification
comment */

Lecture
CECSD B Slide 7 of 69.

You might also like