String Interface Package Exception
String Interface Package Exception
Java String
1. char[] ch={'j','a','v','a','t','p','o','i','n','t'};
2. String s=new String(ch);
is same as:
1. String s="javatpoint";
1. By string literal
2. By new keyword
1) String Literal
int a=10;
float b=60.70f
1. String s="welcome";
Each time you create a string literal, the JVM checks the
"string constant pool" first. If the string already exists in
the pool, a reference to the pooled instance is returned.
If the string doesn't exist in the pool, a new string
instance is created and placed in the pool. For example:
1. String s1="Welcome";
2. String s2="Welcome";//It doesn't create a new instance
2) By new keyword
1. String s=new String("Welcome");//creates two objects a
nd one reference variable
StringExample.java
Output:
java
strings
example
Teststringcomparison1.java
1. class Teststringcomparison1{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. String s4="Saurav";
7. System.out.println(s1.equals(s2));//true
8. System.out.println(s1.equals(s3));//true
9. System.out.println(s1.equals(s4));//false
10. }
11. }
Output:
true
true
false
Teststringcomparison2.java
1. class Teststringcomparison2{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="SACHIN";
5.
6. System.out.println(s1.equals(s2));//false
7. System.out.println(s1.equalsIgnoreCase(s2));//true
8. }
9. }
Output:
false
true
2) By Using == operator
Teststringcomparison3.java
1. class Teststringcomparison3{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. System.out.println(s1==s2);//true (because both refer t
o same instance)
7. System.out.println(s1==s3);//false(because s3 refers to
instance created in nonpool)
8. }
9. }
Output:
true
false
1. class Teststringcomparison4{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3="Ratan";
6. System.out.println(s1.compareTo(s2));//0
7. System.out.println(s1.compareTo(s3));//1(because s1>s
3)
8. System.out.println(s3.compareTo(s1));//-
1(because s3 < s1 )
9. }
10. }
Test it Now
Output:
0
1
-1
8
Hello Java Program for Beginners
As you can see in the above figure that two objects are
created but s reference variable still refers to "Sachin"
not to "Sachin Tendulkar".
1. class Testimmutablestring1{
2. public static void main(String args[]){
3. String s="Sachin";
4. s=s.concat(" Tendulkar");
5. System.out.println(s);
6. }
7. }
Output:Sachin Tendulkar
1. class TestStringConcatenation1{
2. public static void main(String args[]){
3. String s="Sachin"+" Tendulkar";
4. System.out.println(s);//Sachin Tendulkar
5. }
6. }
Test it Now
Output:Sachin Tendulkar
1. class TestStringConcatenation2{
2. public static void main(String args[]){
3. String s=50+30+"Sachin"+40+40;
4. System.out.println(s);//80Sachin4040
5. }
6. }
Test it Now
80Sachin4040
Note: After a string literal, all the + will be treated as
string concatenation operator.
2) String Concatenation by concat() method
1. class TestStringConcatenation3{
2. public static void main(String args[]){
3. String s1="Sachin ";
4. String s2="Tendulkar";
5. String s3=s1.concat(s2);
6. System.out.println(s3);//Sachin Tendulkar
7. }
8. }
Test it Now
Sachin Tendulkar
Substring in Java
In case of String:
o startIndex: inclusive
o endIndex: exclusive
1. String s="hello";
2. System.out.println(s.substring(0,2)); //returns he as a sub
string
TestSubstring.java
Output:
Original String: SachinTendulkar
Substring starting from index 6:
Tendulkar
Substring starting from index 0 to 6:
Sachin
TestSubstring2.java
1. import java.util.*;
2.
3. public class TestSubstring2
4. {
5. /* Driver Code */
6. public static void main(String args[])
7. {
8. String text= new String("Hello, My name is Sachin")
;
9. /* Splits the sentence by the delimeter passed as an
argument */
10. String[] sentences = text.split("\\.");
11. System.out.println(Arrays.toString(sentences));
12. }
13. }
Output:
[Hello, My name is Sachin]
Stringoperation1.java
Output:
SACHIN
sachin
Sachin
Java String trim() method
Stringoperation2.java
Output:
Sachin
Sachin
Java String startsWith() and endsWith() method
Stringoperation3.java
Output:
true
true
Java String charAt() Method
Stringoperation4.java
Stringoperation5.java
Output:
6
Java String intern() Method
Stringoperation6.java
Output:
Sachin
Java String valueOf() Method
Stringoperation7.java
Output:
1010
Java String replace() Method
Stringoperation8.java
Output:
Kava is a programming language. Kava is a
platform. Kava is an Island.
StringBufferExample.java
00:15/05:19
10 Sec
6.1M
149
Exception Handling in Java - Javatpoint
1. class StringBufferExample{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello ");
4. sb.append("Java");//now original string is changed
5. System.out.println(sb);//prints Hello Java
6. }
7. }
Output:
Hello Java
2) StringBuffer insert() Method
StringBufferExample2.java
1. class StringBufferExample2{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello ");
4. sb.insert(1,"Java");//now original string is changed
5. System.out.println(sb);//prints HJavaello
6. }
7. }
Output:
HJavaello
3) StringBuffer replace() Method
StringBufferExample3.java
1. class StringBufferExample3{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello");
4. sb.replace(1,3,"Java");
5. System.out.println(sb);//prints HJavalo
6. }
7. }
Output:
HJavalo
4) StringBuffer delete() Method
StringBufferExample4.java
1. class StringBufferExample4{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello");
4. sb.delete(1,3);
5. System.out.println(sb);//prints Hlo
6. }
7. }
Output:
Hlo
5) StringBuffer reverse() Method
StringBufferExample5.java
1. class StringBufferExample5{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello");
4. sb.reverse();
5. System.out.println(sb);//prints olleH
6. }
7. }
Output:
olleH
6) StringBuffer capacity() Method
StringBufferExample6.java
1. class StringBufferExample6{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer();
4. System.out.println(sb.capacity());//default 16
5. sb.append("Hello");
6. System.out.println(sb.capacity());//now 16
7. sb.append("java is my favourite language");
8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (
oldcapacity*2)+2
9. }
10. }
Output:
16
16
34
7) StringBuffer ensureCapacity() method
StringBufferExample7.java
1. class StringBufferExample7{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer();
4. System.out.println(sb.capacity());//default 16
5. sb.append("Hello");
6. System.out.println(sb.capacity());//now 16
7. sb.append("java is my favourite language");
8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (
oldcapacity*2)+2
9. sb.ensureCapacity(10);//now no change
10. System.out.println(sb.capacity());//now 34
11. sb.ensureCapacity(50);//now (34*2)+2
12. System.out.println(sb.capacity());//now 70
13. }
14. }
Output:
16
16
34
34
70
StringBuilderExample.java
77.6K
8. Modules and PHP Abstract class | Build a CMS using OOP
PHP CMS tutorial MVC [2020]
1. class StringBuilderExample{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder("Hello ");
4. sb.append("Java");//now original string is changed
5. System.out.println(sb);//prints Hello Java
6. }
7. }
Output:
Hello Java
2) StringBuilder insert() method
StringBuilderExample2.java
1. class StringBuilderExample2{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder("Hello ");
4. sb.insert(1,"Java");//now original string is changed
5. System.out.println(sb);//prints HJavaello
6. }
7. }
Output:
HJavaello
3) StringBuilder replace() method
StringBuilderExample3.java
1. class StringBuilderExample3{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder("Hello");
4. sb.replace(1,3,"Java");
5. System.out.println(sb);//prints HJavalo
6. }
7. }
Output:
HJavalo
4) StringBuilder delete() method
StringBuilderExample4.java
1. class StringBuilderExample4{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder("Hello");
4. sb.delete(1,3);
5. System.out.println(sb);//prints Hlo
6. }
7. }
Output:
Hlo
5) StringBuilder reverse() method
StringBuilderExample5.java
1. class StringBuilderExample5{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder("Hello");
4. sb.reverse();
5. System.out.println(sb);//prints olleH
6. }
7. }
Output:
olleH
6) StringBuilder capacity() method
StringBuilderExample6.java
1. class StringBuilderExample6{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder();
4. System.out.println(sb.capacity());//default 16
5. sb.append("Hello");
6. System.out.println(sb.capacity());//now 16
7. sb.append("Java is my favourite language");
8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (
oldcapacity*2)+2
9. }
10. }
Output:
16
16
34
7) StringBuilder ensureCapacity() method
StringBuilderExample7.java
1. class StringBuilderExample7{
2. public static void main(String args[]){
3. StringBuilder sb=new StringBuilder();
4. System.out.println(sb.capacity());//default 16
5. sb.append("Hello");
6. System.out.println(sb.capacity());//now 16
7. sb.append("Java is my favourite language");
8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (
oldcapacity*2)+2
9. sb.ensureCapacity(10);//now no change
10. System.out.println(sb.capacity());//now 34
11. sb.ensureCapacity(50);//now (34*2)+2
12. System.out.println(sb.capacity());//now 70
13. }
14. }
Output:
16
16
34
34
70
StringBuffer Example
//Java Program to demonstrate the use of StringBuffer clas
s.
public class BufferTest{
public static void main(String[] args){
StringBuffer buffer=new StringBuffer("hello");
buffer.append("java");
System.out.println(buffer);
}
}
hellojava
StringBuilder Example
1. //Java Program to demonstrate the use of StringBuilder
class.
2. public class BuilderTest{
3. public static void main(String[] args){
4. StringBuilder builder=new StringBuilder("hello");
5. builder.append("java");
6. System.out.println(builder);
7. }
8. }
hellojava
Performance Test of StringBuffer and StringBuilder
Interface in Java
1. Interface
2. Example of Interface
3. Multiple inheritance by Interface
4. Why multiple inheritance is supported in Interface while
it is not supported in case of class.
5. Marker Interface
6. Nested Interface
Syntax:
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
Java 8 Interface Improvement
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Test it Now
Output:
Hello
Java Interface Example: Drawable
File: TestInterface1.java
Output:
drawing circle
Java Interface Example: Bank
File: TestInterface2.java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10. class TestInterface2{
11. public static void main(String[] args){
12. Bank b=new SBI();
13. System.out.println("ROI: "+b.rateOfInterest());
14. }}
Test it Now
Output:
ROI: 9.15
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. A7 obj = new A7();
13. obj.print();
14. obj.show();
15. }
16. }
Test it Now
Output:Hello
Welcome
Q) Multiple inheritance is not supported through class in
java, but it is possible by an interface, why?
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void print();
6. }
7.
8. class TestInterface3 implements Printable, Showable{
9. public void print(){System.out.println("Hello");}
10. public static void main(String args[]){
11. TestInterface3 obj = new TestInterface3();
12. obj.print();
13. }
14. }
Test it Now
Output:
Hello
Interface inheritance
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }
Test it Now
Output:
Hello
Welcome
Java 8 Default Method in Interface
File: TestInterfaceDefault.java
1. interface Drawable{
2. void draw();
3. default void msg(){System.out.println("default method")
;}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangl
e");}
7. }
8. class TestInterfaceDefault{
9. public static void main(String args[]){
10. Drawable d=new Rectangle();
11. d.draw();
12. d.msg();
13. }}
Test it Now
Output:
drawing rectangle
default method
Java 8 Static Method in Interface
File: TestInterfaceStatic.java
1. interface Drawable{
2. void draw();
3. static int cube(int x){return x*x*x;}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangl
e");}
7. }
8.
9. class TestInterfaceStatic{
10. public static void main(String args[]){
11. Drawable d=new Rectangle();
12. d.draw();
13. System.out.println(Drawable.cube(3));
14. }}
Test it Now
Output:
drawing rectangle
27
Q) What is marker or tagged interface?
1. interface printable{
2. void print();
3. interface MessagePrintable{
4. void msg();
5. }
6. }
More about Nested Interface
Difference between abstract class and interface
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to
100%) whereas interface achieves fully abstraction
(100%).
Example of abstract class and interface in Java
Output:
I am a
I am b
I am c
I am d
Java Package
1. Java Package
2. Example of package
3. Accessing package
1. By import packagename.*
2. By import packagename.classname
3. By fully qualified name
4. Subpackage
5. Sending class file to another directory
6. -classpath switch
7. 4 ways to load the class file or jar file
8. How to put two public class in a package
9. Static Import
10. Package class
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
How to compile java package
For example
1. javac -d . Simple.java
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
2) Using packagename.classname
Subpackage in java
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
To Compile:
o Temporary
o By setting the classpath in the command
prompt
o By -classpath switch
o Permanent
o By setting the classpath in the environment
variables
o By creating the jar file, that contains all the
class files, and copying the jar file in the
jre/lib/ext folder.
1. //save as A.java
2.
3. package javatpoint;
4. public class A{}
1. //save as B.java
2.
3. package javatpoint;
4. public class B{}
Package class
1. class PackageInfo{
2. public static void main(String args[]){
3.
4. Package p=Package.getPackage("java.lang");
5.
6. System.out.println("package name: "+p.getName());
7.
8. System.out.println("Specification Title: "+p.getSpecificati
onTitle());
9. System.out.println("Specification Vendor: "+p.getSpecific
ationVendor());
10. System.out.println("Specification Version: "+p.getSp
ecificationVersion());
11.
12. System.out.println("Implementaion Title: "+p.getImp
lementationTitle());
13. System.out.println("Implementation Vendor: "+p.get
ImplementationVendor());
14. System.out.println("Implementation Version: "+p.ge
tImplementationVersion());
15. System.out.println("Is sealed: "+p.isSealed());
16.
17.
18. }
19. }
Output:package name: java.lang
Specification Title: Java
Plateform API Specification
Specification Vendor: Sun
Microsystems, Inc.
Specification Version: 1.6
Implemenation Title: Java Runtime
Environment
Implemenation Vendor: Sun
Microsystems, Inc.
Implemenation Version: 1.6.0_30
IS sealed: false
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Method Description
1. Exception Handling
2. Advantage of Exception Handling
3. Hierarchy of Exception classes
4. Types of Exception
5. Exception Example
6. Scenarios where an exception may occur
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;
1. Checked Exception
2. Unchecked Exception
3. Error
Keyword Description
JavaExceptionExample.java
Output:
Exception in thread main
java.lang.ArithmeticException:/ by zero
rest of the code...
1. int a=50/0;//ArithmeticException
2) A scenario where NullPointerException occurs
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException
4) A scenario where ArrayIndexOutOfBoundsException
occurs
The catch block must be used after the try block only.
You can use multiple catch block with a single try block.
Internal Working of Java try-catch block
TryCatchExample1.java
Output:
Exception in thread "main"
java.lang.ArithmeticException: / by zero
TryCatchExample2.java
TryCatchExample3.java
Output:
java.lang.ArithmeticException: / by zero
TryCatchExample4.java
Output:
java.lang.ArithmeticException: / by zero
rest of the code
Example 5
TryCatchExample5.java
Output:
Can't divided by zero
Example 6
TryCatchExample6.java
Output:
25
Example 7
TryCatchExample7.java
Output:
Exception in thread "main"
java.lang.ArithmeticException: / by zero
Here, we can see that the catch block didn't contain the
exception code. So, enclose exception code within a try
block and use catch block only to handle the exceptions.
Example 8
TryCatchExample8.java
Output:
Exception in thread "main"
java.lang.ArithmeticException: / by zero
Example 9
TryCatchExample9.java
Output:
java.lang.ArrayIndexOutOfBoundsException:
10
rest of the code
Example 10
TryCatchExample10.java
1. import java.io.FileNotFoundException;
2. import java.io.PrintWriter;
3.
4. public class TryCatchExample10 {
5.
6. public static void main(String[] args) {
7.
8.
9. PrintWriter pw;
10. try {
11. pw = new PrintWriter("jtp.txt"); //may throw
exception
12. pw.println("saved");
13. }
14. // providing the checked exception handler
15. catch (FileNotFoundException e) {
16.
17. System.out.println(e);
18. }
19. System.out.println("File saved successfully");
20. }
21. }
Test it Now
Output:
File saved successfully
Example 1
MultipleCatchBlock1.java
14. {
15. System.out.println("ArrayIndexOutOfBou
nds Exception occurs");
16. }
17. catch(Exception e)
18. {
19. System.out.println("Parent Exception occ
urs");
20. }
21. System.out.println("rest of the code");
22. }
23. }
Test it Now
Output:
3.7M
60
Prime Ministers of India | List of Prime Minister of India
(1947-2020)
Arithmetic Exception occurs
rest of the code
Example 2
MultipleCatchBlock2.java
15. {
16. System.out.println("ArrayIndexOutOfBou
nds Exception occurs");
17. }
18. catch(Exception e)
19. {
20. System.out.println("Parent Exception occ
urs");
21. }
22. System.out.println("rest of the code");
23. }
24. }
Test it Now
Output:
ArrayIndexOutOfBounds Exception occurs
rest of the code
MultipleCatchBlock3.java
15. {
16. System.out.println("ArrayIndexOutOfBou
nds Exception occurs");
17. }
18. catch(Exception e)
19. {
20. System.out.println("Parent Exception occ
urs");
21. }
22. System.out.println("rest of the code");
23. }
24. }
Test it Now
Output:
Arithmetic Exception occurs
rest of the code
Example 4
14. {
15. System.out.println("ArrayIndexOutOfBou
nds Exception occurs");
16. }
17. catch(Exception e)
18. {
19. System.out.println("Parent Exception occ
urs");
20. }
21. System.out.println("rest of the code");
22. }
23. }
Test it Now
Output:
Parent Exception occurs
rest of the code
Example 5
MultipleCatchBlock5.java
1. class MultipleCatchBlock5{
2. public static void main(String args[]){
3. try{
4. int a[]=new int[5];
5. a[5]=30/0;
6. }
7. catch(Exception e){System.out.println("common task c
ompleted");}
8. catch(ArithmeticException e){System.out.println("task1
is completed");}
9. catch(ArrayIndexOutOfBoundsException e){System.out
.println("task 2 completed");}
10. System.out.println("rest of the code...");
11. }
12. }
Test it Now
Output:
Compile-time error
NestedTryBlock.java
Output:
When any try block does not have a catch block for a
particular exception, then the catch block of the outer
(parent) try block are checked for that exception, and if it
matches, the catch block of outer try block is executed.
NestedTryBlock.java
37. }
38. catch (Exception e5) {
39. System.out.print("Exception");
40. System.out.println(" handled in main try-
block");
41. }
42. }
43. }
Output:
Let's see the different cases where Java finally block can
be used.
2.3M
56
History of Java
Case 1: When an exception does not occur
TestFinallyBlock.java
1. class TestFinallyBlock {
2. public static void main(String args[]){
3. try{
4. //below code do not throw any exception
5. int data=25/5;
6. System.out.println(data);
7. }
8. //catch won't be executed
9. catch(NullPointerException e){
10. System.out.println(e);
11. }
12. //executed regardless of exception occurred or not
13. finally {
14. System.out.println("finally block is always executed")
;
15. }
16.
17. System.out.println("rest of phe code...");
18. }
19. }
Output:
TestFinallyBlock1.java
Output:
Case 3: When an exception occurs and is handled by
the catch block
Example:
TestFinallyBlock2.java
Output:
Rule: For each try block there can be zero or more catch
blocks, but only one finally block.
Note: The finally block will not be executed if the
program exits (either by calling System.exit() or by
causing a fatal error that causes the process to abort).
Java throw exception
Java throw keyword
1. throw exception;
Output:
Exception in thread main
java.lang.ArithmeticException:not valid
TestExceptionPropagation1.java
1. class TestExceptionPropagation1{
2. void m(){
3. int data=50/0;
4. }
5. void n(){
6. m();
7. }
8. void p(){
9. try{
10. n();
11. }catch(Exception e){System.out.println("exception
handled");}
12. }
13. public static void main(String args[]){
14. TestExceptionPropagation1 obj=new TestExceptio
nPropagation1();
15. obj.p();
16. System.out.println("normal flow...");
17. }
18. }
Test it Now
Output:
exception handled
normal flow...
TestExceptionPropagation1.java
1. class TestExceptionPropagation2{
2. void m(){
3. throw new java.io.IOException("device error");//check
ed exception
4. }
5. void n(){
6. m();
7. }
8. void p(){
9. try{
10. n();
11. }catch(Exception e){System.out.println("exception
handeled");}
12. }
13. public static void main(String args[]){
14. TestExceptionPropagation2 obj=new TestExceptio
nPropagation2();
15. obj.p();
16. System.out.println("normal flow");
17. }
18. }
Test it Now
Output:
Compile Time Error
1. import java.io.IOException;
2. class Testthrows1{
3. void m()throws IOException{
4. throw new IOException("device error");//checked exc
eption
5. }
6. void n()throws IOException{
7. m();
8. }
9. void p(){
10. try{
11. n();
12. }catch(Exception e){System.out.println("exception
handled");}
13. }
14. public static void main(String args[]){
15. Testthrows1 obj=new Testthrows1();
16. obj.p();
17. System.out.println("normal flow...");
18. }
19. }
Test it Now
Output:
exception handled
normal flow...
1. import java.io.*;
2. class M{
3. void method()throws IOException{
4. throw new IOException("device error");
5. }
6. }
7. public class Testthrows2{
8. public static void main(String args[]){
9. try{
10. M m=new M();
11. m.method();
12. }catch(Exception e){System.out.println("exception
handled");}
13.
14. System.out.println("normal flow...");
15. }
16. }
Test it Now
Output:exception handled
normal flow...
1. import java.io.*;
2. class M{
3. void method()throws IOException{
4. throw new IOException("device error");
5. }
6. }
7. class Testthrows4{
8. public static void main(String args[])throws IOExcepti
on{//declare exception
9. M m=new M();
10. m.method();
11.
12. System.out.println("normal flow...");
13. }
14. }
Test it Now
Output:Runtime Exception
Difference between throw and throws in Java
1. import java.io.*;
2. class Parent{
3. void msg(){System.out.println("parent");}
4. }
5.
6. class TestExceptionChild1 extends Parent{
7. void msg()throws ArithmeticException{
8. System.out.println("child");
9. }
10. public static void main(String args[]){
11. Parent p=new TestExceptionChild1();
12. p.msg();
13. }
14. }
Test it Now
Output:child