Java Tutorial
Java Tutorial
Programming
What is Java
• Java is a high-level, object-oriented programming language developed by Sun Microsystems
• It is designed to be platform-independent, meaning that Java programs can run on any device or operating system that has
a Java Virtual Machine (JVM) installed.
• Java is known for its "write once, run anywhere" (WORA) principle. This makes Java a popular choice for developing cross-
platform applications, such as
• Enterprise software development: Java is widely used for building large-scale, mission-critical applications in
industries like finance, healthcare, and telecommunications.
• Web development: Java-based frameworks like Spring and Hibernate are popular choices for building web
applications and RESTful APIs.
• Mobile development: Java is used for developing Android applications, leveraging the Android SDK and tools like
Android Studio.
• Big data and cloud computing: Java is used in platforms and frameworks like Apache Hadoop and Apache Spark for
processing and analyzing large datasets.
Java History
1. Origins at Sun Microsystems (1991-1995):
• Java was conceived by James Gosling, Mike Sheridan, and Patrick Naughton at Sun Microsystems in the early 1990s.
The project, initially named "Oak," aimed to develop software for consumer electronics like set-top boxes.
• In 1995, the team released Java 1.0, which included a compiler, interpreter, and runtime environment. This marked the
official launch of Java as a programming language.
2. "Write Once, Run Anywhere" (1995-2000):
• Java's key innovation was its platform independence, encapsulated in the slogan "write once, run anywhere." This was
made possible by compiling Java source code into bytecode, which could then be executed on any device with a Java
Virtual Machine (JVM).
• The introduction of applets, small Java programs embedded within webpages, helped popularize Java as a language for
web development.
3. Acquisition by Sun Microsystems and Growth (2000s):
• In 1996, Sun Microsystems officially changed the name from Oak to Java due to trademark issues.
• Sun Microsystems continued to develop and promote Java throughout the late 1990s and early 2000s. The language
gained popularity in enterprise software development, particularly for building large-scale, mission-critical applications.
Java History
4. Open Sourcing and Community Involvement (2006-2010):
• In 2006, Sun Microsystems released Java under the GNU General Public License (GPL) as part of the OpenJDK (Java
Development Kit) initiative. This move aimed to increase community involvement and foster innovation around the Java
platform.
• The Java Community Process (JCP), established in 1998, allowed developers and organizations to contribute to the
evolution of the Java language and platform through a transparent and collaborative process.
5. Acquisition by Oracle Corporation (2010-present):
• In 2010, Oracle Corporation acquired Sun Microsystems, including the rights to Java. Oracle continued to steward the
development of Java, releasing new versions and updates to the language and platform.
• Java has remained one of the most popular programming languages worldwide, used in a wide range of applications and
industries, including web development, mobile apps, enterprise software, big data, and cloud computing.
6. Recent Developments:
• Recent versions of Java have introduced new features and enhancements to improve developer productivity, performance,
and security. Notable releases include Java SE 8 (2014), which introduced lambda expressions and the Stream API, and Java
SE 11 (2018), a long-term support (LTS) release with significant improvements and modernizations.
Why Java
1. Simplicity: Java was designed to be programmer-friendly, with syntax that is easy to understand and learn. Its C-like syntax
and clear, concise code structure make it accessible and simple.
2. Platform Independence: One of the key features of Java is its "write once, run anywhere" capability. Java programs can run
on any device or operating system that has a Java Virtual Machine (JVM) installed, making it highly portable and versatile.
3. Object-Oriented Programming (OOP): Java is an object-oriented programming language, which is a fundamental
programming paradigm used in many modern software development practices. Learning Java introduces beginners to
important OOP concepts like classes, objects, inheritance, polymorphism, and encapsulation.
4. Versatility and Industry Demand: Java is used in a wide range of applications and industries, from web development to
mobile apps, enterprise software, big data, and cloud computing. Learning Java opens opportunities for careers in various
sectors, as it is widely used and in demand in the job market.
5. Robustness and Security: Java's strict compile-time checking, runtime verification, and built-in security features make it a
robust and secure programming language. Beginners can learn best practices for writing reliable and secure code, which is
essential in professional software development.
6. Scalability: Java is well-suited for building scalable and high-performance applications, making it a preferred choice for
large-scale projects and enterprise-level software.
Why Java
7. Multithreaded : Multithreading allows a Java program to execute multiple threads simultaneously. A thread is a
lightweight process that represents an independent flow of execution within a program.
8. Dynamic : Enable Java programs to perform tasks such as reflection, runtime polymorphism, dynamic class loading, code
generation, and dynamic binding, providing greater expressiveness and power to developers.
9. Strongly typed : Java is a statically typed language, which means that variable types must be explicitly declared.
10. Automatic memory management: Java includes a garbage collector that automatically manages memory allocation and
deallocation.
11. Rich standard library : Java comes with a comprehensive standard library that provides support for various tasks such as
networking, I/O, database access, and more.
JVM
The Java Virtual Machine (JVM) is a crucial component of the Java Runtime Environment (JRE) and Java Development Kit
(JDK). It plays a fundamental role in the execution of Java programs. It abstracts the underlying hardware and operating
system, allowing Java developers to write code that can run on diverse platforms with ease.
1. Runtime Environment : The JVM provides a runtime environment for Java applications, including support for
multithreading, exception handling, and other runtime features defined by the Java language specification.
2. Platform Independence : The JVM interprets the bytecode generated by Java compiler into machine’s native code and
executes it. JVM is platform dependent.
3. Class Loading and Linking : The JVM’s class loader is responsible for loading all needed classes and performs necessary
linking tasks such as verification, preparation, and resolution before executing the bytecode.
4. Security: The JVM’s bytecode verifier, which ensures that Java bytecode adheres to certain rules and does not violate the
security constraints of the JVM. This helps prevent malicious code from being executed on the system.
5. Memory Management : The JVM manages memory allocation and garbage collection. It automatically allocates memory
for objects created by the Java program and deallocates memory for objects that are no longer in use, thereby preventing
memory leaks and optimizing memory usage.
Type Conversion and Casting
Variables
• Variables are basic unit of storage in a Java program. They are containers to hold and manipulate data.
• Variables have a data type that specifies the type of data they can hold & a scope that defines their validity and lifetime.
• Data types can be primitive data types (ex: int, float, double, boolean etc) or reference data(ex: objects, array, etc)
• The scope refers to the region of code where the variable is accessible. Local scope (accessible within a method/block),
class scope (accessible within a class but outside any method), or instance scope (accessible within a class).
• Variables are allocated memory based on their data type and scope. Local variables are stored on the stack, while
instance variables are stored in the heap memory along with the object they belong to.
JVM
• Data Types define the kind of data stored and specify the operations that can be performed.
• Java is a strongly typed language which is in fact the reason for its safety and robustness.
• Every variable, expression, parameters of methods should have type defined. They are verified for type compatibility.
OOPS concepts
• Object-Oriented Programming is a programming paradigm based on the concept of objects which contain fields &
methods.
• Helps in managing and organizing complex systems efficiently through modularity, reusability, maintainability, and
scalability
1. Encapsulation: This refers to the bundling of data and methods that manipulate the data into a single unit called a
class. The data is not accessible to the outside world and can be accessed only through functions. Encapsulation
helps in hiding the internal state of an object from the outside world by only exposing the necessary functionalities.
2. Inheritance: This is the process by which one object acquires the properties of another object. This allows a class
(subclass or derived class) to inherit properties and behavior from another class (superclass or base class). This
promotes code reusability and allows for the creation of a hierarchical classification of classes.
3. Polymorphism: The ability of different objects to respond to the same message or method invocation in different
ways. It allows objects of different types to be treated as objects of a common superclass.
4. Abstraction: Hiding the complex implementation details and only showing the necessary features of an object. It
allows programmers to focus on what an object does rather than how it does it.
Java Coding Standards
• Java coding standards are a set of guidelines and conventions that developers follow when writing code.
• These standards help ensure consistency, readability, maintainability, and collaboration within a codebase.
1. Naming Conventions
2. Indentation and Formatting
3. Comments
4. Exception Handling
5. Limited use of globals
6. Code Reusability
7. Version Control
8. Length of functions should not be very large
Packages
• A package is a way to organize related classes and interfaces into a group.
• Import statements are used to use classes from other packages.
1. Avoid naming conflicts by providing a unique namespace for the classes they contain.
2. Organize classes and interfaces into a hierarchical structure
3. Play a role in access control.
Keywords
• Keywords are reserved and cannot be used as identifiers (e.g., variable names, class names) in Java programs.
Keywords
The public access specifier allows unrestricted access to the class, method, variable, or constructor. This means that it can be accessed from any other class or package.
Access Specifiers
• Keywords that define the accessibility/visibility of classes, variables, methods & constructors from other parts of the code.
• These standards help ensure consistency, readability, maintainability, and collaboration within a codebase.
1. public : unrestricted access can be accessed from any other class or package.
2. protected : accessible within the same package and subclasses (even if they are in different packages).
3. private : restricts access to only within the same class. It is the most restrictive access level.
4. default (no specifier) : accessible only within the same package.
The public access specifier allows unrestricted access to the class, method, variable, or constructor. This means that it can be accessed from any other class or package.
Modifier - Static
• Used to create class level variables, methods & blocks
• Only one copy of the static class member is created, and it can be accessed directly by the class name
1. Static Variables : initialized only once, at the start of the execution, and their values are shared among all objects.
2. Static methods : They can only access static variables and other static methods.
main() is static because it must be accessible for an application to run before any instantiation takes place.
The public access specifier allows unrestricted access to the class, method, variable, or constructor. This means that it can be accessed from any other class or package.
Modifier - Static
3. Static Nested class :
• Defined within another class and used for grouping related classes and improving code organization.
• They can access static members of the outer class directly and be instantiated without an instance of the outer class.
4. Static Block :
• Defined within curly braces after static keyword and used for static initialization of variables or performing one-time operations.
• Executed when the class is loaded into memory, even before the creation of any object of that class.
• Static blocks cannot access non-static members of the class directly.
Scanner class
• The Scanner class is used to read input from various sources like the keyboard, files, or strings.
• provides methods like next(), nextInt(), nextLine(), etc to parse primitive types and strings
• May not be the most efficient choice for high-performance input processing. BufferedReader may be more suitable.