Java
is a high-level programming language developed by Sun Microsystems
was developed at Sun Microsystems, Inc. by James Gosling
With the goals it had achieved (smallness, speed, reliability, portability), Java was
discovered to be ideal for web pages
can also be used to write stand-alone applications
Java
Technology
A programming language
A development environment
An application environment
A deployment environment
Features of Java
Object-oriented
Portable and Platform-Independent
Dynamically Linked
Multithreaded
Garbage collected
Java Requirements
Pentium 166MHz or faster processor
At least 48MB of RAM
o Using an applet within Web Browser with JDK(Java Development Kit)plug-in
At least 64MB of RAM
o Graphics based application
Java can be used to create two types of programs:
1. Applet special applications designed to run within the context of a Web Browser
2. Application stand-alone program that does not need a browser to run
Java Program Cycle
Use any text editor to create source code - .java file
Source code compiled according to JVM(Java Virtual Machine) to get byte code .class file
Byte code executed using an interpreter
Source code
Byte code
COMPILE
output
INTERPRETE
Java Coding Guidelines
Java files must
o Have same as public class
o End with expression .java
Use comments for documentation and readability
White spaces are ignored
Indent for readability
Java Statements
Java
A statement is one or more lines of code terminated by a semi-colon.
Example:
System.out.print(Hello,
World!);
Blocks
A block is formed by enclosing statements by curly braces
Block statements can be nested indefinitely
Any amount of white space is allowed
Example:
public static void main(String args[])
{
System.out.println(Hello);
System.out.print(World!);
}
Java Comments
A comment is an optional statement used to describe what a program or a line of
program is doing.
//This is a comment
/* This is a comment */
/**This is a special comment **/
/**for documentation**/
Example:
public class Hello{
/**
* My first Java program
*/
public static void main(String args[])
{
//print Hello, World on screen
System.out.print(Hello, World!);
}
}
Java
Identifiers
Identifiers used to label variables, methods, classes, etc.
Case-sensitive
May contain letters, digits, underscore and dollar sign($)
May not start with a digit
May not use Java keywords
Java Identifier Rules and Guidelines
Rules:
Identifiers can use alphabetic characters of either case (a-z and A-Z), numbers (0-9),
underscores(_), and a dollar signs($).
Identifiers cannot start with a number
Keywords cannot be used as identifiers (for this reason keywords are sometimes
called reserved words).
Guidelines:
Name your identifiers close to its functionality
Method and variable names start in lowercase while classes start in uppercase.
For multi-word identifiers, either use underscores to separate the words, or capitalize
the start of each word
Avoid starting the identifiers using the underscore
Keywords are words with strict meaning and cannot be used for other purpose.
Example:
Java
abstract
do
switch
else
void
int
Boolean
break
const
try
static
String
public
private
throw
Literals are representation of values
Integers
Floating point numbers
Booleans (true or false)
Strings (enclosed in )
Characters (enclosed in )
Escape sequence
\n
\t
\r
\
\
\\
new line
tab
carriage return
double quote
single quote
backslash
Constants value never changes; use the final type modifier in class definition
public class Variables{
public static void main(String args[])
{
final double PI = 3.14;
}
Java
expressions
An expression produces a result and returns a value
An expression can be any combination of variables, literals and operators.
Purposes for expressions:
o To compute values
o To assign values to variables
o To control the flow of execution
Examples:
X = 5;
Y = X;
Z = X * Y;
Java Operators
Operators are special symbols that perform specific operations on one, two or three
operands and then return a result.
Operators used are:
o Arithmetic
o Increment and Decrement
o Assignment
o Relational
o Logical
Arithmetic Operators
Example:
X = 6;
//assign 6 to X
+
Addition
Y = 4;
// assign 4 to Y
Subtraction
X = X + 2;
//X is equal to
*
Multiplication
8
/
Division
Y = Y 3;
//Y is equal to
%
Modulo
1
Z = X * Y;
//Z equal to 8
Z = X / Y;
//Z equal to 8
Z = X % Y;
//Z equal to 0
Increment and Decrement Operators
++
--
Pre/Post Increment
Pre/Post Decrement
Example:
Num = 5;
Z = Num++;
//Z=5;
Num = 5;
Z = ++Num;
//Z=6;
Relational Operators
== Equal to
<
Less than
>
Greater than
<= Less than or equal to
>= Greater than or
equal to
!=
Not equal to
Example:
Expression
3+4 == 7
3+4 != 7
6+4 >= 10
6+4 > 10
Result
True
False
True
False
Logical Operators
&&
||
^
!
AND
OR
Exclusive-OR
NOT
Example:
Expression
(3+4 == 7) && (6+2==8)
(3+4 != 7) || (6+4>10)
!(6+4 >= 10)
Result
True
False
False
Operator Precedence
Operator
()
[]
++ - ++ - *
/
%
+
> < >= <=
== !=
&&
||
?:
= += -= *= /= %=
Type
Parenthesis
Array Subscript
Member access
Prefix increment,
decrement
Postfix increment,
decrement
Unary minus
Multiplication
Division
Modulo
Addition
Subtraction
Relational
Equality
AND
OR
Conditional
Assignment
Order of Evaluation
left to right
right to left
right to left
left to right
left to right
left to right
left to right
left to right
left to right
right to left
right to left