JAVA INTRODUCTION
JAVA INTRODUCTION
JAVA INTRODUCTION
- Java is used to create mobile devices, softwares, web apss, desktop apps etc. etc.
- Java Code is processed through a compiler which translated our code into bytecode that an
operating system can read.
Our JAVA Code -------------> Compiler -----------> byteCode (001110010101010101) ------> OS can read
==================================================
- Object Oriented : Java uses the concepts like classes and objects. Java is a Object Oriented
Language.
- Platform Independence : It works on the concept of WORA (Write Once and Run Anywhere)
Lets say we can write the code on Windows Machine and we can run in Linux Machine.
- Automatic Memory Management : Java is basically having a terms as garbage collector which
automatically handles the memory allocation.
===================================================
2) JRE
3) JVM
Whenever we are supposed to write the code, we need to have JDK for sure.
It is a package that have all the neccesary libraries that we need to run the code.
JRE is the complete enviroment for running Java Code while JVM is a specific component within JRE
that actually executes the code.
===========================================================================
Java Setup
go to google.com
Go to Google.com
Download JAVA
Download ECLIPSE
1) Download JAVA
2) Install JAVA
It will show you the version of JDK that you have installed.
-----------------------------------------------------------------
6- Click New
10-Click OK
11- Click OK
Answer : Sometimes, the system is unable to find Java in our machine so whenever we are adding
this bin path to Environment Variable,
------------------------------------------------------------------
NO
------------------------------------------------------------------
3) Go to Mac tab
6) go to terminal
------------------------------------------------------------------
4) It will redirect you to a new page, click on this new download button.
6) Open your downloads folder and double click on downloaded .exe file of ECLIPSE.
8) Open Eclipse.
9) It will ask you a couple of options like "Eclipse IDE for Java Developers etc". ?
===================================================================
1) Click file
2) Click New
3) Select "Project".
6) Cilck on Finish
===================================================================
Double Click on Project and then right click on the src folder
Go to new and select the new package and create any new package with any name (Package name
should start with small Letter).
Go to New
click on "Class"
Now give any class name and make sure to start it with Capital letter.
Cick OK
===================================================================
===================================================================
To print anything in Console log in Java, we use System.out.println("any string you can pass here");
(The shortcut to write System.out.println is -> write syso and then press Ctrl + space)
===================================================================
2 types
- int
- double
- float
- char
- boolean
- byte
- short
- long
====================================================================
Example :
=====================================================================
Example is :
======================================================================
float : This is also used for decimal numbers but it less precise than double.
Example is :
=====================================================================
char :
Example is :
=====================================================================
Example :
=====================================================================
String :
It is a sequence of characters.
Example :
String myStr = "Subhadrshini, Prasanthi and Anmol comes in evening Java Automation Batch";
=====================================================================
short range :
-32768 to 32767
=====================================================================
DATA TYPE :
Primitive
Non-Primitive
=====================================================================
String name1 = "Hmanshu";
String nameAfterConcatination;
System.out.println(nameAfterConcatination);
=====================================================================
How to write a variable name (common rule for all the data type):
Wrong Syntax :
Correct Syntax :
Wrong Syntax :
Correct Syntax :
String firstName = "Himanshu";
Wrong Syntax :
Correct Syntax :
camelCasing : first word should be small, and first letter of every other word should be in CAPS.
=================================================================
if (compare) {
body
}
it is not neccesary to write the else statement.
=================================================================
if (compare) {
body
else {
body
=================================================================
switch case :
package basicJavaPrograms;
useSwitchCase();
}
public static void useSwitchCase() {
int day = 2;
switch(day) {
case 1:
System.out.println("Its Monday");
case 2:
System.out.println("Its Tuesday");
case 3:
System.out.println("Its Wednesday");
case 4:
System.out.println("Its Thursday");
case 5:
System.out.println("Its Friday");
default :
=========================================================
package basicJavaPrograms;
useSwitchWithLoop();
int day = 2;
switch(day) {
case 1:
System.out.println("Its Monday");
break;
case 2:
System.out.println("Its Tuesday");
break;
case 3:
System.out.println("Its Wednesday");
break;
case 4:
System.out.println("Its Thursday");
break;
case 5:
System.out.println("Its Friday");
break;
default :
case "Himanshu":
System.out.println("This is a mentor");
break;
case "Prasanthi":
System.out.println("This is a learner");
break;
case "Anmol":
System.out.println("This is a learner");
break;
default :
System.out.println("not sure");
break;
switch(name) {
case "Himanshu":
System.out.println("This is a mentor");
break;
case "Prasanthi":
System.out.println("This is a learner");
break;
case "Anmol":
System.out.println("This is a learner");
break;
default :
System.out.println("not sure");
break;
name = "Anmol";
==================================================================================
===
Keywords in Java :
package basicJavaPrograms;
/* Keywords are some reserved words that have a predefined meaning and can not be used
an identifier
*/
// class
// public
// private
// protected
// static
// void
// int
// String
// float
// double
// short
// long
// byte
// char
// boolean
// if
// else
// for
// while
// do while
// continue
// break
// return
// final
// finally
// finalize
// switch
// super
// this
// case
// try
// catch
// finally
// throw
// throws
// abstract
// package
// new
// extends
// implements
// default
// interface
==================================================================================
===
Array In Java
- Array is always fixed in size (we need to define the size of an array when declaring it.
int[] j = new int[8]; // This is the another way to define the array
int i[] = {2,14,78,90}; // this is also correct (very rare people use this)
int []i = {15,60,89,70}; // this is also correct (very rare people use this)
==================================================================================
===
ArrayIndexOutOfBoundsException :
==================================================================================
===
package basicJavaPrograms;
methodForStringArray();
for(int i=0;i<myArr.length;i++) {
System.out.println(myArr[i]);
==================================================================================
===
String class
String pool : if we have multiple variables with the same value, all the variables are pointing out to
the same value.
charAtMethod();
// String Class
String s1 = "Himanshu";
String s2 = "Himanshu";
if(s1==s3) {
if(s1.equals(s3)) {
if(name1.equals(name2)) {
else {
if(name1.equalsIgnoreCase(name2)) {
else {
}
public static void charAtMethod() {
==================================================================================
====
Access Modifiers
OOPS Concepts
==================================================================================
====
1) Static Variable
2) Instance Variable
3) Local Variable
Local Variable :
package basicJavaPrograms;
methodForlocalVariableUsage();
int i=10;
i = 1000;
==================================================================================
===
Static Variable :
// static variable is having its existence at the class level
// we can't use the static keyword for a variable in a method because static works on class level
// Any other class can access the static variable/method of another class by using its className
package basicJavaPrograms;
int a = 10;
int b = 20;
int c = a + b;
package basicJavaPrograms;
ClassForStaticVariableInJava.sum();
============================================================================
whenever we put "new" keyword before the classNameWithParanthesis(), its object is created.
package basicJavaPrograms;
}
public static void sum() {
package basicJavaPrograms;
myObject.multiply();
myObject.divide();
Class1.sum();
Class1.subtract();
package basicJavaPrograms;
myObject.multiply();
myObject.divide();
Class1.sum();
Class1.subtract();
myObject2.multiply();
myObject2.divide();
}
=============================================================================
Constructor In Java :
1) Constructor in java means a method that is having exactly the same name as className.
3) Contructor will be called automatically when the object of its class is created.
package basicJavaPrograms;
public ClassForConstuctor() {
System.out.println("i am m1");
System.out.println("i am m2");
}
package basicJavaPrograms;
myObject.m1();
myObject.m2();
// Non static methods are called with the object in another class
package basicJavaPrograms;
m2();
}
public ClassForConstuctor() {
System.out.println("i am m1");
System.out.println("i am m2");
new ClassForConstuctor();
What is a parameter :
package basicJavaPrograms;
subtract(25, 25);
m3(58, "Himanshu");
}
int c = a+b;
int e = c - d;
Parameterized Contsructor :
package basicJavaPrograms;
package basicJavaPrograms;
public Class5() {
}
public Class5(int number) {
package basicJavaPrograms;
package basicJavaPrograms;
new Class5();
new Class5(15);
new Class5("Guvi");
===============================================================================
Access Modifiers :
1) Default
2) public
3) private
4) protected
==================================================================
- OOPS Concepts (Encapsulation - Getters and Setters, Abstraction , Polymorphism - Overloading and
Overridding, Inheritance)
- Interface Implementation
==================================================================
OOPS :
1) Encapsulation
2) Polymorphism
3) Abstraction
4) Inheritance
-------------------------------------------------------------------
Polymorphism :
Poly - many
phism - forms
Overloading
Overridding
Overloading :
We have same method name in the same class, but we have 3 conditions in overloading.
3) methods having same name, no. of parameters are same, type of parameters are same but order
of paramters are different
package basicJavaPrograms;
sum(50, 50);
sum(2, 2, 3);
sum(20, "Prasanthi");
sum("Prasanthi",30);
int c = a + b;
int d = a + b + c;
============================================================================
Assignment is :
Q1 : Make a class with 5 constructors and call all these 5 constructors in same class by its object
Q2 : Make a class with 5 constructors and call all these 5 constructors in another class by its object
Q3 : Make a class with 5 overloaded methods and call all these 5 methods in the same class.
Q4 : Make a class with 5 overloaded methods and call all these 5 methods in the another class.
Status of Assignment :
Subhadarshini - Yes
Prasanthi - 3 Done
Jegan G - Yes
Sathya - Yes
Shaffna - Yes
Manish - Yes
Prabhu - NO
Mukilan - NO
Madhunika - NO
Anmol - NO
Mohammad - NO
Anbarasan - NO
durairam - NO
============================================================================
- OOPS Concepts (Encapsulation - Getters and Setters, Abstraction , Polymorphism - Overloading and
Overridding, Inheritance)
- Interface Implementation
============================================================================
OOPS Concepts :
1) Polymorphism
2) Abstraction
3) Encapsulation
4) Inheritance
Overridding :
Another names of overridding are : "Run Time Polymorphism", and "Dynamic Method Dispatch".
-----------------------------------------------------------------------------
What is Inheritance :
- Inheritance is one of the OOPS concept wherein one class can inhibit the data members of another
class by using Parent Child relation.
Child Class can access parent class methods freely without creating the Object.
===============================================================================
package basicJavaPrograms;
m1Static();
m2Static();
}
m2Static();
m1Static();
m2NonStatic();
===============================================================================
Body of method
package basicJavaPrograms;
int sumOf3Numbers;
return c;
==================================================================================
===
package basicJavaPrograms;
public class UseOfReturnKeyword {
int sumOf3Numbers;
int d = 5;
int e = 5;
int f = c + d + e;
return f;
==================================================================================
===
Overridding :
ClassA - Child
ClassB - Parent
ClassC - Overridding
package basicJavaPrograms;
package basicJavaPrograms;
package basicJavaPrograms;
// obj1.showGuviDetails();
System.out.println("=================================");
// obj2.showGuviDetails();
System.out.println("=================================");
obj4.showGuviDetails();
obj4.showGuviDetails();
===================================================================
Encapsulation :
this.num means we are referring the num variable of same class where in this line is written.
----------------------------------------------------------------
package basicJavaPrograms;
this.num = num;
return this.num;
package basicJavaPrograms;
myObj.setNum(10);
myObj.getNum();
================================================================================
Inheritance :
package basicJavaPrograms;
sum();
subtract();
package basicJavaPrograms;
package basicJavaPrograms;
=================================================================================
package basicJavaPrograms;
package basicJavaPrograms;
myObj.callSumMethod();
super.sum();
- Whenever the child class is called, its parent class constructor will always be called.
- It means if we have constructor in child class and parent class both, in that case also whenever we
are creating the object of child class,
first of all Parent Class Constuctor will be called and then child class constructor will be called.
package basicJavaPrograms;
public ClassParent1() {
package basicJavaPrograms;
myObj.callSumMethod();
public ClassChild1() {
super.sum();
}
==================================================================================
package basicJavaPrograms;
public ClassChild2() {
package basicJavaPrograms;
public ClassParent2() {
}
package basicJavaPrograms;
public ClassGrandParent2() {
=============================================================
Abstraction :
What is abstraction :
- This is one of the OOPS concept of Java wherein we are hidding the implemenation details from the
actual user.
- User only calls the method and not aware of the logic written inside.
- One Abstract Class can have one of more than one abtract method.
- Concrete methods are the methods which have body (where in abstract methods are the one
which does not have any body).
- Abstract Classes cant be instantiated (We cant create the object of Abstract Class).
- When any other class extends the abstract class, it always have to have all the abstract methods
inside it.
- When any other class extends the abstract class, it always need to give the body of all the abstract
methods inside.
package basicJavaPrograms;
// abstract method
package basicJavaPrograms;
myObj.sleep();
myObj.sound();
@Override
package basicJavaPrograms;
myObj.sleep();
myObj.sound();
@Override
System.out.println("Lion Roars");
===========================================================================
package basicJavaPrograms;
package basicJavaPrograms;
@Override
@Override
@Override
@Override
@Override
myObj.carName();
myObj.carColor();
myObj.tyreType();
myObj.fuelType();
myObj.isSunroofAvailable();
myObj.altoCarSpeciality();
myObj.callNumberOfWheels();
numberOfWheels();
}
}
package basicJavaPrograms;
@Override
@Override
@Override
@Override
@Override
myObj.carName();
myObj.carColor();
myObj.tyreType();
myObj.fuelType();
myObj.isSunroofAvailable();
myObj.hyundaiCarSpeciality();
myObj.callNumberOfWheels();
numberOfWheels();
=================================================================================
Assignment :
methods.
=================================================================================
Interface :
- Interface is the one wherein all the methods are abstract method.
- There is no need to use abstract keyword (by default all methods are abstract in an interface).
In Java Classes, multilevel inheritance is allowed but multiple inheritance is not allowed.
If we want to use multiple inheritance in java, this can only be achieved using Interface.
Interface is giving a basic architecture (basic methods) one class must have when it is implementing
that interface.
One interface can extend another interface by using the word "extends" ( we cant use implements
keyword here).
Note :
Multiple inheritance is not allowed in case of Abstract Classes. (one abstract class cant extend more
than one abstract class).
Multiple inheritance is not allowed in case of normal classes. (one class cant not extend more than
one class)
Mulitiple inheritance is only allowed in case of interfaces. (one class can implement any number of
interfaces).
Important Point : One class can extend one class and can also implement interface.
================================================================================
package basicJavaPrograms;
void mobileColor();
void mobileScreenSize();
void mobileRamSize();
void mobileBattery();
package basicJavaPrograms;
void iOSVersion();
void chargerType();
package basicJavaPrograms;
public class ApplePhoneClass implements MobileInterface, AppleDeviceInterface{
@Override
@Override
@Override
@Override
@Override
@Override
}
}
package basicJavaPrograms;
void androidVersion();
void isCallRecordingFeatureAvailable();
package basicJavaPrograms;
@Override
@Override
@Override
}
@Override
@Override
@Override
==================================================================================
Assignment is :
Create one more interface I2 with 2 new methods and that extends the above interface.
Create a class that implements only I2 (not I1) and check how many methods we need to implement
in this class.
Send me the code in chat window once done.
You have 5-10 minutes to work on it, we will connect at 06:20 to discuss.
==================================================================================
Abhiramasundari -
Anbarasan - Yes
Anish - Yes
Anmol -
Rathi - Yes
Deepa - NO
Durairam - No Show
Jegan - Yes
Madhunika -
Manish - Yes
Prabhu - NO
Rajeswari - No Show
Sathya - Yes
Shaffna - Yes
Somasekar -
Subhadarshini - Yes
==================================================================================
=
=> As we all know that we can never create the object of an interface so we cant create the object of
collection.
We can create the object of collections class (It means we can only instantiate the collections classes
but not the collection interface).
Collections classes has so many predefined methods for searching, sorting etc.
==================================================================================
====
List :
- List is an interface.
Lets say : I have so many fruits and I need to add the fruits name in a list.
List<String> myFruitsList = new ArrayList<>();
using get method of list and pass the index in this get method (index is always 1 less than the
position).
==================================================================================
=====
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.List;
methodFordynamicValuesInAList("Himanshu","Subhadarshini","Manish");
methodFordynamicValuesInAList("Anmol","Prasanthi","Anish");
myFruitsList.add("Apple");
myFruitsList.add("Mango");
myFruitsList.add("Orange");
myList.add(value1);
myList.add(value2);
myList.add(value3);
System.out.println(myList);
=================================================================================
Assignment:
Take 3rd and 7th value from this List and return the sum of these 2 numbers.
Create a new list and add this addition in this new list named as myList2.
Lets connect at 08:15 (you guys have 10 minutes to work on this). OKAY ?????????
=================================================================================
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.List;
methodForAssignment();
myList1.add(10);
myList1.add(25);
myList1.add(38);
myList1.add(42);
myList1.add(70);
myList1.add(90);
myList1.add(100);
myList1.add(120);
myList1.add(11);
myList1.add(14);
myList2.add(sum);
// System.out.println(myList2);
=========================================================================
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.List;
methodForListAssignment();
for(int index=0;index<myArray.length;index++) {
myList.add(myArray[index]);
myList2.add(sum);
==================================================================================
==
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.List;
methodForListAssignment();
for(int index=0;index<myArray.length;index++) {
myList.add(myArray[index]);
myList2.add(sum);
myList2.remove(0);
================================================================
Assignment :
3) Pass all these 10 values from the array to this new list (using the above for loop).
4) Add 2 more (new) values in this list.
10) Copy all values from list1 to list2 (created in step 9).
==================================================================================
=================
Jegan G - Yes
Subhadarshini - Yes
Somesekar - Yes
Abhiramasundari - Yes
Anbarasan - Yes
Anish - Yes
Durairam - Yes
Manish - Yes
Prabhu - Yes
Shaffna - Yes
Amritha - In Progress
Mohammed - No Show
Sathya -
Rajeshwari -
Anmol - Yes
Deepa - In Progress
==================================================================================
=======
LinkedList is Java :
Node1->Node2->Node3-> Node4
Node1<-Node2<-Node3<-Node4
Linked List :
==================================================================================
==========
LinkedList Implementation :
package createqadoc.CreateQADoc;
import java.util.LinkedList;
methodForLinkedListUnderstanding();
myList.add("Himanshu");
myList.add("Anmol");
myList.add("Manish");
myList.add("Prasanthi");
myList.add("Anish");
myList.add(2, "Subhdarshini");
System.out.println("my list is after adding new element in the middle is :: " +
myList);
myList.remove(1);
==================================================================================
====================
Set is an interface.
HashSet is class.
package createqadoc.CreateQADoc;
import java.util.HashSet;
import java.util.Set;
myMethodForSetUnderstanding();
}
mySet.add("Apple");
mySet.add("Banana");
mySet.add("Kiwi");
mySet.add("Potato");
mySet.add("Potato");
// 12, 16, 20, 67, 12, 78, 90, 82, 12, 16,20, 20, 21, 20 - ArrayList
// create a set of above numbers so that all the unique values will be fetched
// compare set with the List to get the iteration(no. of count) of all the integer values
==================================================================================
===========
Exception Handling :
Exception :
So that we can have an idea why and where the error is there in the code and we can fix it.
package createqadoc.CreateQADoc;
try {
System.out.println(myArray[15]);
catch (Exception e) {
e.printStackTrace();
==================================================================================
=================
Assignment :
Task 1 : Create one ArrayList and one LinkedList and write a program to add 10 numbers and then
delete 2 numbers
at index 5 and 6.
Task 2 : Create a list and add 10 values and create a new Hashmap and put all these 10 values in a
Hashmap as :
key is 1,2,3,4,5,6,7,8,9,10
1 - Himanshu
2 - Subhadarshini
3- Anmol
4 - Anish
Task 3 : Create a set and try to include duplicate in set, and check whether it is allowed or not ?
==================================================================================
==================
Task 1:
1st Hashmap :
1=Himanshu
2=Subhadarshini
3=Deepa
4=Prasanthi
5=Abhiramasundari
2nd HashMap :
6=Himanshu
7=Anmol
8=Manish
9=Anish
10=Mohammed
Task 2 :
Create a new LinkedList and add the values of hashmap in this linkedList.
==================================================================================
==================
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
addValuesToList();
firstHashMapHavingValuesFromList();
secondHashMapHavingValuesFromList();
myList.add("Himanshu");
myList.add("Subhadarshini");
myList.add("Deepa");
myList.add("Prasanthi");
myList.add("Abhiramasundari");
myList.add("Himanshu");
myList.add("Anmol");
myList.add("Manish");
myList.add("Anish");
myList.add("Mohammed");
return sizeOfMyList;
for(int index=0;index<sizeOfMyList/2;index++) {
myMap1.put(index+1, myList.get(index));
for(int index=5;index<sizeOfMyList;index++) {
myMap2.put(index+1, myList.get(index));
}
System.out.println("hashMap 2 is :: " + myMap2);
==================================================================================
===========
Another way to do it :
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
addValuesToList();
firstHashMapHavingValuesFromList();
secondHashMapHavingValuesFromList();
myList.add("Himanshu");
myList.add("Subhadarshini");
myList.add("Deepa");
myList.add("Prasanthi");
myList.add("Abhiramasundari");
myList.add("Himanshu");
myList.add("Anmol");
myList.add("Manish");
myList.add("Anish");
myList.add("Mohammed");
sizeOfList = myList.size();
for(int index=0;index<sizeOfList/2;index++) {
myMap1.put(index+1, myList.get(index));
for(int index=5;index<sizeOfList;index++) {
myMap2.put(index+1, myList.get(index));
}
==================================================================================
=========
Task 2 :
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
myArrayList.add("Himanshu");
myArrayList.add("Subhadarshini");
myArrayList.add("Deepa");
myArrayList.add("Prasanthi");
myArrayList.add("Abhiramasundari");
myArrayList.add("Himanshu");
myArrayList.add("Anmol");
myArrayList.add("Manish");
myArrayList.add("Anish");
myArrayList.add("Mohammed");
for(int index=0;index<myArrayList.size();index++) {
myMap.put(index+1, myArrayList.get(index));
}
System.out.println(myMap);
for(int index=0;index<myArrayList.size();index++) {
myLinkedList.add(index, myMap.get(index+1));
System.out.println(myLinkedList);
==================================================================================
=========
TreeMap:
HashMap allows one null key and TreeMap does not allow any null key.
HashMap allows multiple null values but TreeMap does not allow any null value.
HashMap does not have keys in the ordered way (asceding) but Treemap has keys in ordered form
(ordered does not mean indexing).
==================================================================================
=========
package basicJavaPrograms;
import java.util.TreeMap;
public class TreeMapImplementation {
myMethodToUnderstandTreeMap();
myTreeMap.put("Himanshu", "Mentor");
myTreeMap.put("Prasanthi", "Student");
myTreeMap.put("Manish", "Learner");
myTreeMap.put("Anmol", "Leaner");
System.out.println(myTreeMap);
==================================================================================
============
package basicJavaPrograms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
public class TreeMapImplementation {
myMethodToUnderstandTreeMap();
myTreeMap.put(1, "Mentor");
myTreeMap.put(2, "Student");
myTreeMap.put(3, "Learner");
myTreeMap.put(4, "ExpertLearner");
System.out.println(myTreeMap);
Collections.sort(myList);
System.out.println(myList);
==================================================================================
==========
Exception Handling :
- We need to handle such exception else our code will be stopped then and there (where we are
getting the exception).
- We use try, catch, finally blocks to handle exception.
- Exception is a class in Java that is the parent of all the other exception classes like :
ArrayIndexOutOfBoundsException, NullPointerException etc.
- Checked Exception
- Unchecked Exception.
==================================================================================
==========
Whenever we have anything risky, we can put it in a try block and we can catch the exception in
catch block.
Whenever we get the exception, the code after it will not be executed :
package basicJavaPrograms;
arrayIndexOutOfBoundException();
myArray[0] = "Subhadarshini";
myArray[1] = "Sathya";
System.out.println("================Code execution is
Started==============================");
System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
System.out.println("================Code execution is
done=================================");
==================================================================================
========================
try {
catch() {
=====================================================================
package basicJavaPrograms;
public class ExceptionHandling {
arrayIndexOutOfBoundException();
try {
myArray[0] = "Subhadarshini";
myArray[1] = "Sathya";
System.out.println("================Code execution is
Started==============================");
System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
System.out.println("================Code execution is
done=================================");
catch (Exception e) {
e.printStackTrace();
=====================================================================
So, whenever we get any exception in try block, the code automatically moves the execution to
catch block :
package basicJavaPrograms;
arrayIndexOutOfBoundException();
try {
myArray[0] = "Subhadarshini";
myArray[1] = "Sathya";
System.out.println("================Code execution is
Started==============================");
System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
catch (Exception e) {
e.printStackTrace();
System.out.println("================Code execution is
done=================================");
=====================================================================
package basicJavaPrograms;
getDivisionOfTwoNumbers(12, 6);
getDivisionOfTwoNumbers(12, 0);
getDivisionOfTwoNumbers(12, 2);
try {
catch (Exception e) {
e.printStackTrace();
=====================================================================
- Whenever you use childException, always use Parent exception at last so that if there is any new
child exception comes in your code, that will be
=====================================================================
package basicJavaPrograms;
getDivisionOfTwoNumbers(12, 6);
getDivisionOfTwoNumbers(12, 0);
getDivisionOfTwoNumbers(12, 2);
try {
catch (NullPointerException e) {
e.printStackTrace();
catch (Exception e) {
}
=====================================================================
package basicJavaPrograms;
getDivisionOfTwoNumbers(12, 6);
getDivisionOfTwoNumbers(12, 0);
getDivisionOfTwoNumbers(12, 2);
try {
catch (ArithmeticException e) {
e.printStackTrace();
=====================================================================
package basicJavaPrograms;
public class ExceptionHandling3 {
getDivisionOfTwoNumbers(12, 6);
getDivisionOfTwoNumbers(12, 0);
getDivisionOfTwoNumbers(12, 2);
try {
catch (ArithmeticException e) {
e.printStackTrace();
catch (NullPointerException e) {
catch (Exception e) {
=====================================================================
Finally block :
4) Finally block is used to write some important code like dbConnectionClose, ExcelClose,
DriverQuite etc.
=====================================================================
=====================================================================
try {
catch() {
finally {
=====================================================================
package basicJavaPrograms;
method2ForUnderstandingFinally();
try {
catch (Exception e) {
finally {
try {
System.out.println(myArray[10]);
catch (Exception e) {
finally {
=====================================================================
Throws keyword :
- It is just to help the other developrs so that if they are using the method they can handle the
exception.
- throws is used near the method signature (methodName where we have created the method).
- throws is just an information that whenever you are calling this method, this can have the
mentioned exception (mentioned near method signature)
=====================================================================
package basicJavaPrograms;
try {
methodToUnderstandThrows();
catch (ArrayIndexOutOfBoundsException e) {
}
public static void methodToUnderstandThrows()throws ArrayIndexOutOfBoundsException {
myArray[0] = 23;
myArray[1] = 56;
for(int index=0;index<=myArray.length;index++) {
System.out.println(myArray[index]);
======================================================================
======================================================================
throw is added in the code whenver we need to explicitly print the exception.
======================================================================
package basicJavaPrograms;
methodToUnderstandThrow(23);
methodToUnderstandThrow(123);
}
public static void methodToUnderstandThrow(int a) {
int age = a;
else {
========================================================================
package basicJavaPrograms;
methodToUnderstandThrow(23);
methodToUnderstandThrow(123);
methodToUnderstandThrow(500);
int age = a;
else {
========================================================================
package basicJavaPrograms;
methodToUnderstandThrow(23);
methodToUnderstandThrow(123);
methodToUnderstandThrow(5);
try {
int age = a;
else {
}
catch (ArithmeticException e) {
System.out.println("handle this");
===========================================================================
use only try and finally (when we dont want to catch the exception using catch) :
package basicJavaPrograms;
useTryAndFinally();
try {
finally {
}
============================================================================
package basicJavaPrograms;
useTryAndFinally();
try {
finally {
============================================================================
package basicJavaPrograms;
useTryAndFinally();
try {
System.exit(0);
finally {
Case 2 : If we have any exception in the finally block also, the complete finally block will not be
executed.
package basicJavaPrograms;
public class ExceptionHandling7 {
useTryAndFinally();
try {
finally {
System.out.println(myArray[10]);
==================================================================================
=======
18-Nov-2024 :
We call it as "workbook".
WORKBOOK
SHEET
ROW
COLUMN
CELL
==================================================================================
=========
PROJECT :
Students
Teachers
- Register Students.
TO DO :
1. Encapsulation :
Create any class that has private fields for creating getters and setters method.
2.Inheritance
Child Classes -> Student and Teacher (both the classes have common properties like name, age from
Person class).
3.Polymorphism
4.Abstraction
abstractClassForTeacher{
work();
Teacher Class extends this abstract class and implement work method and have some in this work
method.
==================================================================================
==================
Java Project :
In Java Project, we need download the JARS manually and we need to add them in the build Path of
the project.
Maven Project :
We dont need to download any JAR manually and we can simply add the dependencies in POM>xml
file and we are done.
==================================================================================
==================
Go to File -> New -> Project -> Maven Project -> Next -> Select any quickstart archietype -> Give any
name in artifact ID -> Click FINISH.
Press Enter.
Maven Project Creation is done (you will see a message as BUILD SUCCESS).
==================================================================================
=================
POM.xml :
<dependencies>
<dependency>
</dependency>
<dependency>
</dependency>
<dependency>
</dependency>
<dependency>
</dependency>
</dependencies>
==================================================================================
==================