Describe the components of a basic Java program
Identify the conventions to be followed in a Java program
Use Java reserved words
Use single-line and multi-line comments in Java programs
In Java, almost everything is in classes, i.e., inside:
class {
...
}
There should generally be only one class per source file
And the names should match - class "Student" goes in file "Student.java"
JAVA IS CASE SENSITIVE Java Is Case Sensitive
The sooner you accept this reality, the better
There are a few things that can be expressed both in upper and lower case, but not many
VIDEO
Java source files contain:
Classes
Variables
Methods
Comments
Reserved words
Modifiers
Statements
Blocks
All will be introduced at the right time
A class consists of two fundamental kinds of things:
A class describes its objects
The data (variables) that each object of the class will have
The operations (methods) defined on those objects
A class is a way to model an OO concept
Let's learn more about the language first
VIDEO
Java classes are organized into packages
Packages are directories on the file system (more later)
Think "name space" or "surname"
A class without a package statement is in the default package
Class names must be unique within a package
The Java Core API library is divided up into packages
java.lang, java.util, java.io, etc.
Names used for classes, variables, and methods
Can be made up of any length of:
Letters
Digits
Underscore (_)
Dollar ($)
Cannot begin with a digit
Cannot be a Java keyword (we'll see these soon)
Java is case sensitive
"User" and "user" are completely different identifiers
Valid identifiers:
MyClass
myVariable
MY_VARIABLE
x
_myvariable
$myvariable
Invalid identifiers:
My Variable
9pins
a+c
"+" is not an alphanumeric character
testing1-2-3
"-" is not an alphanumeric character
VIDEO
While not strictly required (by the compiler), Java naming conventions are pretty much law
Class names begin with a capital letter
Variable and method names begin with a lowercase letter
In both cases, subsequent "words" in the name are capitalized
Class examples:
String, System, Person, HelloWorld, SecurityManager
Variable examples:
name, age, initialCount, numWords, bufferSize
Method examples:
getAge(), setAge(), println(), flushBuffer(), maxVal()
VIDEO
Multi-line or "block"
Begin with /* and end with */
Cannot nest these (why not?)
Single line
Begin with //
Continues to end of line
Javadoc documentation comments
Begin with /** and end with */
Read by javadoc tool to generate HTML-based API doc (later)
// single-line comment
/*
this comment spans
several lines
*/
VIDEO
Declare a class with a valid identifier that follows java naming convention.
Use comments to list 3 vaild identifiers for classes, methods, and variables.
Use comments to list 3 invaild identifiers for classes, methods, and variables.
VIDEO
Spaces, blank lines, and tabs
Used to separate words and symbols in a program
Extra white space is ignored by the compiler
Add white space to make your code more readable
Code should be formatted to enhance readability, using consistent indentation
Which of the two code blocks below is easier to read?
class WhitespaceExample {
public static void main(String[] args) {
System.out.println("try to be neat"); /*
but somehow can't */
System.out.
println("good luck trying to maintain this "); System.
out.
println("code");
}}
class WhitespaceExample {
public static void main(String[] args) {
System.out.println("try to be neat");
/* but somehow can't */
System.out.println("good luck trying to maintain this ");
System.out.println("code");
}
}
VIDEO