Java Basics
Introduction
JAVA is a pure object oriented programming language developed by
Microsystems of USA 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.
Features of Java
Java has the following features
Compiled and Interpreted
Object Oriented
Familiar, Simple and Small
Robust and Secure
Platform Independence (Portable)
Distributed Applications
Multithreading
High Performance
Compiled and Interpreted
Java is a two stage system.
First, Java compiler translates source code into the byte code, Byte
code are not the machine instructions.
In Second stage, Java interpreter converts byte code into machine
code.
Java is Object Oriented
Object oriented programming is a method in which
programs are organized as collection of objects,
each of which represents an instance of a class
4 main concepts of OOP are
Abstraction
Encapsulation
Inheritance
Polymorphism
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
forms
Simple, Small and Familiar
Java is a small and simple language. Does not have complex features
as other programming languages like
Operator overloading,
Multiple inheritance through classes, pointers and
Explicit memory allocation.
Robust and Secure
Java is a robust language. It has strict compile time and
run time checking for data types. 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 & Multithreading
Java is designed as a distributed language for creating applications on
network.
Objects on one JVM can execute procedures on a remote JVM.
Multithreading means handling several tasks simultaneously.
Platform Independent and Portable
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.
Java Virtual Machine(JVM)
Java Environment
Java environment includes a large number of development tools and
hundreds of classes and methods.
Development tools are part of JDK(Java Development Kit).
Classes and Methods are part of Java Standard Library(JSL), also
known as Application Programming Interface(API).
Java Development Kit
JDK Exercise
Application Program Interface
Installing and Using Java
First install Java 2 JDK version 11
This can be downloaded from the official website of Java
Setting the Environmental Variables
• Environmental variables describe the operating environment of the
process.
• Some common environmental variables describe the home directory and
command search path.
• How to set environmental variables?
Setting JAVA_HOME helps to derive all other environmental variables
used in JVM.
The following commands can be typed in the command prompt,
Windows:
JAVA_HOME=C:\Java installation directory
UNIX:
JAVA_HOME=/var/usr/java
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
Explanation of program
• 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.
Compiling and Executing a Java
Program
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.
More on Basics of Java
More on Basics of Java