CSE 14 Lecture Notes - Lecture 10: 5,6,7,8
CMPS12A Lecture 10 – Overload and Arrays
When java compiles a program, it is smart to know the type of function that it should return or
print out. Say that you make main() and also make two functions that do the same thing, take
the sum of the two values in the parameter. Both functions, however, return a different type,
one of integer type and the other of double type because of what their parameters are defined.
Overload is basically having functions that, when you call on them, they can have similar types
or not of that type in the parameters.
Example:
public static void main(String[] args) {
System.out.println(sum(1, 3)); //prints out 4
System.out.println(sum(1.0, 3.0)); //prints out 4.0
}
static int sum(int x, int y) {
return (int) (x + y);
}
static double sum(double x, double y) {
return x + y;
}
There is another example of making a function using the same concept, except what if you try
to use ariales alread defied for the paraeter i ai ut the do’t ath a of the
functions made.
Example:
public static void main(String[] args) {
int i = 4, j = 9;
System.out.println(sum(4, 5.0));
System.out.println(sum(5.0, 4));
System.out.println(sum(i, j));
}
find more resources at oneclass.com
find more resources at oneclass.com
static int sum(int x, double y) {
return (int) (x + y);
}
static double sum(double x, int y) {
return x + y;
}
OUTPUT: 9
9.0
///////////////////////////////
So as you can see, whatever types are placed in the parameter of the function in the right
order, those functions will be performed. Notice how in the first printed value, it is first 4, which
is of int type, and then 5.0, of double type. That exact order is what java is looking for in the
functions when it runs. Notice how the last printed value, i and j are integer types but there is
no function of that parameter. You will get an error because of this. Thus, to fix it, you would
need to change its type with the (int) or (double) method by placing either one in front of the
variable, but to match the functions types.
Chapter 5 : Arrays
Arrays in java are a set of memory locations all stored in the same type of data. What this
means is that you can store, for example, 10 items in the single variable of type array, and you
will always be able to access all those items using its indices.
Example:
“trig [] = {, , , , , , , , , , };
int [] y = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
What this is telling us programmers is that the variable x only stores values of type String in the
array, while y only stores values of type integer in the array. In order to access their memory, as
stated earlier, you will need the indices to see each space of memory in the array.
Example:
“ste.out.pritl[] + + [];
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
When java compiles a program, it is smart to know the type of function that it should return or print out. Say that you make main() and also make two functions that do the same thing, take the sum of the two values in the parameter. Both functions, however, return a different type, one of integer type and the other of double type because of what their parameters are defined. Overload is basically having functions that, when you call on them, they can have similar types or not of that type in the parameters. //prints out 4. 0 static int sum(int x, int y) { return (int) (x + y); static double sum(double x, double y) { return x + y; There is another example of making a function using the same concept, except what if you try to use (cid:448)aria(cid:271)les alread(cid:455) defi(cid:374)ed for the para(cid:373)eter i(cid:374) (cid:373)ai(cid:374)(cid:894)(cid:895) (cid:271)ut the(cid:455) do(cid:374)"t (cid:373)at(cid:272)h a(cid:374)(cid:455) of the functions made.