Java Programming - Unit I Java: SS Govt. Arts College
Java Programming - Unit I Java: SS Govt. Arts College
Java Programming - Unit I Java: SS Govt. Arts College
JAVA
HISTOY OF JAVA
Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to implement a virtual machine and a language that had a
familiar C like notation but with greater uniformity and simplicity than C/C++.
The First publication of Java 1.0 was released by Sun Microsystems in 1995.
It made the promise of "Write Once, Run Anywhere", with free runtimes on
popular platforms.
In 2006-2007 Sun released java as open source and and plateform independent
software.
Over time new enhanced versions of Java have been released.
The current version of Java is Java 1.7 which is also known as Java 7.
JVM (JAVA VIRTUAL MACHINE) & JRE( JAVA RUN TIME ENVIRONMENT)
Java is a simple language because of its various features, Java Doesnt Support
Pointers, Operator Overloading etc.
It doesnt require unreferenced object because java support automatic garbage
collection.
Java provides bug free system due to the strong memory management.
2) Object-Oriented
1|Page
SS
Govt.
Arts
College
o Polymorphism
o Inheritance
o Abstraction
As the languages like Objective C, C++ fulfills the above four characteristics yet
they are not fully object oriented languages because they are structured as well
as object oriented languages
In java everything is an Object.
Java can be easily extended since it is based on the Object model.
3) Secure
Java is Secure Language because of its many features it enables to develop virusfree, tamper-free systems.
Authentication techniques are based on public-key encryption.
Java does not support pointer explicitly for the memory.
All Program Run under the sandbox.
4) Robust
5) Platform-independent
6) Architecture neutral
It is not easy to write an application that can be used on Windows , UNIX and a
Macintosh.
Its getting more complicated with the move of windows to non Intel CPU
architectures.
Java takes a different approach. Because the Java compiler creates byte code
instructions that are subsequently interpreted by the java interpreter, architecture
neutrality is achieved in the implementation of the java interpreter for each new
architecture.
7) Portable
2|Page
SS
Govt.
Arts
College
This is in direct contrast to languages like C and C++ that leave the sized of
primitive types up to the compiler and developer.
Additionally, Java is portable because the compiler itself is written in Java.
8) Dynamic
9) Interpreted
For all but the simplest or most infrequently used applications, performance is
always a consideration for most applications, including graphics-intensive ones
such as are commonly found on the WWW, the performance of java is more than
adequate.
11) Multithreaded
Writing a computer program that only does a single thing at a time is an artificial
constraint that weve lived with in most programming languages.
With java, we no longer have to live with this limitation. Support for multiple,
synchronized threads is built directly into the Java language and runtime
environment.
Synchronized threads are extremely useful in creating distributed, network-aware
applications. Such as application may be communicating with a remote server in
one thread while interacting with a user in a different thread.
12) Distributed
SS
Govt.
Arts
College
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Object
Any entity that has state and behavior is known as an object.
Class
Collection of objects is called class.
It is a logical entity.
Inheritance
When one object acquires all the properties and behaviours of parent
object i.e. known as inheritance.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks
woof etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction.
4|Page
SS
Govt.
Arts
College
Encapsulation
Binding (or wrapping) code and data together into a single unit is known
as encapsulation.
Java bean is the fully encapsulated class because all the data members are
private here.
JAVA TOKENS
Java tokens are the smallest individual units that form part of a Java Program. Different
types of java tokens are:
Identifiers Identifiers are java tokens designed and decided by the java
programmer. Examples for java tokens namely identifiers are: name for the class,
name for members of the class, and temporary variables in class methods.
Literals Literals are java tokens containing set of characters. Literals are used
to represent a constant that has to be stored in a variable.
Separators Separators are java tokens that are used to divide as well as
arrange codes in group.
VARIABLE
A variable provides us with named storage that our programs can manipulate.
Each variable in Java has a specific type, which determines the size and layout of
the variable's memory.
The range of values that can be stored within that memory and the set of
operations that can be applied to the variable.
Syntax:
data type variable [ = value][, variable [= value] ...] ;
Example:
int a, b, c;
5|Page
SS
Govt.
Arts
College
Size in Bytes
Range
byte
1 byte
-128 to 127
short
2 bytes
-32,768 to 32,767
int
4 bytes
long
8 bytes
-9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
float
4 bytes
approximately 3.40282347E+38F
double
8 bytes
approximately 1.79769313486231570E+308
char
2 byte
0 to 65,536 (unsigned)
boolean
not
defined*
ARRAYS
An array is a group of like-typed variables that are referred to by a common name.
Arrays of any type can be created and may have one or more dimensions.
A specific element in an array is accessed by its index.
Arrays offer a convenient means of grouping related information.
There are two types of array.
Single Dimensional Array
type var-name[ ];
array-var = new type [size];
eg.
int month_days[];
month_days[1] = 28;
Multidimensional Array
type var-name [size][size]=new type[size][size];
eg.
int twoD[][] = new int[4][5];
JAVA TYPE CASTING
6|Page
SS
Govt.
Arts
College
Widening Casting(Implicit)
7|Page
SS
Govt.
Arts
College
8|Page
SS
Govt.
Arts
College
iteration
jump.
Selection statements allow your program to choose different paths of execution based
upon the outcome of an expression or the state of a variable.
Iteration statements enable program execution to repeat one or more statements (that is,
iteration statements form loops).
Jump statements allow your program to execute in a nonlinear fashion.
9|Page
SS
Govt.
Arts
College
Example
// Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}
Here is the output produced by the program:
April is in the Spring.
switch Statement
10 | P a g e
SS
Govt.
Arts
College
SS
Govt.
Arts
College
Here, the case 1: statement in the inner switch does not conflict with the case 1:
statement in the outer switch. The count variable is compared only with the list of cases
at the outer level. If count is 1, then target is compared with the inner list cases.
Important features of the switch statement
The switch differs from the if in that switch can only test for equality, whereas if
can evaluate any type of Boolean expression. That is, the switch looks only for a
match between the value of the expression and one of its case constants.
No two case constants in the same switch can have identical values. Of course, a
switch statement and an enclosing outer switch can have case constants in
common.
A switch statement is usually more efficient than a set of nested ifs.
ITERATION STATEMENTS
while Statement
The while loop is Javas most fundamental loop statement. It repeats a statement or
block while its controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
The condition can be any Boolean expression. The body of the loop will be executed
as long as the conditional expression is true. When condition becomes false, control
passes to the next line of code immediately following the loop. The curly braces are
unnecessary if only a single statement is being repeated.
Example
// Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
do-while Statement
In while statement, if the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all. However, sometimes it is desirable
to execute the body of a loop at least once, even if the conditional expression is false to
begin with. In other words, there are times when you would like to test the termination
expression at the end of the loop rather than at the beginning. Java supplies a loop that
does just that: the do-while. The do-while loop always executes its body at least once,
because its conditional expression is at the bottom of the loop.
12 | P a g e
SS
Govt.
Arts
College
do {
// body of loop
} while (condition);
Each iteration of the do-while loop first executes the body of the loop and then
evaluates the conditional expression. If this expression is true, the loop will repeat.
Otherwise, the loop terminates. As with all of Javas loops, condition must be a Boolean
expression.
Example
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}
for Statement
Loop statements are an important part of nearly any programming language. Java supplies a
powerful assortment of loop constructs. Perhaps the most versatile is the for loop.
When the loop first starts, the initialization portion of the loop is executed.
Generally, this is an expression that sets the value of the loop control variable, which
acts as a counter that controls the loop.
It is important to understand that the initialization expression is executed only once.
Next, condition is evaluated. This must be a Boolean expression.
It usually tests the loop control variable against a target value. If this expression is
true, then the body of the loop is executed.
If it is false, the loop terminates. Next, the iteration portion of the loop is executed.
This is usually an expression that increments or decrements the loop control variable.
The loop then iterates, first evaluating the conditional expression, then executing the
body of the loop, and then executing the iteration expression with each pass.
This process repeats until the controlling expression is false.
Example
class ForTick {
public static void main(String args[]) {
int n;
for(n=10; n>0; n--)
System.out.println("tick " + n);
}
}
13 | P a g e
SS
Govt.
Arts
College
Nested Loops
Java allows loops to be nested. That is, one loop may be inside another.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();
}
}
}
Jump Statements
Java supports three jump statements: break, continue, and return. These statements
transfer control to another part of your program.
Using break
In Java, the break statement has three uses. First, it terminates a statement sequence in
a switch statement. Second, it can be used to exit a loop. Third, it can be used as a
civilized form of goto.
Example
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
continue Statement
Sometimes it is useful to force an early iteration of a loop. That is, you might want to
continue running the loop but stop processing the remainder of the code in its body for
this particular iteration. This is, in effect, a goto just past the body of the loop, to the
loops end. The continue statement performs such an action. In while and do-while
loops, a continue statement causes control to be transferred directly to the conditional
expression that controls the loop. In a for loop, control goes first to the iteration portion
of the for statement and then to the conditional expression. For all three loops, any
intermediate code is bypassed.
Example
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
}
14 | P a g e
SS
Govt.
Arts
College
}
}
Return
The last control statement is return. The return statement is used to explicitly return
from a method. At any time in a method the return statement can be used to cause
execution to branch back to the caller of the method. Thus, the return statement
immediately terminates the method in which it is executed.
Example
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
15 | P a g e
SS
Govt.
Arts
College