Sie sind auf Seite 1von 75

Introduction to Java

Introduction- Data Types


Data Types in Java Integer Types byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point Types float 4 bytes double 8 bytes Character Type char 2 bytes -

[Support for Unicode Character Set].

Introduction-Hello World Program


A Sample Java Program: public class myclass { public static void main( String [] ) { System.out.println(Hello World ) ; } }

main is a static method of the class myclass. Therefore in order to call the method main it is not necessary to instantiate myclass.

Introduction-Parameter Passing
public class myclass { private static int a = 10 ; public static void main( String [] a) { System.out.println( a ) ; } } Guess what is the O/P of the Program ?

Introduction-Parameter Passing
[Ljava.lang.String;@f4a24a public class myclass { private static int a = 10 ; public static void main( String [] a) { System.out.println( a[0] ) ; } }

Here we try to print the value from the arguments array. Since the array a gets the local scope, the private integer variable remains untouched. Let us rename the private integer variable as b.

Introduction-Static Methods
public class myclass { private int b = 10 ; public static void main( String [] a) { System.out.println( b ) ; } } Guess what will be printed now ?

Introduction-Static method & variables


public class myclass { private static int b = 10 ; public static void main( String [] a) { System.out.println( b ) ; } } Hope this solves our problem !!!

Introduction-Static method & variables


public class myclass { public void display() { System.out.println(Hello); } public static void main( String [] a) { display() ; } } Guess what is the o/p ?

Introduction-Static method & variables


The function display is not a static function and hence main cannot call it. This is detected by the java compiler and hence the error ! NOTE : A Static method in a class can access only static data members and static member functions of that class.

Introduction-Static method & variables


public class zzz { void display() { System.out.println(Hi); } } public class myclass { public static void main( String [] a) { zzz b; b.display() ; } } Guess what is the o/p ?

Introduction-Static method & variables


This particular source files contains 2 public classes. Java makes it compulsory that a source file may contain at most one public class and the name of the file is the name of this public class !

Introduction-Static method & variables


class zzz { void display() { System.out.println(Hi); } } public class myclass { public static void main( String [] a) { zzz a; a.display() ; } } Guess what is the o/p ?

Introduction-Static method & variables


Variable a is defined twice ! Error !

Introduction-Static method & variables


class zzz { void display() { System.out.println(Hi); } } public class myclass { public static void main( String [] a) { zzz b; b.display() ; } } Guess what is the o/p ?

Introduction-Static method & variables


Compilation error ! The variable b is not initialized ( constructor not called !) Guess ???

Introduction-Static method & variables


class zzz { void display() { System.out.println(Hi); } } public class myclass { public static void main( String [] a) { zzz b = new zzz(); b.display() ; } } Guess what is the o/p ?

Introduction-Static method & variables


class zzz { public static void Printme() { System.out.println(Hello); } void display() { System.out.println(Hi); }

}
public class myclass { public static void main( String [] a) { zzz b = new zzz(); b.display() ; b.Printme(); } } Guess what is the o/p ?

Introduction-Static method & variables

This program functions perfectly well and prints the following as result Hi Hello

Introduction-Static method & variables


class A { A() { System.out.println ( "In Class A" ) ; } static void C() { System.out.println ( "A Method that is static" ) ; }

} public class MyProgram { public static void main( String[] args) { A.C() ; System.out.println ( "How are you ajay" ) ; } } O/P ?

Introduction-Static method & variables


Explaination : A.C() gets called as expected ( Since C is static it does not require a object to be instantiated)

class A {

void B() { System.out.println ( "A Method named B" ) ; static void C() { System.out.println ( "A Method that is static" ) ; B() ; }

} public class MyProgram { public static void main( String[] args) { A.C() ; System.out.println ( "How are you ajay" ) ; } }

Introduction-Static method & variables


Compilation Error: non-static method B() cannot be referenced from a static context. A static method can call only other static method

class Yy1 { int a , b ; } public class myclass { public static void main(String[] c) { Yy1 a = new Yy1(); System.out.println(a.a) ; } } What is the o/p ?

Introduction-Static method & variables


The o/p of the program is 0. By default the values of respective java primitive types is set to 0,null or false. Note : Every class belongs to a package. If you do not mention the name of the package at the beginning of the class in the file in which it is defined, it is by default considered to be in the default package. If the package statement is present, it should be the first statement.

Introduction-Constructors
class A { A() { System.out.println ( 0" ) ; } } public class MyProgram { public static void main( String[] args) { A a; System.out.println ( 1" ) ; } } Guess this too !

Introduction-Constructors
Explanation : This program will execute. But will not execute the constructor. Since A a; will just declare a variable for the object of type A. This variable is currently empty, as the object of type A needs to be instantiated. Since the object is not instantiated the constructor is not called.

Introduction-Constructors
class A {

A() {
System.out.println ( 0" ) ; } } public class MyProgram { public static void main( String[] args) { A a = new A(); System.out.println ( 1" ) ; } } Here the constructor is called and we receive the output as expected.

Introduction-Constructors
class A { A() { System.out.println ( "In Class A" ) ; } A( int a) { System.out.println ( "Parameterized One" ) ; } } public class MyProgram { public static void main( String[] args) { A a = new A(1); System.out.println ( "How are you" ) ; } } Here the parameterized constructor is called and we receive the output as expected.

Introduction-Constructors
class A { A() { System.out.println ( "In Class A" ) ; } B() { System.out.println ( "A method named B" ) ; } } public class MyProgram { public static void main( String[] args) { A a; a = new A() ; a.B() ; System.out.println ( "How are you sangita" ) ; } }

Introduction-Constructors
Compilation Error : return type required for the method B() class A {

A() { } void B() { }

System.out.println ( "In Class A" ) ;

System.out.println ( "A method named B" ) ;

}
public class MyProgram { public static void main( String[] args) { A a; a = new A() ; a.B() ; System.out.println ( "How are you ajay" ) ; } } Explanation: Problem Solved !

Introduction-Constructors
class Base { Base() { } } System.out.println("Base");

class Derived { Derived() { System.out.println("Derived"); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } } Guess ?

Introduction-Constructors
O/P is Derived !

class Base { Base() { } }

System.out.println("Base");

class Derived extends Base { Derived() { System.out.println("Derived"); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } }

Introduction-Constructors
O/P is Base Derived class Base { Base(int a) { System.out.println("Base"); } } class Derived extends Base { Derived() { System.out.println("Derived"); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } } O/P ?

Introduction-Constructors
Compilation Error ! class Base { Base(int a) { System.out.println("Base"); } } class Derived extends Base { Derived() { System.out.println("Derived"); super(10); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } } O/P ?

Introduction-Constructors
Compilation Error ! Call to super should be first statement in constructor.

class Base { Base(int a) { System.out.println("Base"); } } class Derived extends Base { Derived() { super(10); System.out.println("Derived"); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } } O/P ?

Introduction-Constructor
No Compilation Error ! Every class in Java is derived from a predefined class. Therefore super() in above will call the constructor of that class. Base Derived class A { int x; A(int x) { x=x; } void Display() { System.out.println ( x ) ; } } public class MyProgram { public static void main( String[] args) { A a = new A( 7 ) ; a.Display() ; } } Guess O/P:

Introduction-Constructor
O/P: 0

class A {

int x; A(int x) { this.x = x ; } void Display() { System.out.println ( x ) ; }

} public class myclass { public static void main( String[] args) { A a = new A( 7 ) ; a.Display() ; } } O/P

Introduction-Constructor
O/P: class A { 7
int x; A(int b) { x=b; } void Display() { System.out.println ( x ) ; }

} class B extends A { B( int y ) { x=y; } void Display() { System.out.println ( x ) ; } } public class myclass { public static void main( String[] args) { B a = new B( 7 ) ; a.Display() ; } } o/p: ?

Introduction-Constructor
O/P:

Compilation Error: cannot resolve symbol : constructor A ()

Introduction-Inheritance
class Base extends Object { Base() { System.out.println("Base"); } } class Derived extends Base { Derived() { System.out.println("Derived"); } } public class myclass { public static void main(String[] c) { Derived d = new Derived() ; } } O/P ?

Introduction-Inheritance
No Compilation Error ! The program runs and we get the o/p: Base Derived class MyMaths { int a ; void AssignAndDisplay() { a = 10 ; System.out.println ( a ) ; } } class OtherClass extends MyMaths { void JustDisplay() { AssignAndDisplay() ; System.out.println ( a ) ; } } public class MyProgram { public static void main( String[] args) { OtherClass a = new OtherClass(); a.JustDisplay() ; } } O/P ?

Introduction-Inheritance
No Compilation Error ! The program runs and we get the o/p: 10 10

Explaination : When no access specifier is specified it means the default access specifier friendly.

Introduction-Inheritance
class MyMaths { int a ; void AssignAndDisplay() { a = 10 ; System.out.println ( a ) ; } } class OtherClass extends MyMaths { void JustDisplay() { System.out.println ( a ) ; } } public class myclass { public static void main( String[] args) { OtherClass a = new OtherClass(); a.a = 1000 ; a.JustDisplay() ; a.AssignAndDisplay() ; a.JustDisplay() ; } }

Introduction-Inheritance
O/P: 1000 10 10

Introduction-Inheritance
class A { int x; A(int b) { x=b; } void Display() { System.out.println ( x ) ;

} class B extends A { B( int y ) { super(y) ; x=x+1+y; } void Display() { System.out.println ( x ) ; } public class myclass { public static void main( String[] args) { B a = new B( 7 ) ; a.Display() ; } }

Introduction-Inheritance
O/P: 15 class A {

int x; A( int a ) { x=a; }

} class B extends A { B( int y ) { super(y) ; x = 100 ; super(y) ; } } public class myclass { public static void main( String[] args) { B a = new B( 7 ) ; System.out.println( a.x ) ; } }

Introduction-Inheritance
Compilation Error: call to super must be first statement in constructor class A { int x; A( int a ) { x=a; } } class B extends A { B( int y ) { super(y) ; super(y+1) ; } } public class myclass { public static void main( String[] args) { B a = new B( 7 ) ; System.out.println( a.x ) ; } }

Introduction-Inheritance
Compilation Error: call to super must be first statement in constructor class A { int x; A( int a ) { x=a; } } class B extends A { B( int y ) { super(y) ; } void Display() { super(y+1); } } public class myclass { public static void main( String[] args) { B a = new B( 7 ) ; a.Display(); System.out.println( a.x ) ; } }

Introduction-Inheritance
Compilation Error: call to super must be first statement in constructor

class MyMaths { protected void Dara() { System.out.println ( "Some one called Singh" ) ; } } class OtherClass extends MyMaths { void Dara() { System.out.println ( "A Clone of dara lives near my house" ) ; } void Jackie() { System.out.println ( "Some one called Chan" ) ; } } public class myclass { public static void main( String[] args) { OtherClass a = new OtherClass(); a.Dara() ; a.Jackie() ; } }

Introduction-Inheritance
Dara() in OtherClass cannot override Dara() in MyMaths ; attempting to assign
weaker access priviliges; was protected.

Introduction-Inheritance
class A { int x; A(int x) { this.x = x ; } void Display() { System.out.println ( x ) ; } void finalize() { System.out.println ( "Call to finalize" ) ; }

} public class myclass { public static void main( String[] args) { A a = new A( 7 ) ; a.Display() ; a = new A(10) ; a.Display() ; a = new A(20) ; } }

O/P: ?

Introduction-Inheritance
finalize( ) in A cannot override finalize() in java.lang.Object; attempting to assign weaker access priviliges; was protected. class A { int x; A(int x) { this.x = x ; } void Display() { System.out.println ( x ) ; } protected void finalize() { System.out.println ( "Call to finalize" ) ; } } public class myclass { public static void main( String[] args) { A a = new A( 7 ) ; a.Display() ; a = new A(10) ; a.Display() ; a = new A(20) ; } } O/P : ?

Introduction-Inheritance
O/P: 7 10

The finalize method is not called ? Why !!!


Discuss

Introduction-Inheritance
class A { int x; A(int x) { this.x = x ; } void Display() { System.out.println ( x ) ; } protected void finalize() { System.out.println ( "Call to finalize : " + x ) ; }

} public class myclass { public static void main( String[] args) { A a = new A( 7 ) ; a.Display() ; a = new A(10) ; a.Display() ; a = new A(20) ; System.gc() ; } }

Introduction-Inheritance
7 10 Call to Finalize : 7 Call to Finalize : 10

Introduction-Inheritance
final class MyMaths { void Foo1() { System.out.println ( "In Foo" ) ; } } class OtherClass extends MyMaths { void Foo2() { System.out.println ( "In Foo2" ) ; } } public class myclass { public static void main( String[] args) { OtherClass a = new OtherClass(); a.Foo2() ; } }

Introduction-Inheritance
Compilation Error: cannot inherit from final MyMaths Explanation : A final class cannot be inherited !

Introduction-Inheritance
interface A { int a=10 ; void DisplayA(); } interface B { final int b ; void DisplayB(); } class AB implements A,B { public void DisplayA() { System.out.println( a ) ; } public void DisplayB() { System.out.println( b ) ; } }

Introduction-Inheritance
public class myclass { public static void main( String[] args) { AB x = new AB() ; x.DisplayA(); x.DisplayB(); System.out.println( "Finished" ) ; } }

Introduction-Inheritance
Compilation Error ! A variable defined in an interface by default is final and it is necessary to initialize it with a constant value. Also since the variable is final (constant), it cannot be changed.

Introduction-Inheritance
interface A { void DisplayA(); } interface B extends A { void DisplayB(); } class AB implements B { public void DisplayA() { System.out.println( "a" ) ; } public void DisplayB() { System.out.println( "b" ) ; } }

Introduction-Inheritance
public class myclass { public static void main( String[] args) { AB x = new AB() ; x.DisplayA(); x.DisplayB(); } } O/P : interface inheritance is allowed !

Introduction-Exception Handling
public class myclass { public static void main( String[] args) { int a,b; try { b=0; a = 42 / b ; } catch( ArithmeticException e) { System.out.println(e.getMessage()) ; } System.out.println(Will this get printed); }

} o/p : ?

Introduction-Exception Handling
O/P is

/ by zero Will this get printed


public class myclass { public static void main( String[] args) { int a,b; try { b=0; a = 42 / b ; } catch( ArithmeticException e) { System.out.println(e) ; } } }

O/P :

Introduction-Exception Handling
O/P is

java.lang.ArithmeticException: / by zero public class myclass { public static void main( String[] args) { int a,b; b=0; a = 42 / b ; System.out.println("Hello World" ) ; } } O/P:

Introduction-Exception Handling
O/P is

Exception in thread main java.lang.ArithmeticException: / by zero At myclass.main(myclass.java:6) The program exits as soon as the exception is encountered. The statements that follow are not executed at all.

Introduction-Exception Handling
O/P is

/ by zero Will this get printed


public class myclass { public static void main( String[] args) { int a,b; try { b=0; a = 42 / b ; System.out.println("Musharraf" ) ; } catch( ArithmeticException e) { System.out.println(e.getMessage()) ; } System.out.println("Man Mohan" ) ; } } O/P :

Introduction-Exception Handling
O/P is

/ by zero
Man Mohan

public class myclass { public static void main( String[] args) { int a,b; try { b=0; a = 42 / b ; System.out.println("Musharraf" ) ; } catch( Exception e) { System.out.println(e.getMessage()) ; } System.out.println("Man Mohan" ) ; } }

Introduction-Exception Handling
O/P is

/ by zero
Man Mohan public class myclass { public static void main( String[] args) { int a,b; int c[] = { 1, 2 } ; try { b = args.length ; a = 42 / b ; c[2] = a ; } catch( ArithmeticException e) { System.out.println(e.getMessage()) ; } catch( ArrayIndexOutOfBoundsException e) { System.out.println(e) ; } } } O/P : ?

Introduction-Exception Handling
Discuss !!
public class myclass { public static void main( String[] args) { try { throw new NullPointerException("My Exception"); } catch( NullPointerException ne) { System.out.println(ne) ; } } }

Introduction-Exception Handling
O/P: java.lang.NullPointerException: My Exception

class A { public void MyMethod() { throw new NullPointerException("My Exception"); } } public class myclass { public static void main( String[] args) { A a = new A(); a.MyMethod(); } }

Introduction-Exception Handling
O/P: java.lang.NullPointerException: My Exception class A {

public void MyMethod() throws Throwable { throw new NullPointerException("My Exception"); }


} public class myclass { public static void main( String[] args) { A a = new A(); a.MyMethod();

}
}

Introduction-Exception Handling
O/P : Throws an Error
public class myclass { static void MyMethod() { throw new Exception("My Exception"); }
public static void main( String[] args) { MyMethod(); }

Introduction-Exception Handling
O/P : Complie Time Error public class myclass { static void MyMethod() throws Exception { throw new Exception("My Exception"); }

public static void main( String[] args) {


try {

MyMethod(); }catch(Exception e){ }


} } Discussion !!!!!

Introduction-Thread
class myclass { public static void main(String[] args) { Thread t = new Thread.currentThread(); System.out.println("Current thread:" + t); t.setName("My Thread"); System.out.println("After Name Change: "+ t); try { for (int n =5; n > 0; n--) System.out.println(n); Thread.sleep(1000); } catch(InterruptedException e) {System.out.println("Main Thread Interrupted");} }}

Introduction- Thread
class NewThread implements Runnable { Thread t; NewThread() { t = new Thread(this , "Demo Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run() { try{ for(int i=5;i>0;i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } }catch(InterruptedException e) { System.out.println("Child interuppted"); } System.out.println("Exiting child thread"); }}

Introduction- Thread
class myclass { public static void main(String[] args) {new NewThread(); 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"); } }

Das könnte Ihnen auch gefallen