Professional Documents
Culture Documents
Java
Java
Created By
Manvendra Mishra
HISTORY OF JAVA
James Gosling, Mike Sheridan, and Patrick
Naughton initiated the Java language project in June 1991.
The language was initially the name Green and was finally
renamed Java, from Java coffee. Gosling designed Java with a
C/C++-style syntax that system and application programmers
would find familiar.
Sun Microsystems released the first public implementationas Java
1.0 in 1995. It promised "Write Once, Run Anywhere" (WORA),
providing no-cost run-times on popular platforms. Fairly secure
and featuring configurablesecurity, it allowed network- and file-
access restrictions. Major web browsers soon incorporated the
ability to run java applets within web pages, and Java quickly
became popular.
SIMPLE
Java is very easy to learn and its syntax is simple,clean
and easy to understand.
OBJECT
O RIENTED
Java is Object-oriented programming language. Everything in
Java is an object. Object-oriented means we organize our software
as a combination of different types of objectsthat incorporates
both data and behaviour. Object-oriented programming(OOPs) is
a methodology that simplifies software development and
maintenance by providing somerules. Basic concepts of OOPs
are:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
PLATFORM
INDEPENDENT
Java is platform independent because it is differentfrom
other languages like C, C++ etc. which are compiled
into platform specific machines while Java is a write
once, run anywhere language. A platform is the
hardware or software environment in which a program
runs.
SECUR
E
Java is best known for its security. With Java, wecan
develop virus-free systems.
OS
App JVM
PORTABL
E
Java is portable because it facilitates you to carrythe
java bytecode to any platform.
WHAT HAPPENS AT COMPILE TIME?
read bytecode
stream then
execute the
instructions.
DIFFERENCE BETWEEN JDK, JRE AND
JVM
JVM (Java Virtual Machine) is an abstract machine.It is a
specification that provides runtime environment in which
java bytecode can be executed.
JRE is an acronym for Java Runtime Environment. It is
used to provide runtime environment. It is the
implementation of JVM. It physically exists. It contains
set of libraries + other files that JVM usesat runtime.
JDK is an acronym for Java Development Kit. It
physically exists. It contains JRE + development
tools.
UNICODE
SYSTEM
Unicode is a universal international standard character encoding that iscapable
of representing most of the world's written languages.
Why java uses Unicode System?
Before Unicode, there were many language standards:ASCII (American
Standard Code for Information Interchange) for the United States.
ISO 8859-1 for Western European Language.
KOI-8 for Russian.
GB18030 and BIG-5 for chinese, and so on.
Problem
This caused two problems:A particular code value corresponds to
different letters in the various language standards.
The encodings for languages with large character sets have variable
length.Some common characters are encoded as single bytes, other require
two or more byte.
Solution
To solve these problems, a new language standard was developed i.e. Unicode
System.In unicode, character holds 2 byte, so java also uses 2byte for
characters.
lowest value: \u0000 highest value: \uFFFF
WHAT IS JAVA TOKENS?
Tokens are the various Java program elements which are
identified by the compiler. A token is the smallest
element of a program that is meaningful tothe compiler.
Tokens supported in Java include keywords, variables,
constants, special characters, operators etc.
TOKENS ARE THE SMALLEST UNIT OF
PROGRAMTHERE IS FIVE TYPES OF TOKENS
Reserve Word or Keywords
Identifier
Literals
Operators
Separators
White space
WHITESPAC
E
Whitespace in Java is used to separate the tokens in a Java
source file. Whitespace is required in some places,such as
between access modifiers, type names and Identifiers, and is
used to improve readability elsewhere.
if (testExpression)
{ // statements }
The if statement evaluates the test expression inside theparenthesis.
If the test expression is evaluated to true , statementsinside
the body of if is executed.
If the test expression is evaluated to false , statements inside
the body of if is skipped from execution.
If(testExpression)
{Statements}
Else
{Statements}
If test expression is true, codes inside the body
of if statement is executed and, codes inside the bodyof else
statement is skipped.
If test expression is false, codes inside the body
of else statement is executed and, codes inside the bodyof if
statement is skipped.
NESTED
IF
(If with in called nested if). A nested if is an if statement
that is the target of another if statement.Nested if
statements means an if statement insideanother if
statement.
If(test)
{
If(test)
{
}
else{ }
}
MULTIPLE IF
STATEMENT
If else if is called multiple if statement.
If(test)
{
Else{
If(test)
{
}
}
}
SWITCH
STATEMENT
Switch case statements are a substitute for long if statements that compare a variable to several integral
values
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution
to different parts of code based on the value of the expression.
Switch is a control statement that allows a value to change control of execution.
Syntax:
switch (n) { case 1: // code to be executed if n = 1; break; case 2: // code to be executed if n = 2; break;
default: // code to be executed if n doesn't match any cases }Important Points about SwitchCase Statements:
The expression provided in the switch should result in a constant value otherwise it would not bevalid.
Valid expressions for switch:
// Constant expressions allowed switch(1+2+23) switch(1*2+3%4)Invalid switch expressions for switch:
// Variable expression not allowed switch(ab+cd) switch(a+b+c)
Duplicate case values are not allowed.
The default statement is optional.Even if the switch case statement do not have a default statement,
it would run without any problem.
The break statement is used inside the switch to terminate a statement sequence. When a breakstatement is
reached, the switch terminates, and the flow of control jumps to the next line following the switch
statement.
The break statement is optional. If omitted, execution will continue on into the next case. The flow of control
will fall through to subsequent cases until a break is reached.
Nesting of switch statements are allowed, which means you can have switch statements inside another
switch. However nested switch statements should be avoided as it makes program morecomplex and less
readable.
LOOPS
Loops in programming comes into use when we need to repeatedlyexecute
a block of statements.
In computer programming, a loop is a sequence of instructions that is repeated
until a certain condition is reached.
An operation is done, such as getting an item of data and changing it, and then
some condition is checked such as whether a counter has reached a prescribed
number.
Counter not Reached: If the counter has not reached the desired number, the next
instruction in the sequence returns to the first instruction in the sequence and
repeat it.
Counter reached: If the condition has been reached, the next instruction “falls
through” to the next sequential instruction or branches outside theloop.
There are mainly two types of loops:
Entry Controlled loops: In this type of loops the test condition is testedbefore
entering the loop body. For Loopand While Loop are entry controlled loops.
Exit Controlled Loops: In this type of loops the test condition is tested or
evaluated at the end of loop body. Therefore, the loop body will execute atleast
once, irrespective of whether the test condition is true or false. do
– while loop is exit controlled loop.
FOR
L OOP
A for loop is a repetition control structure which allows us to write a loop
that is executed a specific number of times. The loop enables usto perform n
number of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr) { // body of the loop //
statements we want to execute } In for loop, a loop variable is used to control
the loop. First initialize this loop variable to some value, then check whether
this variable is less than or greater than counter value.If statement is true,
then loop body is executed and loop variable gets updated . Steps are
repeated till exit condition comes.
Initialization Expression: In this expression we have to initialize theloop
counter to some value. for example: int i=1;
Test Expression: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of loop and go to
update expression otherwise we will exit from the for loop.For example: i
<= 10;
Update Expression: After executing loop body this expression
increments/decrements the loop variable by some value. for example:i++;
WHILE
L OOP
While studying for loop we have seen that the number of
iterations is known beforehand, i.e. the number of times the
loop body is needed to be executed is known to us. while
loops are used in situations where we do not know the exact
number of iterations of loop beforehand. The loop execution is
terminated on the basis of test condition.
Syntax:
We have already stated that a loop is mainly consisted ofthree
statements – initialization expression, test expression, update
expression. The syntax of the three loops – For, while and do
while mainly differs on the placement of these three
statements.
initialization expression; while (test_expression) { //
statements update_expression; }
DO WHILE
L OOP
In do while loops also the loop execution is terminated on
the basis of test condition. The main difference between do
while loop and while loop is indo while loop the condition
is tested at the end of loop body, i.e do while loop is exit
controlled whereasthe other two loops are entry controlled
loops.
Note: In do while loop the loop body will execute atleast
once irrespective of test condition.
Syntax:
initialization expression;do
{
// statements update_expression;
} while (test_expression);