Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using PARAMETERS In Java.

Similar presentations


Presentation on theme: "Using PARAMETERS In Java."— Presentation transcript:

1 Using PARAMETERS In Java

2 First, what is a reference?
© A+ Computer Science -

3 References In Java, any variable that refers to (points to) an Object is a reference variable, where it is located. The variable stores the memory address of the actual Object. All variables in Java that refer to Objects are called references. Reference variables store the location / memory address of the actual Object. For most situations, the value stored in a reference is a memory address.

4 References x "Chuck" Name x = new Name("Chuck");
The variable x here refers to the Object created on the right – it points to the the Object (in its location) in memory – it ..holds the address. x In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B3 0x2B3 "Chuck"

5 References y x "Chuck" Name x = new Name("Chuck"); Name y = x;
x and y now store the same memory address. y x In this example, x and y both the store the location of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B7 0x2B7 0x2B7 "Chuck"

6 References y x “Pilot" “Accord" Honda x = new Honda(“Accord");
Honda y = new Honda(“Pilot"); x and y – two different variables - store different memory addresses. Two different objects – two memory locations with two different variable names y x In this example, x stores the location / address of a String Object that stores the value Chuck. y also stores the location of a different String Object that stores the value Chuck. x and y do not store the same location / address. For this example, x==y is false. x and y do not store the same location / address. For this example, x.equals(y) is true. 0x2FE 0x2B7 0x2FE 0x2B7 “Pilot" “Accord"

7 References y x "Chuck" String x = "Chuck"; x holds 0x2B7
String y = "Chuck"; y holds 0x2B7 x = null; x now holds null y x In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. At the start, x==y is true. x is then referred to null. x now stores null. y was in no way changed. y still stores the address of Chuck. After changing the value of x, x==y is false. 0x2B7 0x2B7 0x2B7 null "Chuck"

8 What is a parameter? © A+ Computer Science -

9 Parameters Parameters are variables in a method signature parentheses.
public int gator( int ali, int croc) { eater = ali + croc } a) Notice, they are variables, not actual values. b) These parameters also occupy a certain position. Notice that ali is first, then croc. Parameters are used to pass information to a method. Many methods need information in order to perform some operation. The parameters tell the method what to do and how to do it.

10 Parameters public int gator( int ali, int croc) {
int eater = ali + croc; System.out.println(eater); } Animal.gator( 200, ); arguments When “gator” is called here, the calling method has data -- called arguments – that is passed to the parameters, to be used inside the method . Animal calls gator, passing 200 and 400 to it, to use. It will print 600. Parameters are used to pass information to a method. Many methods need information in order to perform some operation. The parameters tell the method what to do and how to do it.

11 Parameters void setColor(Color theColor) the call passing “red”
setColor() is a method of the Graphics class whose parameters receives a Color. void setColor(Color theColor) the call passing “red” window.setColor( Color.red ); Most, if not all, of the Graphics class methods require parameters. The parameters communicate to the Graphics methods information about what needs to be done. The setColor() method changes the current drawing color to the color passed in. setColor() cannot be called without a color parameter. method call: passing argument to parameter

12 Parameters void fillRect (int x, int y, int width, int height)
window.fillRect( 10, 50, 30, 70 ); method call: arguments to parameters The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a heigth. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters.

13 Parameters void fillRect(int x, int y, int width, int height)
window.fillRect( 10, 50, 30, 70 ); The call to fillRect will draw a rectangle at position 10,50 with a width of 30 and a height of 70.


Download ppt "Using PARAMETERS In Java."

Similar presentations


Ads by Google