E-Content
Introduction to Java
Shivneet Tripathi
Department of Computer Application
UIET , CSJM University,
Kanpur
Introduction to
Java
JAVA was originated at Sun Microsystems Inc., in 1991.
It was conceived by James Gosling and Patrick Naughton.
Simple programming language to write, compile and debug
a program easily.
Helps to create modular programs and reusable code.
Powerful Features
• Java has the following features
– Object Oriented
– Simplicity
– Robust
– Platform Independent (Portable)
– Security
– Distributed Applications
– Multithreading
Object Oriented Programming
• Abstraction
– Denotes the extraction of essential characteristics of an
object that distinguish from all other kinds of objects.
• Encapsulation
– Hiding the implementation details of a class.
– Forces the user to use an interface to access the data.
• Inheritance
– Process by which one class acquires the properties and
functionalities of the other.
• Polymorphism
– Means the ability of methods to exist in several different
Java is a Simple Language
• Does not have complex features as other
programming languages like
– Operator overloading,
– Multiple inheritance through classes,
pointers and
– Explicit memory allocation.
Java is a Robust Language
• Two main hurdles which cause program failures i.e
memory management mistakes and mishandled
runtime errors can be overcome.
– Memory management mistakes can be overcome by
garbage collection. Garbage collection is automatic
de-allocation of objects which are no longer needed.
– Mishandled runtime errors are resolved by Exception
Handling procedures.
Java is Secure
• Provides a virtual firewall between the
application and the computer.
• Java codes are confined within Java
Runtime Environment (JRE).
Java is Distributed
• Java is designed for the distributed
environment of the internet.
• Objects on one JVM can execute
procedures on a remote JVM.
Multithreading
• Enables a program to perform several
tasks simultaneously.
Java is Platform Independent
• An application developed on Java can run in any
machine.
• When Java is compiled, it is not compiled into
platform specific machine or platform
independent byte code.
• The byte code is distributed over the web and
interpreted by Java Virtual Machine (JVM) on
whichever platform it is run.
Java Virtual Machine(JVM)
• JVM acts as an interpreter and converts byte codes to
machine codes.
• The source code of Java is stored with “.java” extension and
the compiler ie., javac converts .java code into byte code.
• Byte codes are Binary Language codes and will be in a file
with extension “.class”.
• The byte codes are interpreted by JVM and .class file that is
generated is the machine code of the processor.
• JVM is platform dependent.
• The JVM must be implemented on a particular platform
before compiled programs can run on that platform.
Setting the PATH
• The PATH variable is used to locate the
executable files by the operating system.
• Windows:
– Set PATH=%PATH%; %JAVA_HOME%\bin
• UNIX:
– Set PATH=$PATH: $JAVA_HOME/bin
Compilation and Execution of Java Program
• To create a java code an editor such as notepad,
text pad or an IDE like eclipse can be used.
• Sample Java Program:
public class WelcomeApp {
public static void main(String[] args){
System.out.println(“Welcome to Java”);
}//End of main
}//End of WelcomeApp Class
Output : Welcome to Java
• In the above program the class WelcomeApp has
public access and hence declared public.
• ‘class’ is the keyword used to create a class.
• For running stand alone programs ‘main’ method is
needed which has a signature similar to the one
defined in the above program.
• ‘Main’ method takes an array of strings as an
argument. The name of the array can be anything.
• To display the output, pass the string as an
argument to the method system.out.println
Compiling and Executing a Java Program
• Step1: Save the source file as WelcomeApp.java
• Step2: Open command prompt and navigate to the directory where
you have stored the file.
• Step 3: To compile, type javac WelcomeApp.java and press Enter.
• Step 4: On successful compilation, you will see the command prompt
and WelcomeApp.class file in the same folder where
WelcomeApp.java is stored. This is the byte code of the program.
• Step 5: To execute, type java WelcomeApp. Do not type the extension
while executing.
• Step 6: See the output Welcome to Java displayed on the console.
Common Programming Errors in Java
The following are the general programming errors and the solution for them while
running on windows machine.
‘javac’ is not recognized as an internal or external command, operable program or
batch file
This means that the operating system cannot find the compiler - javac. In order
to resolve this the PATH variable has to be set.
Exception in thread “main”java.lang.NoClassDefFoundError: WelcomeApp
This error means that java cannot find your compiled byte code,
WelcomeApp.class. If the class file is present in directory other than the directory
where java file is stored, then the CLASSPATH must be set pointing to the directory
that contain compiled class files.