Compile Interprete Source Code Byte Code Output
Compile Interprete Source Code Byte Code Output
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
Source code
Byte code
COMPILE
output
INTERPRETE
Java
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
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
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
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