CSE 252 Principles of Programming Languages LAB SECTION

Download Report

Transcript CSE 252 Principles of Programming Languages LAB SECTION

CSE 252 Principles of Programming
Languages
LAB SECTION
WEEK 2
Introduction to Java II
WEEK 2
 ReferenceType Arrays
 ArrayList
 Methods
 Pass by Value (Primitives and References)
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
sArray[0]=new String(“Kemal”);
Reference Type Arrays
String s=“CSE252”;
String[] sArray=new String[4];
sArray[0]=“Kemal”;(1)
sArray[3]=s;(2)
??
String[] sArray = new String[4]; (1)(2)
Arrays(General)
Array is a kind of class in Java API.
The most useful variable: length: the size of an array.
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.
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”);
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.
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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.
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.
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 lowlevel systems.
Exceptions will be handled with try catch statements.
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);