Overview-Java
S.REVATHI AP/CSE
Entering the Program
A source file is officially called a compilation unit. It is a text file that
contains one or more class definitions.
The Java compiler requires that a source file use the .java filename
extension.
In Java, all code must reside inside a class.
Name of that class should match the name of the file that holds the
program.
Java is case-sensitive.
Make sure that the capitalization of the filename matches the class
name
Compiling the Program
To compile the Example program,
C:\>javac Example.java
The javac compiler creates a file called Example.class that contains
the bytecode version of the program.
To run the Example program
C:\>java Example
1st simple program
2nd simple program
CODING DEMOS
Two Control Statements
1.if Statement:
The Java if statement works much like the IF statement in any other
language.
Syntactically identical to the if statements in C, C++, and C#.
if(condition) statement;
Here, condition is a Boolean expression. If condition is true, then the
statement is executed.If condition is false, then the statement is bypassed.
Example:
if(num < 100) System.out.println("num is less than 100");
2.for Loop
for(initialization; condition; iteration) statement;
Example:
for(x=0; x<10; x=x+1)
Using Blocks of Code
Java allows two or more statements to be grouped into blocks of code, also called code
blocks.
This is done by enclosing the statements between opening and closing curly braces.
Once a block of code has been created, it becomes a logical unit that can be used any
place that a single statement can.
Example:
if(x < y) {
// begin a block
x = y;
y = 0;
} // end of block
Lexical Issues
Java programs are a collection of:
whitespace
identifiers
literals
comments
operators
separators
keywords.
Whitespace
Java is a free-form language. This means we do not need to follow
any special indentation rules.
For instance, the Example program could have been written all on
one line or in any other strange way you felt like typing it,
At least one whitespace character between each token
In Java, whitespace is a space, tab, or newline
Identifiers
Identifiers are used for class names, method names, and variable names.
An identifier may be any descriptive sequence of uppercase and lowercase
letters, numbers, or the underscore and dollar-sign characters.
They must not begin with a number, lest they be confused with numeric literal.
Again, Java is case-sensitive, so VALUE is a different identifier than Value.
AvgTemp count a4 $test this_is_ok
2count high-temp Not/ok
Literals
A constant value in Java is created by using a literal
representation of it.
For example, here are some literals:
100 98.6 'X' "This is a test"
Comments
Single-line
Multiline
Documentation comment.
This type of comment is used to produce an HTML file that
documents your program.
The documentation comment begins with a /** and ends with a
*/.
Separators
The Java Keywords
Java reserves the following: true, false, and null
The Java Class Libraries
Two of Java’s built-in methods in Example:println( ) and print( )
These methods are members of the System class, which is a
class predefined
Java environment relies on several built-in class libraries that
contain many built-in methods that provide support for: I/O,
string handling, networking, and graphics.
Learning to use the standard Java classes