UNIT 1 - Programming in Java
UNIT 1 - Programming in Java
• Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak.
class Simple{
public static void main(String args[])
{
[Link]("Hello Java");
}
}
History of Java
Origin (1991)
•Java was developed by James Gosling, Mike Sheridan, and Patrick Naughton at Sun Microsystems.
•Initially called "Oak" (inspired by a tree outside Gosling's office), later renamed to Java in 1995.\
Purpose
•Designed for consumer electronic devices (e.g., set-top boxes, interactive TVs).
•Evolved into a general-purpose programming language due to its versatility.
• 1997: Java gained popularity with the rise of the internet and applets.
• 1998: Java 2 (J2SE) introduced significant improvements, including Swing for GUI
development.
• 2004: Java 5 introduced generics, enums, and enhanced for loops.
• 2014: Java 8 brought functional programming features like lambda expressions.
• 2020 onwards: Oracle (acquired Sun in 2010) releases new versions every six months.
An object in Java is the physical as well as a logical entity, whereas, a class in Java is
a logical entity only.
An object has three characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of
an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented
via a unique ID.
Class
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which you can create an individual
object. Class doesn't consume any space.
Inheritance
Reusability
Classes can be reused across programs through inheritance and polymorphism.
Example: A "Vehicle" class can be reused for "Car" and "Bike" classes.
Scalability
OOP supports large projects by breaking them into smaller, manageable components.
Example: A banking system with separate classes for "Account," "Customer," and "Transaction.“
Maintainability
Changes in one class don’t affect others, making debugging and updates easier.
Example: Update the "display" method in a "Student" class without changing the entire program.
Flexibility
Encapsulation hides data, and polymorphism allows multiple forms of methods.
Example: A "Shape" class with a "draw" method that works differently for circles and rectangles.
Type of Applications
There are mainly 4 types of applications that can be created using Java programming:
1. Standalone Application
• Standalone applications are also known as desktop applications or window-based applications. These are traditional
software that we need to install on every machine. Examples of standalone application are Media player, antivirus,
etc. AWT and Swing are used in Java for creating standalone applications.
2. Web Application
• An application that runs on the server side and creates a dynamic page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web applications in Java.
3. Enterprise Application
• An application that is distributed in nature, such as banking applications, etc. is called enterprise application. It has
advantages of the high-level security, load balancing, and clustering. In Java, EJB is used for creating enterprise
applications.
4. Mobile Application
• An application which is created for mobile devices is called a mobile application. Currently, Android and Java ME are
used for creating mobile applications.
Features of java
• The primary objective of Java was to make it portable, simple and secure programming language. Apart from this,
there are also some excellent features which play an important role in the popularity of this language.
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
Features of java
1. Simple
• Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun,
Java
language is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator
overloading, etc.
• There is no need to remove unreferenced objects because there is an Automatic Garbage Collection
in
Java.
2. Object-oriented
• Java is an object-oriented programming language. Everything in Java is an object. Object-oriented
means we organize our software as a combination of different types of objects that incorporates both
data and behavior.
Features of java
3. Platform Independent
• Java is platform independent because it is different from other languages like C, C++, etc. which are
compiled into platform specific machines while Java is a write once, run anywhere language. A
platform is the hardware or
software environment in which a program runs.
• Java code can be run on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS,
etc. Java code is compiled by the compiler and converted into bytecode.
This bytecode is a platform-independent code because it can be run on multiple platforms, i.e., Write
Once and Run Anywhere(WORA).
Features of java
4. Secured
Java is best known for its security. With Java, we can develop virus-free systems.
Java is secured because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Classloader: Classloader in Java is a part of the Java Runtime Environment(JRE) which
is used to load Java classes into the Java Virtual Machine dynamically. It adds security by
separating the package for the classes of the local file system from those that are imported
from network sources.
• Bytecode Verifier: It checks the code fragments for illegal code that can violate access
right to objects.
• Security Manager: It determines what resources a class can access such as reading and
writing to the local disk.
Features of java
5. Robust
It simply means strong. Java is robust because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• There is automatic garbage collection in java which runs on the Java Virtual Machine to get rid
of objects which are not being used by a Java application anymore.
• There are exception handling and the type checking mechanism in Java. All these points make
Java robust.
6. Interpreted
• Java is a platform-independent programming language. It means that we can run Java on the
platforms that have a Java interpreter.
• It is the reason that makes the Java platform-independent.
• The Java interpreter converts the Java bytecode (.class file) into the code understand by the
operating system.
Features of java
7. Architecture Neutral
• Java is architecture neutral because there are no implementation dependent features, for example, the size of
primitive types is fixed.
• In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for
64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
8. Portable
• Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any
implementation.
9. High-performance
• Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native
code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why
it is slower than compiled languages, e.g., C, C++, etc.
Features of java
10. Distributed
• Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB
are used for creating distributed applications. This feature of Java makes us able to access files by
calling the methods from any machine on the internet.
11. Multi-threaded
• A thread is like a separate program, executing concurrently. We can write Java programs that deal
with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it
doesn't occupy memory for each thread. It shares a common memory area. Threads are important for
multi-media, Web applications, etc.
12. Dynamic
• Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on
demand. It also supports functions from its native languages, i.e., C and C++. Java supports dynamic
compilation and automatic memory management (garbage collection).
Comparison of Java with C and C++
Feature C Java
Procedural programming; focuses on functions and
Programming Paradigm Fully object-oriented; everything is within classes.
procedures.
Platform-dependent; code must be recompiled for Platform-independent; runs on JVM with "write once, run
Platform Dependency
each platform. anywhere."
Pointers Supports pointers; direct memory access possible. No pointers; uses references for safer memory access.
Inheritance No inheritance; uses structs for data grouping. Supports single inheritance; multiple via interfaces.
• Installation
Must be installed to write and run Java code; available from Oracle or
OpenJDK.
Example: Set PATH to access JDK tools from the command line.
• Role in Development
Provides libraries (e.g., [Link], [Link]) and tools for debugging and
packaging.
Example: Create executable JAR files for applications.
Java Runtime Environment (JRE)
• Key Functions
Loads Byte Code, verifies it, and executes it; manages memory via
Garbage Collector.
Example: Automatically frees unused objects to prevent memory
leaks.
• Components
Includes Class Loader, Byte Code Verifier, and Execution Engine.
Example: Class Loader loads .class files into memory for execution.
Byte Code
• Definition
Intermediate code generated by the Java compiler (javac) from source code.
Example: [Link] compiles to [Link].
• Platform Independence
Byte Code is platform-neutral; runs on any system with a JVM.
Example: Same .class file works on Windows, Linux, or macOS.
• Structure
Consists of instructions for the JVM; not human-readable like source code.
Example: Byte Code includes opcodes like invokevirtual for method calls.
• Security
Verified by JVM before execution to ensure safety and prevent errors.
Example: Prevents invalid memory access, unlike C/C++.
Anatomy of a class
• When the JVM starts running, it looks for the class you give it at the
command line. Then it starts looking for a specially-written method that
looks exactly like:
public static void main (String[] args) {
// your code goes here
}
• Next, the JVM runs everything between the curly braces { } of your main
method. Every Java application has to have at least one class, and at least
one main method (not one main per class; just one main per application).