0% found this document useful (0 votes)
16 views28 pages

UNIT 1 - Programming in Java

bsc introduction to java 3rd sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

UNIT 1 - Programming in Java

bsc introduction to java 3rd sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMMING IN JAVA

Course code : 24CSC2T431


Unit 1: Introduction to Java

History; Java and the Internet; Fundamentals of Object-Oriented Programming; Object-


Oriented Paradigm; Basic Concepts of Object-Oriented Programming; Benefits of
Object-Oriented Programming; Application of Object-Oriented Programming; Features
- platform-independent nature of Java; Comparison of Java with C and C++; Overview
of Java; simple Java program; Structure; Java Development Kit (JDK); Byte Code; Java
Virtual Machine
Introduction to Java

• Java is an object-oriented, class-based, concurrent, secured and general-purpose


computer-programming language. It is a widely used robust technology.

• 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.

• Platform: Any hardware or software environment in which a program runs, is known as a


platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

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.

First Release (1995)


•Java 1.0 was officially released in January 1996 by Sun Microsystems.
•Marketed with the slogan "Write Once, Run Anywhere" (WORA) due to its platform-independent nature.

Impact on the Industry


•Java became a cornerstone for web applications, enterprise software, and Android development.
•Widely used in server-side applications, big data tools (e.g., Hadoop), and mobile apps.
Key Milestones

• 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.

Why Java Endures?

Platform independence via Java Virtual Machine (JVM).


Robust, secure, and scalable, making it ideal for large-scale systems.
Large community support and extensive libraries/APIs.
Applications of Java
According to Sun, 3 billion devices run Java. There are many devices where Java is
currently used.
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as [Link], [Link], etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
Object-Oriented Programming
Object-Oriented Programming is a paradigm that provides many concepts, such
as inheritance, data binding, polymorphism, etc.
Object means a real-world entity such as a pen, chair, table, computer, watch,
etc. Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects. It simplifies software development and
maintenance by providing some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Object

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

• It is a mechanism in which one object acquires all the properties and


behaviors of a parent object. It is an important part of OOPS.
• The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes.

Why use inheritance in java?


• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability
Polymorphism

• Polymorphism in Java is a concept by which we can perform a single


action in different ways. If one task is performed in different ways, it is
known as polymorphism.
For example: to draw something, for example, shape, triangle, rectangle, etc.
• In Java, we use method overloading and method overriding to achieve
polymorphism.
• Another example can be to speak something; for example, a cat speaks
meow, dog barks woof, etc.
Abstraction

• Abstraction is a process of hiding the implementation details and showing only


functionality to the user.
• Another way, it shows only essential things to the user and hides the internal details, for
example, sending SMS where you type the text and send the message. You don't know the
internal processing about the message delivery.
• Abstraction lets you focus on what the object does instead of how it does it.
There are two ways to achieve abstraction in java:
• Abstract class
• Interface
Encapsulation

• Encapsulation in Java is a process of wrapping code and data together


into a single unit, for example, a capsule which is mixed of several
medicines.
• It provides you the control over the data. Suppose you want to set the
value of id which should be greater than 100 only, you can write the
logic inside the setter method. You can write the logic not to store the
negative numbers in the setter methods.
• It is a way to achieve data hiding in Java because other class will not
be able to access the data through the private data members.
Benefits of Object-Oriented Programming
Modularity
Code is organized into classes and objects, making it easier to manage and understand.
Example: A "Car" class separates car-related code from other parts of a program.

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.

The features of Java are also known as java buzzwords.

• 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.

Automatic via Garbage Collector; no manual memory


Memory Management Manual (malloc/free); prone to memory leaks.
management.

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.

Robust try-catch with built-in and user-defined


Exception Handling No built-in exception handling; uses error codes.
exceptions.

Extensive libraries (e.g., [Link], [Link]) for diverse


Standard Library Basic standard library (e.g., stdio.h, stdlib.h).
tasks.

Web apps, Android apps, enterprise systems (e.g.,


Use Case Example System programming (e.g., OS kernels).
banking software).
Java Development Kit (JDK)
• What is JDK?
A software package for developing, compiling, and running Java programs.
Example: Includes tools like javac (compiler) and java (runtime).
• Components of JDK
Contains Java Compiler (javac), Java Runtime Environment (JRE), and
tools (e.g., javadoc, jar).
Example: Use javac [Link] to compile a Java program.

• 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)

• Java Run-time Environment (JRE) is the part


of the Java Development Kit (JDK).
• It is a freely available software distribution
which has Java Class Library, specific tools, and
a stand-alone JVM.
• It is the most common environment available
on devices to run java programs.
• The source Java code gets compiled and
converted to Java bytecode.
• The JRE loads classes, verify access to
memory, and retrieves the system resources. JRE
acts as a layer on the top of the operating system.
Java Virtual Machine (JVM)
• What is JVM?
A virtual machine that executes Java Byte Code; part of the JRE.
Example: Runs [Link] using the java command.
• Platform Independence
Acts as an interpreter, making Java programs run on any device with
a JVM.
Example: A Java app written on Windows runs unchanged on Linux.

• 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).

You might also like