Wednesday, October 27, 2010

Introduction

 Java is related to C++, which is a direct descendent of C. From C, java derives its syntax.
Many of java’s object oriented features were influenced by C++.
 Java was conceived by James Gosling, Patrick Naughton, Ed Frank, and Mike Sheridan at
Sun Microsystems, Inc. in 1991. This language was initially called “Oak”, but was renamed
to “Java” in 1995.
 The primary motivation behind development of Java was the need for a platformindependent
language that could be used to create software to be embedded in various
consumer electronic devices, such as microwave ovens and remote controls.
 The output of Java compiler is not executable code. Rather, it is bytecode. Bytecode is a
highly optimized set of instructions designed to be executed by the Java run-time system,
which is called Java Virtual Machine (JVM). The JVM is an interpreter for bytecode.
 HotSpot technology of Sun provides a Just-In-Time (JIT) compiler for bytecode. When a JIT
compiler is part of JVM, selected portions of bytecode are compiled into executable code in
real time.
 Java features : i) Simple ii) Secure iii) Portable iv) Object-oriented v) Robust
vi) Multithreaded vii) Architectura-neutral viii) Interpreted
ix) High performance x) Distributed xi) Dynamic
 Object Oriented Programming organizes a program around its data (i.e., objects). An object
oriented program can be characterized as data controlling access to code.
 Object - An object is a real world entity or real world thing, which one knows how to use it
but unaware of its internal structure or the way it works.
 Abstraction – An abstraction is a lack of knowledge. An Object Oriented Programming
language advises the programmer to maintain an abstraction between user & application to
keep away the complexity of an application from user. A powerful way to manage the
abstraction is through the use of hierarchical classifications.
 Three Object Oriented Programming Principles
 Encapsulation – Encapsulation is the mechanism that binds together code and data it
manipulates, and keeps both safe from outside interference and misuse. In java, the basis
of encapsulation is the class. Each method or variable in a class may be marked private
or public. The public interface of a class represents everything that external users of the
class need to know, or may know. The private methods and data can only be accessed
by code that is a member of a class.
 Interference – Interference is the process by which object acquires the properties of
another object. A new subclass inherits all of the attributes of all of its ancestors.
 Polymorphism – Polymorphism is a feature that allows one interface to be used for a
general class of actions. The concept of polymorphism is often expressed by the phrase
“one interface, multiple methods.”
 As compared to VB, which is object based language, Java is class based language with more
than 15 thousand classes & above 2 lack methods.
 Java uses both compiler & interpreter to execute a program.
 In case of a program, containing multiple classes, one can assign any name to a program,
but can execute the program using the class name which has ‘main’ method. A ‘.class’ file is
created for each class in a program.
 Java doesn’t recognize ‘boolean’ data type as ‘int’. It stores ‘true’ & ‘false’ literals.
 A first simple program : Displaying messages on console.
1. class Example {
2. public static void main(String args[]) {
3. System.out.println(“Hello! Vishal Birnale”);
4. }
5. }
The 1 s t line uses the keyword class to declare that a new class is being defined.
The 2 nd line of code uses:
‘ public’ keyword – it is an access specifier, which allows the programmer to control the
visibility of class members. public member may be accessed by code outside the class in
which it is declared.
‘ static’ keyword – it allows main() to be called without having to instantiate particular
instance of the class.
‘ void’ keyword – it simply tells the compiler that the main() doesn’t return a value.
The 3 rd line contains System class, which is a predefined class that provides access to system,
and out is the output stream that is connected to the console.
println() displays the string which is passed to it.
 Block of code ({ . . . })
Coding is done in blocks. A variable declared in a block is local to that block & is
accessible anywhere within the same block; it can’t be accessed in any other block.
In case of nesting of blocks, the variable of outer block can be accessed in inner block but
not vice versa.