CSE 14 Lecture Notes - Lecture 10: 5,6,7,8

33 views5 pages
3 May 2018
School
Course
Professor
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 ariales alread defied for the paraeter i ai ut the do’t ath 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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
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:
“trig []  = {, , , , , , , , , , };
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.pritl[] +   + [];
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions