190 likes | 295 Views
CSE 252 Principles of Programming Languages LAB SECTION. WEEK 2 Introduction to Java II. WEEK 2. Reference Type Arrays ArrayList Methods Pass by Value ( Primitives and References ). Reference Type Arrays.
E N D
CSE 252 Principles of ProgrammingLanguagesLAB SECTION WEEK 2 Introductionto Java II
WEEK 2 • ReferenceTypeArrays • ArrayList • Methods • PassbyValue (PrimitivesandReferences)
ReferenceTypeArrays Referencetypearraysarereferencetypes of collection of referencetypes. String[] sArray = newString[4]; (1)(2) sArray[0]=newString(“Kemal”);//aliastosArray[0]=“Kemal”; //sArray[1],sArray[2],sArray[3] null
ReferenceTypeArrays String s=“CSE252”; String[] sArray=newString[4]; sArray[0]=“Kemal”;(1) sArray[3]=s;(2) ??
Arrays(General) Array is a kind of class in Java API. Themostusefulvariable: length: the size of an array.
Methods Functions in java. • InstanceMethods: An instance method is a method which is associated with one object and uses the variables of that object. int i=scannerObject.nextInt(); // nextInt is instancemethod. • StaticMethods: Themethodwhich is associatedwith a class, not an object. System.out.println(“StaticMethod”);
Methods Themethodforreturning a variableandpassingparametersaresame at a instancemethodand a classmethod. Herewewilllook at staticmethodsbecausemainmethod can onlycallstaticmethods. Thecallingprinciple of a method at java is pass-by-value.
Example(1) publicclassFunctionExample { publicstaticvoidmain(String[] args){ int a=max(4,15); System.out.println(a); } staticintmax(int a,int b){ return a>b?a:b; } }
Example(2) publicclassFunctionExample { publicstaticvoidmain(String[] args){ write(“CSE 252”); write(generateNum()); intnum=generateNum(); System.out.println(num); } staticvoidwrite(String s){ System.out.println(s); } staticintgenerateNum(){ return 5; } }
Example(3): publicclassFunctionExample { publicstaticvoidmain(String[] args){ String s=“Kemal”; write(s); } staticvoidwrite(String s){ System.out.println(s); } staticintgenerateNum(){ return 5; } }
Pass-by-value (Inprimitivetypes) Example (4): publicclassFunctionExample { publicstaticvoidmain(String[] args){ int a=90; int b=80; swap(a,b); // not swapped !!! System.out.println(a + “ “ + b); } staticvoid swap(int a,int b){ inttmp=a; a=b; b=tmp; } }
Pass-by-value (ReferenceTypes) publicclassFunctionExample { publicstaticvoidmain(String[] args){ int[] intArray=createArray(); System.out.println(intArray.length); // hereintArray.length is 5 System.out.println(intArray[0]); // hereintArray[0] is 9 } staticint[] createArray(){ int[] arrayInMethod =newint[5]; arrayInMethod[0]=9; returnarrayInMethod; } }
Exceptions Exceptions -> runtimeerrors. • Exceptionscannot be catched at compilation time • It can occurwhenthe program is running. • Normal flow of instructions can be distruptedwith an exception. • 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.
Exceptions Example: int a=scannerObj.nextInt(); thismethodwillwaitforuserentrancefromkeyboardand it alsoacceptusertoenter a numberliteral (3,-2,4,5009,-80,90.3). Whenuserenters an nonnumberliterallike 3AA4,Kemal,CSE252,443AA, a violationforsemanticsoccursand an exceptionwill be thrown.
HandlingExceptions Exceptionsmust be handled. If an occurredexception is not handledbythe program, theexecution of program will be terminatedwith an exceptionmessage. Thisstatement can causefatalsituationswhenexecuting I/O systemsorlow-levelsystems. Exceptionswill be handledwithtrycatchstatements.
HandlingExceptions Example: int i; try{ i=scannerObject.nextInt(); }catch(Exception e){ System.out.println(“Youmustenter a number”); return; } System.out.println(i);