Presentation is loading. Please wait.

Presentation is loading. Please wait.

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.

Similar presentations


Presentation on theme: "Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java."— Presentation transcript:

1 Core Java Lecture 4-5

2 What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java Shift Operators (>>, >>) Use of final keyword in Java

3 Methods Fulfills the operational responsibilities of the class. Handles Operation part of Class Partial General Syntax [static][final] method-name(,…. ) { ……………………………. …………………………….. } BODY OF METHOD  public, protected, private or default package private

4 Method Components Method-Name Return Type Signatures[Type & Number of Parameters] Scope Examples: 1.public void display()  Signature: (), Return Type: void, Scope : public 2.private int show()  Signature: (), Return Type: int, Scope : private 3.intsum(int x, int y)  Signature: (int,int), Return Type: int, Scope : default 4.float computeSum(double a, char b, int c)  Signature : (double,char, int) Return Type : float, Scope default

5 Method Example 1 class Test { public static double sum(double a, double b) { return (a+b); }// End of sum public static void main(String x[]) { double d = sum(10.6,4.5); System.out.println(d); System.out.println(sum(20,10)); }// End of main }// End of Test Class Call to sum Method from main() Method

6 Method OverLoading Two Methods in the Same Class are said to be overloaded if and only if they have same name BUT Different Signature Overloaded Methods may have same return type or different return type Overloaded Methods may have same return scope or different scope

7 Method Overloading Examples 1.voidsum(int a, int b) 2.intsum(float a, float b) 3.floatsum(double a, double b) 4.floatsum(int x, float y) 5.voidsum(float a, int b) SIGNATURES (int,int) (float,float) (int,float) (float,int) (double,double)

8 class Test { public static double sum( double a, double b) { System.out.println("Sum: Double Double Called"); return (a+b); }// End of sum public static int sum( int a, int b) { System.out.println("Sum: int int Called"); return (a+b); }// End of sum public static void main(String x[]) { System.out.println(sum(20,10)); System.out.println(sum(20.5,10.7)); System.out.println(sum(20.5f,10)); System.out.println(sum(20.0,10)); byte b = 10; short s = 8; System.out.println(sum(b,s)); }// End of main }// End of class Test Sum: int int Called 30 Sum: Double Double Called 31.2 Sum: Double Double Called 30.5 Sum: Double Double Called 30.0 Sum: int int Called 18

9 class Test { public static double sum( double a, float b) { System.out.println("Sum: Double Float Called"); return (a+b); }// End of sum public static double sum( float a, double b) { System.out.println("Sum: Float Double Called"); return (a+b); }// End of sum public static void main(String x[]) { System.out.println(sum(20.5,10.5f)); System.out.println(sum(20.5f,10.7)); System.out.println(sum(20.0,10)); }// End of main }// End of class Test Sum: Double Float Called 31.0 Sum: Float Double Called 31.2 Sum: Double Float Called 30.0

10 class Test { public static double sum( double a, float b) { System.out.println("Sum: Double Float Called"); return (a+b); }// End of sum public static double sum( float a, double b) { System.out.println("Sum: Float Double Called"); return (a+b); }// End of sum public static void main(String x[]) { System.out.println(sum(5.6f,4.5f)); System.out.println(sum(5.6,4.5)); }// End of main }// End of class Test Test.java:15: error: reference to sum is ambiguous, both method sum(double,floa ) in Test and method sum(float,double) in Test match System.out.println(sum(20.5f,10.7f)); ^ Test.java:16: error: no suitable method found for sum(double,double) System.out.println(sum(20.0,10.5)); ^ method Test.sum(float,double) is not applicable (actual argument double cannot be converted to float by method invocation conversion) method Test.sum(double,float) is not applicable (actual argument double cannot be converted to float by method invocation conversion) 2 errors

11 Scope and Life Time of Variables 1.Block scope 2.Method Scope // Test.java class Test { intx; public static void main(String args[]) { int y = 0; { int y1 =10; System.out.println(y); System.out.println(y1); } }// End of main() Method }// End of class block {}

12 Example 1 Method Variables and Block Variables class Test { intx; public static void main(String args[]) { int y = 0; // BLOCK 1 { int y1 =10; System.out.println(y); System.out.println(y1); } // BLOCK 2 { int y1 =100; System.out.println(y); System.out.println(y1); } }// End of main() Method }// End of class

13 Example 2 Method Variables and Block Variables class Test { intx; public static void main(String args[]) { int y1 = 0; // BLOCK 1 { int y1 =10; System.out.println(y); System.out.println(y1); } // BLOCK 2 { int y1 =100; System.out.println(y); System.out.println(y1); } }// End of main() Method }// End of class Test.java:8: error: y1 is already defined in main(String[]) int y1 =10; Test.java:9: error: cannot find symbol System.out.println(y); symbol: variable y location: class Test Test.java:13: error: y1 is already defined in main(String[]) int y1 =100; Test.java:14: error: cannot find symbol System.out.println(y); symbol: variable y location: class Test 4 errors

14 Example 3 Method Variables and Block Variables class Test { intx; public static void main(String args[]) { int y = 0; // BLOCK 1 { int y1 =10; System.out.println(y); System.out.println(y1); }// End of Block System.out.println(y1); }// End of main() Method }// End of class C:\Program Files\Java\jdk1.7.0\bin>javac Test.java Test.java:12: error: cannot find symbol System.out.println(y1); ^ symbol: variable y1 location: class Test 1 error

15 Command Line Arguments General Form of main public static void main(String[] args) String array[] is used to store command line arguments that are passed when program is being executed Assume Name of Source Java file is Test.java and name of Driver class is Driver java Driver  No command line arguments. args.length = 0

16 Command Line Arguments cont… If Execution is as follows: java Driver Hello I am fine args.length = 6 [Number of Arguments Passed] args[0] = “Hello” args[1] = “I”; args[2] = “am” ….. and so on

17 Converts String to int if String contains an integer in String form Exercise Print prime numbers from the list of numbers passed as command Line arguments class commandPrime { public static void main(String x[ ]) { for(int i=0; i

18 Tutorial Q1…… Running of CommandPrime D:\java\bin>java commandPrime D:\java\bin>java commandPrime x y Exception in thread "main" java.lang.NumberFormatException: For input string: "x “ at java.lang.NumberFormatException. forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at commandPrime.main(prime.java:9) D:\java\bin>java commandPrime x y 13 Exception in thread "main" java.lang.NumberFormatException: For input string: "x"at java.lang.NumberFormatException.forInputString (NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at commandPrime.main(prime.java:9)

19 There are Four different uses of static keyword in java. 1.static instance variables 2.static methods 3.static classes 4.static blocks Use of static keyword in Java Note : static field/methods of a class can be accessed/referenced even without creating the objects of that class [ Provided They are not static]. Syntax :. OR.

20 static instance variables/fields Static field/instance variables are allocated space in the heap area. Static field is just like a global variable for a class that is allocated memory once and all objects of that class share that common copy. For a static fields of any class, all the objects of that class share a common copy. Declaring an instance field as static makes it class variable that belongs to a whole class not an individual object. Any Field/Attribute/instance field of a class can be declared as static.

21 Example (Static Fields) class circle { static double PI= ; double radius; double area() { return PI * radius * radius; } double perimeter() { return 2*PI*radius; } } // End of circle class Static Member Non-static instance field circle c1 = new circle(); circle c2 = new circle(); c1c2 radius PI = Memory Map

22 Example (Static Fields) class A { static int a = 10; double b, c; ………. } A a1 = new A(); A a2 = new A(); Memory Map a1 a2 bc bc a=10

23 Static Methods static methods can use only static data static methods can be called/accessed/referenced even without creating the objects that class. Syntax. static method can not call non static methods. Examples: Math.sqrt (all math functions) Static method can declare local variables but from outside it can access only static data(fields/methods)

24 class num { int a,b,c; static int d = 10; static double e = 20.56; num(int a,int b,int c) { this.a = a; this.b =b; this.c =c; } static int sum(int a1, int b1) { // System.out.println(“a=”+a+”b=”+b+”c=”+c); System.out.println(“d=”+d+”e=”+e); return 40; } static Method Example a,b,c are non static fields and can not be accessed from a static method non static instance fields static instance fields static method

25 static double sum(double a, double b) { System.out.println(“d=”+d+”e=”+e); return 40.56; } static void pr() { System.out.println(“This is method pr”); } void print() { System.out.println(“This is method print”); pr(); //  call to static method from a non static method ----  Vaild System.out.println(“a=”+a+”b=”+b+”c=”+c); System.out.println(“d=”+d+”e=”+e); }

26 class BOX { private double l,b,h;// Instance Fields BOX(double a,double b,double c) { l=a;this.b=b;h=c;}// Constructor boolean isEqual(BOX other) { if (this.l == other.l && this.b == other.b && this.h == other.h) return true; else return false; } static boolean isEqual(BOX b1, BOX b2) { if (b1.l == b2.l && b1.b == b2.b && b1.h == b2.h) return true; else return false; } } // End of BOX class

27 class statictest { public static void main(String args[]) { BOX b1 = new BOX(10,6,8); BOX b2 = new BOX(10,6,8); BOX b3 = new BOX(1,16,18); BOX b4 = new BOX(2,6,8); System.out.println(b1.isEqual(b2)); System.out.println(BOX.isEqual(b1,b2)); System.out.println(b3.isEqual(b1,b2)); System.out.println(b4.isEqual(b2)); System.out.println(b4.isEqual(b4,b2)); } } D:\Java1>java statictest true false

28 Explain How You view the Following class // Test.java class A { privateinta,b,c; private static intd=10; public void show()// An Instance Method { System.out.println("a="+a+"b="+b+"c="+c+"d="+d); System.out.println("a="+this.a+"b="+this.b+"c="+this.c+"d="+A.d); //System.out.println("a="+this.a+"b="+this.b+"c="+this.c+"d="+this.d); display();//A.display(); Call to a static method from non-static method }// End of show() Method public static void display() { //System.out.println("a="+a+"b="+b+"c="+c+"d="+d); A a1 = new A(); System.out.println("a="+a1.a+"b="+a1.b+"c="+a1.c+"d="+a1.d); }// End of display() Method }// End of class A

29 class Test { public static void main(String args[]) { // What features of class A can be accessed here and How? A a1 = new A(); A a2 = new A(); // System.out.println(a1.a); a1.show(); A.display(); a1.display(); }// End of main() Method }// End of class Test

30 Use >>, >> Operators >> Right Shift Operator [Preserve Sign Bit] << Left Shift [ Left Shift via SignBit] >>> Unsigned Right Shift [ Fill 0 to Sign bit for each move]

31 Right Shift [>>] Each Right Shift will divide the number by 2 Preserve the Sign Bit Syntax : num >> k ; Where k is number of positions shifted. k = k % 32 ( if num is int, char, short or byte type) K = k % 64 (if num is long type)

32 Left Shift [<<] Each Left Shift will multiply the number by 2 May Change the Sign bit [ If Sign bit occurs then overflow has occurred] Syntax : num << k ; Where k is number of positions shifted. k = k % 32 ( if num is int, char, short or byte type) K = k % 64 (if num is long type)

33 Unsigned Right Shift [>>>] Each Right Shift will fill 0 in the sign bit and then number is shifted along with sign bit. If number is +ive then both >> & >>> will work the same way Negative numbers sign will change and also its value after single right shift. For int type after 32 shifting 0 will be filled in all places. Syntax : num >>> k ; Where k is number of positions shifted. k = k % 32 ( if num is int, char, short or byte type) K = k % 64 (if num is long type)


Download ppt "Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java."

Similar presentations


Ads by Google