Welcome To Java: Java Is A Write Once, Run Anywhere Language
Welcome To Java: Java Is A Write Once, Run Anywhere Language
H-TECH IT SOLUTION 1
AGENDA
Introduction to Java programming
Basics OOPs Concept
Control Statement
Java Object Classes
Java Array
Wrapper classes
Packages
Multithreading
Exception Handling
H-TECH IT SOLUTION 2
INTRODUTION TO JAVA PROGRAMMING
WHAT IS JAVA
VERSIONS OF JAVA
FEATUERS OF JAVA
C++ VS JAVA
JVM ,JRE,JDK
HELLO JAVA PROGRAM
H-TECH IT SOLUTION 3
HISTORY OF JAVA
• 1) Originally designed for small, embedded systems in electronic appliances like set-top boxes.
• 2) Firstly, it was called "Greentalk" by James Gosling, and file extension was .gt.
• 3) After that, it was called Oak and was developed as a part of the Green project.
Why Java named "Oak"?
• Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like U.S.A., France, Germany, Romania,
etc.
• 4) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
• 5)Java is an island of Indonesia where first coffee was produced (called java coffee).
• 6) Notice that Java is just a name, not an acronym.
• 7) Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in
1995.
• 8) In 1995, Time magazine called Java one of the Ten Best Products of 1995.
• 9) JDK 1.0 released in(January 23, 1996).
H-TECH IT SOLUTION 4
VERSIONS OF JAVA
• Many java versions have been released till now. The
current stable release of Java is Java SE 10.
JDK Alpha and Beta (1995) J2SE 5.0 (30th Sep 2004)
JDK 1.0 (23rd Jan 1996) Java SE 6 (11th Dec 2006)
JDK 1.1 (19th Feb 1997)
Java SE 7 (28th July 2011)
J2SE 1.2 (8th Dec 1998)
Java SE 8 (18th March 2014)
J2SE 1.3 (8th May 2000)
Java SE 9 (21st Sep 2017):
J2SE 1.4 (6th Feb 2002)
Java SE 10 (20th March 2018):Released to remove primitive data
types
Upcoming Java Version
H-TECH IT SOLUTION 6
APPLICATIONS
• According to Sun, 3 billion devices run Java. There are many devices where Java is currently
used. Some of them are as follows:
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as irctc.co.in, javatpoint.com, etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
H-TECH IT SOLUTION 7
FEATUERS OF JAVA
H-TECH IT SOLUTION 8
C++ VS JAVA
H-TECH IT SOLUTION 9
H-TECH IT SOLUTION 10
HOW TO RUN JAVA PROGRAM
• Let's create the hello java program:
• class Simple{
• public static void main(String args[])
• {
• System.out.println("Hello Java");
• }
• }
• Save this file as Simple.java
• To compile:javac Simple.java To execute: java Simple Output:Hello Java
• Compilation Flow: When we compile Java program using javac tool, java compiler converts the source
code into byte code.
H-TECH IT SOLUTION 11
INTERNAL DETAILS OF JAVA PROGRAM
At compile time, java file is compiled by Java Compiler (It does
not interact with OS) and converts the java code into byte code.
H-TECH IT SOLUTION 12
WORKING OF JVM,JRE,JDK
JVM:
JVM (Java Virtual Machine) is an abstract machine.
It is called a virtual machine because it doesn't physically exist.
It is a specification that provides a runtime environment in which Java byte
code can be executed.
It can also run those programs which are written in other languages and
compiled to Java byte code.
JVMs are available for many hardware and software platforms.
H-TECH IT SOLUTION 13
JRE
JRE is an acronym for Java Runtime Environment.
It is also written as Java RTE. The Java Runtime Environment is a set of software tools which are
used for developing Java applications.
It is used to provide the runtime environment.
It is the implementation of JVM.
It physically exists.
It contains a set of libraries + other files that
JVM uses at runtime.
H-TECH IT SOLUTION 14
JDK
• JDK is an acronym for Java Development Kit.
• The Java Development Kit (JDK) is a software development environment which is used to
develop Java applications . It physically exists. It contains JRE + development tools.
JDK is an implementation of :
Standard Edition Java Platform
Enterprise Edition Java Platform
Micro Edition Java Platform
H-TECH IT SOLUTION 15
BASICS OF JAVA
VARIABLES
• LOCAL VARIABLE
• INSTATNCE VARIABLE
• STATIC VARIABLES
DATA TYPES
UNICODE SYSTEM
OPERATORS
• 1)Unary Operator,2)Arithmetic Operator,3)Shift Operator,4)Relational Operator,5)Bitwise Operator,6)Logical Operator,7)Ternary Operator and
• 8)Assignment Operator.
KEYWORDS
16
CONTROL STATEMENTS
JAVA IF-ELSE
JAVA SWITCH
JAVA FOR LOOP
JAVA WHILE LOOP
JAVA DO WHILE LOOP
FOR EACH LOOP
JAVA BREAK
JAVA CONTINUE
JAVA COMENTS
17
FOR EACH LOOP
• In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList). It is also known as the
enhanced for loop.
• for-each Loop Sytnax
• The syntax of the Java for-each loop is:
• for(dataType item : array) { ... }Here,
• array - an array or a collection
• item - each item of array/collection is assigned to this variable
• dataType - the data type of the array/collection
18
EXAMPLE
• // print array elements
• class Main {
• public static void main(String[] args) {
• // create an array
• int[] numbers = {3, 9, 5, -5};
• // for each loop
• for (int number: numbers) {
• System.out.println(number);
• }}}
• Output -> 3 9 5 -5
19
Example 2: Sum of Array Elements
• // Calculate the sum of all elements of an array
• class Main {
• public static void main(String[] args) {
• // an array of numbers
• int[] numbers = {3, 4, 5, -5, 0, 12};
• int sum = 0;
• // iterating through each element of the array
• for (int number: numbers) {
• sum += number; }
• System.out.println("Sum = " + sum); } }
• Output: Sum = 19
20
EXAMPLE 2
• class Main {
• public static void main(String[] args) {
• char[] vowels = {'a', 'e', 'i', 'o', 'u'};
• // iterating through an array using the for-each loop
• for (char item: vowels) {
• System.out.println(item);
• } }}
21
H-TECH IT SOLUTION 22
OBECTS AND CLASSES
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and
behavior.
The object is an instance of a class.
H-TECH IT SOLUTION 23
H-TECH IT SOLUTION 24
CLASS IN
What is a class in Java:
JAVA
A class is a group of objects which have common properties.
It is a template or blueprint from which objects are created. It is a
logical entity. It can't be physical.
H-TECH IT SOLUTION 26
CONSTRUCTOR IN JAVA
In Java, a constructor is a block of codes similar to the method.
It is called when an instance of the object is created, and memory is allocated for the object.
It is a special type of method which is used to initialize the object.
When is a constructor called
Every time an object is created using new() keyword, at least one constructor is called. It
calls a default constructor.
It is called constructor because it constructs the values at the time of object creation.
It is not necessary to write a constructor for a class. It is because java compiler creates a
default constructor if your class doesn't have any.
H-TECH IT SOLUTION 27
RULES FOR CREATING CONSTRUCTOR
There are two rules defined for the constructor.
Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized
H-TECH IT SOLUTION 28
H-TECH IT SOLUTION 29
DEFAULT CONSTRUCTOR
• A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
• class_name(){}
H-TECH IT SOLUTION 30
PARAMETERIZED CONSTRUCTOR
• A constructor which has a specific number of parameters is called a Parameterized constructor.
Why use the parameterized constructor?
• The parameterized constructor is used to provide different values to the distinct objects.
However, you can provide the same values also.
H-TECH IT SOLUTION 31
EXAMPLES OF CONSTRUCTOR
H-TECH IT SOLUTION 32
H-TECH IT SOLUTION 33
JAVA STATIC KEYWORD
The static keyword in Java is used for memory management
mainly.
We can apply java static keyword with variables, methods, blocks
and nested class. The static keyword belongs to the class than an
instance of the class.
H-TECH IT SOLUTION 34
EXAMPLE OF STATIC KEYWORD
35
Example of
• class Student{ static variable
• int rollno; //instance variable
• String name;
• static String college ="ITS";//static variable
• Student(int r, String n) //constructor {
• rollno = r; name = n;
• }//method to display the values
• void display ()
{System.out.println(rollno+" "+name+" "+college);}
• } public class TestStaticVariable1
//Test class to show the values of objects
36
JAVA STATIC METHOD
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
The static method can not use non static data member or call non-static method directly.
• class A
• { int a=40; //non static
• public static void main(String args[])
• { System.out.println (a);
• } S }
37
• Q) Why is the Java main method static?
• Ans) It is because the object is not required to call a static method. If it were a non-static method,
JVM creates an object first then call main() method that will lead the problem of extra memory
allocation.
• Q) Java static block
• Is used to initialize the static data member.
• It is executed before the main method at the time of classloading.
38
THIS KEYWORD
There can INkeyword.
be a lot of usage of java this JAVA In java, this is a reference variable that
refers to the current object.
39
40
THIS: TO INVOKE CURRENT CLASS METHOD
41
EXAMPLES OF THIS KEYWORD
H-TECH IT SOLUTION 42
INHERITANCE IN JAVA
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors
of a parent object. It is an important part of OOPs (Object Oriented programming system).
Why we use inheritance in java:
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
The syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• { //methods and fields }
• The extends keyword indicates that you are making a new class that derives from an existing
class. The meaning of "extends" is to increase the functionality.
H-TECH IT SOLUTION 43
EXAMPLE OF INHERITANCE
H-TECH IT SOLUTION 44
TYPES OF INHERITANCE IN JAVA
There can be three types of inheritance in java: Single, Multilevel and Hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.
H-TECH IT SOLUTION 45
EXAMPLES
H-TECH IT SOLUTION 46
Multiple Inheritance is not Supported in Java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B
classes. If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if
you inherit 2 classes.
So whether you have same method or different, there will be compile time error.
H-TECH IT SOLUTION 47
• class A{
• void msg(){System.out.println("Hello");}
• }
• class B{
• void msg(){System.out.println("Welcome");}
• }
• class C extends A,B{//suppose if it were
• public static void main(String args[]){
• C obj=new C();
• obj.msg();//Now which msg() method would be invoked?
• }
• }
H-TECH IT SOLUTION 48
AGGREGATION IN JAVA
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A
relationship.
Consider a situation, Employee object contains many informations such as id, name, emailId etc. It
contains one more object named address, which contains its own informations such as city, state,
country, zipcode etc. as given below.
• class Employee
• {
• int id;
• String name;
• Address address;//Address is a class
• }
H-TECH IT SOLUTION 49
WHEN USE AGGREGATION?
Code reuse is also best achieved by aggregation when there is no is-a relationship.
Inheritance should be used only if the relationship is-a is maintained throughout the
lifetime of the objects involved; otherwise, aggregation is the best choice.
H-TECH IT SOLUTION 50
POLYMORPHISM IN JAVA
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java:
1. Compile-time polymorphism
2. Runtime polymorphism.
• We can perform polymorphism in java by method overloading and method overriding.
H-TECH IT SOLUTION 51
RUNTIME POLYMORPHISM
Runtime polymorphism or Dynamic INin JAVA
Method Dispatch is a process which a call to an
overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass.
The determination of the method to be called is based on the object being referred to by the
reference variable.
Let's first understand the upcasting before Runtime Polymorphism.
Upcasting:
• If the reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:
H-TECH IT SOLUTION 52
EXAMPLES
• class A{}
• class B extends A{}
• A a=new B();//upcasting
• For upcasting, we can use the reference variable of class type or an interface type.
For Example:
• interface I{}
• class A{}
• class B extends A implements I{}
H-TECH IT SOLUTION 53
METHOD OVERLOADING IN JAVA
• If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.
• If we have to perform only one operation, having same name of the methods increases the
readability of the program.
• Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you as well as other programmers to understand the
behavior of the method because its name differs.
H-TECH IT SOLUTION 54
ADVANTAGE OF METHOD OVERLOADING
Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
In java, Method Overloading is not possible by changing the return type of the
method only.
H-TECH IT SOLUTION 55
EXAMPLES
H-TECH IT SOLUTION 56
METHOD OVERRIDING IN JAVA
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding:
Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.
Method overriding is used for runtime polymorphism
H-TECH IT SOLUTION 57
H-TECH IT SOLUTION 58
EXAMPLES
H-TECH IT SOLUTION 59
SUPER KEYWORD IN JAVA
The super keyword in Java is a reference variable which is used to refer immediate parent class
object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which
is referred by super reference variable.
H-TECH IT SOLUTION 60
H-TECH IT SOLUTION 61
FINAL KEYWORD IN JAVA
The final keyword in java is used to restrict the user. The java final keyword can be used in many
context. Final can be:
o variable
o method
o class
The final keyword can be applied with the variables, a final variable that have no value it is called
blank final variable or uninitialized final variable.
It can be initialized in the constructor only. The blank final variable can be static also which will
be initialized in the static block only. Let's first learn the basics of final keyword.
H-TECH IT SOLUTION 62
FINAL VRIABLE,METHOD,CLASS
If you make any Variable as final, you cannot change the value of final variable(It will be constant).
Final variable once assigned a value can never be changed.
If you make any Method as final, you cannot override it.
H-TECH IT SOLUTION 63
EXAMPLES
H-TECH IT SOLUTION 64
POLYMORPHISM IN JAVA
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java:
1. Compile-time polymorphism
2. Runtime polymorphism.
• We can perform polymorphism in java by method overloading and method overriding.
H-TECH IT SOLUTION 65
RUNTIME POLYMORPHISM
Runtime polymorphism or Dynamic INin JAVA
Method Dispatch is a process which a call to an
overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass.
The determination of the method to be called is based on the object being referred to by the
reference variable.
Let's first understand the upcasting before Runtime Polymorphism.
Upcasting:
• If the reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:
H-TECH IT SOLUTION 66
EXAMPLES
• class A{}
• class B extends A{}
• A a=new B();//upcasting
• For upcasting, we can use the reference variable of class type or an interface type.
For Example:
• interface I{}
• class A{}
• class B extends A implements I{}
H-TECH IT SOLUTION 67
STATIC BINDING AND DYNAMIC BINDING
Connecting a method call to the method body is known as binding.
There are two types of binding:
• Static Binding (also known as Early Binding).
• Dynamic Binding (also known as Late Binding).
H-TECH IT SOLUTION 68
H-TECH IT SOLUTION 69
STATIC BINDING
• When type of the object is determined at compiled time(by the compiler), it is known as static binding.
• If there is any private, final or static method in a class, there is static binding.
Example of static binding
• class Dog{
• private void eat()
• {System.out.println("dog is eating..."); }
• public static void main(String args[])
• { Dog d1=new Dog();
• d1.eat();
• } }
H-TECH IT SOLUTION 70
DYNAMIC BINDING
• When type of the object is determined at run-time, it is known as dynamic binding.
• Example of dynamic binding:
• class Animal{
• void eat(){System.out.println("animal is eating...");}
• }
• class Dog extends Animal{
• void eat(){System.out.println("dog is eating...");}
• public static void main(String args[])
• { Animal a=new Dog();
• a.eat(); } }//output will be :dog is eating…
H-TECH IT SOLUTION 71
ABSTRACTION IN JAVA
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.
Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
1. Abstract class
2. Interface
H-TECH IT SOLUTION 72
ABSTRACT CLASS IN JAVA
• A class which is declared as abstract is known as an abstract class. It can have abstract and
non-abstract methods. It needs to be extended and its method implemented. It cannot be
instantiated.
Points to Remember
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.
H-TECH IT SOLUTION 75
INTERFACE IN JAVA
An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance
in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
H-TECH IT SOLUTION 76
WHY USE JAVA INTERFACE?
H-TECH IT SOLUTION 77
HOW TO DECLARE AN INTERFACE?
• An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the fields are
public, static and final by default. A class that implements an interface must implement all the
methods declared in the interface.
• Syntax:
• interface <interface_name>{
• // declare constant fields
• // declare methods that abstract
• // by default.
• }
H-TECH IT SOLUTION 78
THE
As shownRELATIONSHIP BETWEEN
in the figure given below, a class extends CLASSES
another class, an interface extends another
AND INTERFACES
interface, but a class implements an interface.
H-TECH IT SOLUTION 79
H-TECH IT SOLUTION 80
ACCESS MODIFIERS IN JAVA
There are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.
• There are 4 types of java access modifiers:
1. Private:-The private access modifier is accessible only within class.
3. Protected:-The protected access modifier is accessible within package and outside the package but through
inheritance only.
4. Public:-The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
There are many non-access modifiers such as static, abstract, synchronized,
native, volatile, transient etc.
H-TECH IT SOLUTION 81
ENCAPSULATION IN JAVA
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.
We can create a fully encapsulated class in Java by making all the data members of the class private.
Advantage of Encapsulation in Java
By providing only a setter or getter method, you can make the class read-only or write-only. In other
words, you can skip the getter or 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.
The encapsulate class is easy to test. So, it is better for unit testing.
H-TECH IT SOLUTION 82
ARRAY IN JAVA
H-TECH IT SOLUTION 83
JAVA PACKAGE
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Advantage of Java Package
• 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
• 2) Java package provides access protection.
• 3) Java package removes naming collision.
H-TECH IT SOLUTION 84
SIMPLE EXAMPLE OF PACKAGE
• The package keyword is used to create a
• package in java.
• //save as Simple.java
• package mypack;
• public class Simple
• {
• public static void main(String args[])
• {
• System.out.println("Welcome to package");
• } }
H-TECH IT SOLUTION 85
HOW TO COMPILE & RUN JAVA PACKAGE
If you are not using any IDE, you need to follow the syntax given below:
• javac -d directory javafilename
• For example:
• javac -d . Simple.java
• The -d switch specifies the destination where to put the generated class file. You can use any
directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep
the package within the same directory, you can use . (dot).
How to run java package program:
• To Compile: javac -d . Simple.java To Run: java mypack.Simple
• Output:Welcome to package
H-TECH IT SOLUTION 86
HOW TO ACCESS PACKAGE FROM
ANOTHER
There are three ways toPACKAGE?
access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
• 1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not
subpackages.
The import keyword is used to make the classes and interface of another package accessible to the
current package.
H-TECH IT SOLUTION 87
Example Of Package That Import The
1)Packagename.*
• //save by A.java //Save by B.java
• package pack;
package mypack;
• public class A import pack.*;
class B
• {
{
• public void msg() public static void main(String args[])
{
• { A obj = new A();
• System.out.println("Hello"); obj.msg();
}
• } }
• }
Output:Hello
•
H-TECH IT SOLUTION 88
2)Using Package_Name.Classname
If you import package.classname then only declared
class of this package will be accessible.
Example of package by import package.classname
//Save by B.java
• //save by A.java
• package pack; package mypack;
import pack.A;
• public class A
• { class B
{
• public void msg() public static void main(String args[])
• { {
A obj = new A();
• System.out.println("Hello");
obj.msg();
• } } }
}
Output: Hello
H-TECH IT SOLUTION 89
3) Using
If you Fullyname
use fully qualified Qualified
then only declaredName
class of this package will be accessible.
Now there is no need to import.
But you need to use fully qualified name every time when you are accessing the class or interface.
It is generally used when two packages have same class name.
Ex: java.util and java.sql packages contain Date class.
H-TECH IT SOLUTION 90
EXAMPLE OF PACKAGE FULLY
•QUALIFIED NAME
//save by B.java
//save by A.java
package mypack;
• package pack; class B
• public class A {
public static void main(String args[])
• { {
• public void msg() pack.A obj = new pack.A();//using fully qualified name
obj.msg();
• { }
• }
System.out.println("Hello");
• } Output: Hello
• }
H-TECH IT SOLUTION 91
SUBPACKAGE IN JAVA
Package inside the package is called the subpackage.
It should be created to categorize the package further.
Let's take an example, Sun Microsystem has definded a package named java that contains many
classes like System, String, Reader, Writer, Socket etc.
• Example: package com.javatpoint.core;
• class Simple{
• public static void main(String args[]){
• System.out.println("Hello subpackage"); } }
• To Compile: javac -d . Simple.java
• To Run: java com.javatpoint.core.Simple Output:Hello
subpackage
H-TECH IT SOLUTION 92
PACKAGE CLASS
The package class provides methods to get information about the specification and implementation
of a package.
It provides methods such as getName(), getImplementationTitle(), getImplementationVendor(),
getImplementationVersion() etc.
Examples-->
H-TECH IT SOLUTION 93
JAVA ACCESS MODIFIERS
H-TECH IT SOLUTION 94
WRAPPER CLASS IN JAVA
Wrapper class in java provides the mechanism to convert primitive into object and object into
primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into
primitive automatically.
The automatic conversion of primitive into object is known as autoboxing and vice-versa
unboxing.
The eight classes of java.lang package are known as wrapper classes in java. The list of eight
wrapper classes are given below:
H-TECH IT SOLUTION 95
H-TECH IT SOLUTION 96
MULTITHREADING IN JAVA
What is Thread in java
• A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution.
• Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It uses
a shared memory area.
Multithreading in java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and
multithreading, both are used to achieve multitasking.
we use multithreading than multiprocessing because threads use a shared memory area. They don't
allocate separate memory area so saves memory
Java Multithreading is mostly used in games, animation, etc.
H-TECH IT SOLUTION 97
LIFE CYCLE
The life cycle of the thread in OF A THREAD
java is controlled by JVM. The java thread states are as follows:
H-TECH IT SOLUTION 98
• Java Thread Example by extending Thread class
• class Multi extends Thread
• {
• public void run()
• {
• System.out.println("thread is running...");
• }
• public static void main(String args[]){
• Multi t1=new Multi();
• t1.start(); } }
• Output:thread is running...
H-TECH IT SOLUTION 99
EXCEPTION
• The Exception Handling inHANDLING IN JAVA
Java is one of the powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained.
What is Exception in Java
• Dictionary Meaning: Exception is an abnormal condition.
• In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is
thrown at runtime.
What is Exception Handling
• Exception Handling is a mechanism to handle runtime errors such ClassNotFoundException,
IOException, SQLException, RemoteException, etc.
2) Unchecked Exception:
o ArithmeticException,
o NullPointerException,
o ArrayIndexOutOfBoundsException etc.
o Unchecked exceptions are not checked at compile-time, but they
are checked at runtime.
3) Error:
Error is irrecoverable :.
o OutOfMemoryError,
o VirtualMachineError,
o AssertionError etc.