Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.

Similar presentations


Presentation on theme: "WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION."— Presentation transcript:

1 WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION

2 WEEK 2 Reference Type Arrays ArrayList Methods Pass by Value (Primitives and References)

3 Reference Type Arrays Reference type arrays are reference types of collection of reference types. String[] sArray = new String[4]; (1)(2) sArray[0]=new String(“Kemal”);//alias to sArray[0]=“Kemal”; //sArray[1],sArray[2],sArray[3]  null

4 sArray[0]=new String(“Kemal”);

5 Reference Type Arrays String s=“CSE252”; String[] sArray=new String[4]; sArray[0]=“Kemal”;(1) sArray[3]=s;(2) ??

6

7 String[] sArray = new String[4]; (1)(2)

8 Arrays(General) Array is a kind of class in Java API. The most useful variable: length: the size of an array.

9 Methods Functions in java. (1) Instance Methods: An instance method is a method which is associated with one object and uses the variables of that object.methodvariables int i= scannerObject.nextInt(); // nextInt is instance method. (1) Static Methods: The method which is associated with a class, not an object. System.out.println(“Static Method”);

10 Methods The method for returning a variable and passing parameters are same at a instance method and a class method. Here we will look at static methods because main method can only call static methods. The calling principle of a method at java is pass-by-value.

11 Example(1) public class FunctionExample { public static void main(String[] args){ int a=max(4,15); System.out.println(a); } static int max(int a,int b){ return a>b?a:b; }

12 Example(2) public class FunctionExample { public static void main(String[] args){ write(“CSE 252”); write(generateNum()); int num=generateNum(); System.out.println(num); } static void write(String s){ System.out.println(s); } static int generateNum(){ return 5; }

13 Example(3): public class FunctionExample { public static void main(String[] args){ String s=“Kemal”; write(s); } static void write(String s){ System.out.println(s); } static int generateNum(){ return 5; }

14 Pass-by-value (In primitive types) Example (4): public class FunctionExample { public static void main(String[] args){ int a=90; int b=80; swap(a,b); // not swapped !!! System.out.println(a + “ “ + b); } static void swap(int a,int b){ int tmp=a; a=b; b=tmp; }

15 Pass-by-value (Reference Types) public class FunctionExample { public static void main(String[] args){ int[] intArray=createArray(); System.out.println(intArray.length); // here intArray.length is 5 System.out.println(intArray[0]); // here intArray[0] is 9 } static int[] createArray(){ int[] arrayInMethod =new int[5]; arrayInMethod[0]=9; return arrayInMethod; }

16 Exceptions Exceptions -> runtime errors. (1) Exceptions cannot be catched at compilation time (2) It can occur when the program is running. (3) Normal flow of instructions can be distrupted with an exception. (4) When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception. An example of such a violation is an attempt to index outside the bounds of an array.

17 Exceptions Example: int a=scannerObj.nextInt(); this method will wait for user entrance from keyboard and it also accept user to enter a number literal (3,-2,4,5009,-80,90.3). When user enters an non number literal like 3AA4,Kemal,CSE252,443AA, a violation for semantics occurs and an exception will be thrown.

18 Handling Exceptions Exceptions must be handled. If an occurred exception is not handled by the program, the execution of program will be terminated with an exception message. This statement can cause fatal situations when executing I/O systems or low- level systems. Exceptions will be handled with try catch statements.

19 Handling Exceptions Example: int i; try{ i=scannerObject.nextInt(); }catch(Exception e){ System.out.println(“You must enter a number”); return; } System.out.println(i);


Download ppt "WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION."

Similar presentations


Ads by Google