SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Inheritance
class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class B extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}

string
import java.util.Scanner;

class Compare_Strings
{
   public static void main(String args[])
   {
      String s1, s2;
      Scanner in = new Scanner(System.in);

         System.out.println("Enter the first string");
         s1 = in.nextLine();

         System.out.println("Enter the second string");
         s2 = in.nextLine();

         if ( s1.compareTo(s2) > 0 )
            System.out.println("First string is greater than second.");
         else if ( s1.compareTo(s2) < 0 )
            System.out.println("First string is smaller than second.");
         else
            System.out.println("Both strings are equal.");
    }
}

Stringbuffer
public class StringBufferDemo {


            public static void main(String[] args) {
                       //           Examples of Creation of Strings
                       StringBuffer strBuf1 = new StringBuffer("Bob");
                       StringBuffer strBuf2 = new StringBuffer(100); //With capacity 100
                       StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16
                       System.out.println("strBuf1 : " + strBuf1);
                       System.out.println("strBuf2 capacity : " + strBuf2.capacity());
                       System.out.println("strBuf3 capacity : " + strBuf3.capacity());
            }
}
Try catch
class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero
error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}

thread
// Create a new thread.
class NewThread implements Runnable {
   Thread t;
   NewThread() {
      // Create a new, second thread
      t = new Thread(this, "Demo Thread");
      System.out.println("Child thread: " + t);
      t.start(); // Start the thread
   }

    // This is the entry point for the second thread.
    public void run() {
        try {
           for(int i = 5; i > 0; i--) {
              System.out.println("Child Thread: " + i);
              // Let the thread sleep for a while.
              Thread.sleep(500);
           }
      } catch (InterruptedException e) {
           System.out.println("Child interrupted.");
      }
      System.out.println("Exiting child thread.");
    }
}

class ThreadDemo {
   public static void main(String args[]) {
      new NewThread(); // create a new thread
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Main Thread: " + i);
            Thread.sleep(1000);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}


Reading and writing file
class FileStreamsReadnWrite {
       public static void main(String[] args) {
              try {
                     File stockInputFile = new File("C://stock/stockIn.txt");
                     File StockOutputFile = new File("C://stock/StockOut.txt");

                             /*
                              * Constructor of FileInputStream throws FileNotFoundException if
                              * the argument File does not exist.
                              */

                             FileInputStream fis = new FileInputStream(stockInputFile);
                             FileOutputStream fos = new FileOutputStream(StockOutputFile);
                             int count;

                          while ((count = fis.read()) != -1) {
                                fos.write(count);
                          }
                          fis.close();
                          fos.close();
                   } catch (FileNotFoundException e) {
                          System.err.println("FileStreamsReadnWrite: " + e);
                   } catch (IOException e) {
                          System.err.println("FileStreamsReadnWrite: " + e);
                   }
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

First Steps. (db4o - Object Oriented Database)
First Steps. (db4o - Object Oriented Database)First Steps. (db4o - Object Oriented Database)
First Steps. (db4o - Object Oriented Database)
Wildan Maulana
 

Was ist angesagt? (20)

Linux intro 5 extra: makefiles
Linux intro 5 extra: makefilesLinux intro 5 extra: makefiles
Linux intro 5 extra: makefiles
 
Storm introduction
Storm introductionStorm introduction
Storm introduction
 
working with files
working with filesworking with files
working with files
 
JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196
 
The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
 
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202
 
biopython, doctest and makefiles
biopython, doctest and makefilesbiopython, doctest and makefiles
biopython, doctest and makefiles
 
Posfix
PosfixPosfix
Posfix
 
Penetration testing using python
Penetration testing using pythonPenetration testing using python
Penetration testing using python
 
Looking Ahead to Tcl 8.6
Looking Ahead to Tcl 8.6Looking Ahead to Tcl 8.6
Looking Ahead to Tcl 8.6
 
First Steps. (db4o - Object Oriented Database)
First Steps. (db4o - Object Oriented Database)First Steps. (db4o - Object Oriented Database)
First Steps. (db4o - Object Oriented Database)
 
The Ring programming language version 1.10 book - Part 34 of 212
The Ring programming language version 1.10 book - Part 34 of 212The Ring programming language version 1.10 book - Part 34 of 212
The Ring programming language version 1.10 book - Part 34 of 212
 
The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184
 
The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202
 
The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88
 
java copy file program
java copy file programjava copy file program
java copy file program
 
NIO.2, the I/O API for the future
NIO.2, the I/O API for the futureNIO.2, the I/O API for the future
NIO.2, the I/O API for the future
 
#5 (Remote Method Invocation)
#5 (Remote Method Invocation)#5 (Remote Method Invocation)
#5 (Remote Method Invocation)
 

Andere mochten auch (6)

Chapter 28 presentation the affluent society 1
Chapter 28 presentation  the affluent society 1Chapter 28 presentation  the affluent society 1
Chapter 28 presentation the affluent society 1
 
Prototype in afghanistan-Hospital Build Apr 2013
Prototype in afghanistan-Hospital Build Apr 2013Prototype in afghanistan-Hospital Build Apr 2013
Prototype in afghanistan-Hospital Build Apr 2013
 
Psicología Dinámica
Psicología DinámicaPsicología Dinámica
Psicología Dinámica
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Sampieri ética de la investigación
Sampieri ética de la investigaciónSampieri ética de la investigación
Sampieri ética de la investigación
 
S neier asn10april2013ss
S neier asn10april2013ssS neier asn10april2013ss
S neier asn10april2013ss
 

Ähnlich wie Inheritance

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdfDisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
sudheerforce
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
shahidqamar17
 

Ähnlich wie Inheritance (20)

Lab4
Lab4Lab4
Lab4
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
Code red SUM
Code red SUMCode red SUM
Code red SUM
 
Bhaloo
BhalooBhaloo
Bhaloo
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Java practical
Java practicalJava practical
Java practical
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе
 
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdfDisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
Java 104
Java 104Java 104
Java 104
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
 

Kürzlich hochgeladen

怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
yynod
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Menggugurkan Kandungan 087776558899
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
gynedubai
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
ZurliaSoop
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
gajnagarg
 
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
HyderabadDolls
 
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
gajnagarg
 
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 

Kürzlich hochgeladen (20)

怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
 
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime MalegaonVip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
 
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime MysoreMysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
 
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
Jual obat aborsi Dubai ( 085657271886 ) Cytote pil telat bulan penggugur kand...
 
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
 
Guide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWNGuide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWN
 
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
 
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
 
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
 
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In bhubaneswar [ 7014168258 ] Call Me For Genuine Mode...
 
UIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 UpdateUIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 Update
 
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
 
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Sagar [ 7014168258 ] Call Me For Genuine Models We ...
 
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
 
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
 

Inheritance

  • 1. Inheritance class A { int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System.out.println(x); } } class B extends A{ public static void main(String args[]){ A a = new A(); a.get(5,6); a.Show(); } void display(){ System.out.println("B"); } } string import java.util.Scanner; class Compare_Strings { public static void main(String args[]) { String s1, s2; Scanner in = new Scanner(System.in); System.out.println("Enter the first string"); s1 = in.nextLine(); System.out.println("Enter the second string"); s2 = in.nextLine(); if ( s1.compareTo(s2) > 0 ) System.out.println("First string is greater than second."); else if ( s1.compareTo(s2) < 0 ) System.out.println("First string is smaller than second."); else System.out.println("Both strings are equal."); } } Stringbuffer public class StringBufferDemo { public static void main(String[] args) { // Examples of Creation of Strings StringBuffer strBuf1 = new StringBuffer("Bob"); StringBuffer strBuf2 = new StringBuffer(100); //With capacity 100 StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16 System.out.println("strBuf1 : " + strBuf1); System.out.println("strBuf2 capacity : " + strBuf2.capacity()); System.out.println("strBuf3 capacity : " + strBuf3.capacity()); } } Try catch class Exc2 { public static void main(String args[]) { int d, a; try { // monitor a block of code. d = 0;
  • 2. a = 42 / d; System.out.println("This will not be printed."); } catch (ArithmeticException e) { // catch divide-by-zero error System.out.println("Division by zero."); } System.out.println("After catch statement."); } } thread // Create a new thread. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); // Let the thread sleep for a while. Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } } Reading and writing file class FileStreamsReadnWrite { public static void main(String[] args) { try { File stockInputFile = new File("C://stock/stockIn.txt"); File StockOutputFile = new File("C://stock/StockOut.txt"); /* * Constructor of FileInputStream throws FileNotFoundException if * the argument File does not exist. */ FileInputStream fis = new FileInputStream(stockInputFile); FileOutputStream fos = new FileOutputStream(StockOutputFile); int count; while ((count = fis.read()) != -1) { fos.write(count); } fis.close(); fos.close(); } catch (FileNotFoundException e) { System.err.println("FileStreamsReadnWrite: " + e); } catch (IOException e) { System.err.println("FileStreamsReadnWrite: " + e); }
  • 3. } }