Basic Elements of Java
Basic Elements of Java
Basic Syntax
Case
Class
Basic Syntax
Method
Program
Basic Syntax
public
Package
Program Structure
Program Structure
To
Program Structure
Hello World
A Java Program
Sample Run
Java Class
The
Java class
Java Class
The
ASimpleJavaProgram
Java class.
The
Java Class
Method main
Method main
The
Method main
The
Declaration
Executable
Programming
Comments
Typically,
Comments
compiler.
Comments
ASimpleJavaProgram,
given in our
example, contains the following comments:
Two
single-line comments
multiple-line comments
Single-line Comments
Single-line
comments begin
with // and can be placed anywhere
in the line.
Everything encountered in that line
after // is ignored by the compiler
You can put comments at the end of
this line as follows:
System.out.println("7 + 8 = " + (7 + 8)); //prints:
7 + 8 = 15
Multiple-line Comments
Multiple-line
Special Symbols
In
Semicolons
are used to
end a Java statement.
The
Reserved Words
(Keywords)
Reserved
words
are also called
keywords.
Reserved
words
cannot be
redefined within
any program;
that is, they
cannot be used
for anything
other than their
intended use.
Identifiers
A
Identifier:
Most
Identifiers
The
first
conversion
payRate
counter1
$Amount
Identifiers
For
For
In
Avoid
Data Types
The
Data
Which
No
Recall
When
Note
The
(Boolean) values
Default value: false
The
Floating-point Data
Types
To
float vs double
float:
double:
number
of decimal places
float values is 6 or 7.
double type is typically 15.
class String
String
manipulate a string.
the class String is not a primitive type
String
Null
The
When
The
The
Evaluation
"Sunny Day"
Sample Program
Named Constant
Variables
In
Variable:
Variable Types
There
Java:
Local variables
Instance variables
Class/static variables
Local vs Instance vs
Class
Local vs Instance vs
Class
Sample Program
In
this example:
andhypSquared
are local variables
intestRightTriangle. They are only
valid intestRightTriangle.
Variables
Initialize
Assignment Statement
Assignment Statements
SAMPLE
PROGRAM
Assignment Statements
Assignment Statement
Output Statements
In
Output Statements
System.out.println();
or System.out.print("\n");
line.
Syntax
Errors
//Line 1
int y
//Line 2
double z; //Line 3
y = w + x; //Line 4
When
Note
USE OF SEMICOLONS,
BRACES, AND COMMAS
In
Semantics
The
For
If
Prompt Lines
Prompt
DebuggingCode Walkthroughs
As