You are on page 1of 23

Training

in
Core Java
By
Rahul karmakar
2813231
ECE(4th year)

JAVA FUNDAMENTALS

Introduction to java
Principle of OOPs
JDK and JRE
Byte Code and JVM (Java Virtual Machine)
Platform Independent
Applications and Applets

History And Facts

Java was conceived by James Gosling and his partners in


1991 at Sun Microsystems
it was first named as oak
Surprisingly it was not meant for internet
Gosling and others began to work on a portable, platform
independent language that could work on variety of CPUs
under different environments

Features of Java :

Compiled and Interpreted.


Platform-Independent and Portable
Object-Oriented
Robust and Secure
Distributed
Familiar, Simple and Small
Multithreaded and Interactive
High Performance
Dynamic and Extensible

Contents
Fundamental Principles of OOP
1. Inheritance
2. Encapsulation
3. Polymorphism

Fundamental Principles of OOP

Inheritance

Encapsulation

Inherit members from parent class


Hide the internals of a class

Polymorphism
Access a class through its parent interface

Inheritance

Encapsulation
Encapsulation hides the implementation details
Class announces some operations (methods) available
for its clients its public interface
All data members (fields) of a class should be hidden

Accessed

via properties (read-only and read-write)

No interface members should be hidden

Polymorphism
1.

Polymorphism = ability to take more than one


form (objects have more than one type)
A class can be used through its parent
interface
ii. A child class may override some of the
behaviors of the parent class.
Polymorphism ensures that the appropriate
method of the subclass is called through its base
class' interface
i.

2.

Polymorphism How it Works?


Polymorphism ensures that the appropriate method of the
subclass is called through its base class' interface
Polymorphism is implemented using a technique called
late method binding

Exact

method to be called is determined at runtime, just before


performing the call
Applied for all abstract / virtual methods

Note: Late binding is slower than normal (early) binding


10

Byte Code & JVM(Java Virtual


Machine)
Since platform-independence is a defining characteristic of Java, it is important to understand
how it is achieved. Programs exist in two forms; source code and object code. Source Code is
the textual version of the program that you write using a text editor. The programs printed in a
book are shown as source code. The executable form of a program is object code. The computer
can execute object code. Typically, object code is specific to a particular CPU. Therefore, it
cannot be executed on a different platform. Java removes this feature in a very elegant manner.
Like all computer languages, a java program begins with its source code. The difference is
what happens when a Java program is compiled. Instead of producing executable code, the Java
Compiler produces an object file that contains bytecode. Bytecodes are instructions that are not
for any specific CPU. Instead, they are designed to be interpreted by a Java Virtual Machine
(JVM). The key to Javas platform-independence comes from the fact that the same bytecodes
can be executed by any JVM on any platform. As long as there is a JVM implemented for a a
given environment, it can run any Java program. For example, Java programs can execute under
Windows 98,Solaris,IRIX, or any other platform for which a JVM can be implemented for that
platform. This would then allow any Java program to execute in that new environment.

Java Program

Java Compiler

Source Code

Virtual Machine
Bytecode

Process of Compilation

Bytecode
Virtual Machine

Java Interpreter

Machine Code
Real Machine

Process of Converting bytecode into machine code

Application and Applets


There are two types of programs that can be built in Java Applications and applets.
Applications can be directly executed by a JVM. In fact, Java can be used to develop
programs for all kinds of applications, Hot Java itself is a Java Application program.
Applets are small Java programs developed for Internet Applications. An applet located
on distant computer (Server) can be downloaded via Internet and executed on a local
computer (Client) using a Java enabled browser. We can develop applets for doing
everything from simple animated graphics to complex games and utilities. Since applets
are embedded in an HTML document and run inside a Web Page, creating and running
applets are more complex than creating application. Stand alone program can read and
write files and perform certain operations that applet can not do. An Applet can only run
within a Web Browser. The Web browser includes a JVM that provides an execution
environment for the applet. It is also possible to use a tool called the appletviewer to
run an applet. This utility is included in the Java Development Kit(JDK) and is used to
test applets. In this manner, an applet written by any developer in the world may be
dynamically downloaded from the Web Server and executed on a client PC or
workstation.

Java
Source
Code

Java Compiler
Application Type

Applet Type
Java
Enabled
Browser

Output

Java
Interpreter

Output

JAVA CONTROLS

Variables and Constants


Arithmetic Operator and Expressions
Type Conversion in Java
Comments in Java(3 Types)
Javas Control Statements
If
If-else
Do-while
While
for
Increment and Decrement Operators
Escape Sequences Characters
Relational and Logical Operators
Ternary Operators
Switch case
Break
Bitwise Operators
Arrays-Single and Multidimensional

Java Control Statements


Control
Statement

Selection Statement

if

If-else

Iteration Statement

switch

Jump Statement

break

while

do

for

conti
nue

return

JAVA Classes and Methods

The General Form of a class


Constructor and Method Overloading
The new Operator
Garbage Collection
Finalize method
Command Line Arguments
The System Class

JAVA Classes

The class is the fundamental concept in JAVA (and other OOPLs)


A class describes some data object(s), and the operations (or methods) that
can be applied to those objects
Every object and method in Java belongs to a class
Classes have data (fields) and code (methods) and classes (member classes
or inner classes)
Static methods and fields belong to the class itself
Others belong to instances

NSIT ,Jetalpur

Multithreading
Multitheading is specialized form of multitasking. like windows and other os uses multitasking
technique i.e. they handled more than one process at a time, the same thing we can do in Java
using multithreading technique.
A thread is similar to a program that has a single flow of control. It has a beginning, a body, and
an end,and executes commands sequentially. Java enables us to use multiple flows of control in
developing programs.
Each flow of control is represented by thread that runs parallel to others. i.e. a program that
contains multiple flows of control is known as multithreaded program.Threads in Java are
subprograms of a main application program and share the same memory space, known as
lightweight threads or lightweight process.
Threds running in parallel does not mean that they actually run at the same time. Since all the
threads are running on the same processor, the flow of execution is shared between the threads.
The Java interpreter handles the switching of control between the threads in such a way that it
appears they are running concurrently.
Creating Threads :
Threads are implemented in the form of objects that contain a method called run(). The run()
method
is the heart and soul of any thread.

LIFE CYCLE OF A THREAD


Newborn

New Thread

stop

start

stop

Running

Active
Thread

yield

Runnable

Dead
Killed
Thread

suspend
sleep
wait

Idle Thread
(Not Runnable

resume
notify

Blocked

stop

APPLET
An applet is a program that can be referenced by the html source code of web page. It is
dynamically downloaded from a Web Server to a browser. The applet then executes within the
environment provided by the browser. Alternatively you may use a tool such as the appletviewer
to run it.
It is important to recognize that downloading code from the Internet and executing it on your
computer is inherently dangerous. Therefore, applet do not have the same capabilities as Java
applications. They are restricted to operating within the confines of a sandbox. In other words
code that is untrusted is not allowed to operate outside certain boundaries.
For Example, applets are normally not allowed to read or write to your local disk. This would
obviously be risky because they could accidentally or maliciously destroy any data stored on that
device.Applet can not execute any native code.
An applet may open a socket connection back to the host from which it was downloaded, but not
to any other host. The reason for this restriction can be understood if you imagine a configuration
in which a firewall protects a corporate Intranet from computer hackers. Assume that an employee
has downloaded an applet from internet to an PC or workstation. If that applet is allowed to open
sockets to any machine, it would then have the potential to steal proprietary information and send
back to the hackers machine. This must be prevented. Therefore, an applet is not allowed to
contact any of those private machines.

Difference between Applet and Application


Applet are not full-featured application programs. They are usually written to accomplish a
small task or a component of a task. Since they are usually designed for use on the Internet,
they impose certain limitations and restrictions in their design.

Applet do not use main() method for initiating the execution of the code. Applets,
when
loaded, automatically call certain methods of Applet class to start and execute the applet code.

Unlike stand-alone applications, applet can not be run independently. They are
run from inside a web page using a special feature known as HTML tag.

Applets cannot red from or write to the files in the local computer.

Applets cannot run any program the local computer.

Writing Applet Program


1.

Building an applet code (.java file).

2.

Creating an executable applet(.class file).

3.

Designing a Web Page using HTML tags.

4.

Preparing <Applet> tag, Incorporating <applet> tag into the Web Page.

5.

Creating HTML file. Testing the applet code

Begin Applet

Initialization

Born

(Load Applet)

stop()

start()
Display
paint()

Running

Idle

destroy()

start()
Destroyed

Stopped

Dead

End

Initialization State : Applet enters the initialization state when it is first loaded. This is achieved by calling
the init() method of Applet Class.The applet is born. We required following at this stage :
Create objects needed by the applet.
Set up initial values
Load images or fonts
Set up colors

You might also like