Parameter Passing - Java and the Web - Lecture Slides, Slides of Java Programming

During the course study of Java and the Web, I study the main concept about the different programming languages, specially java and the application of the java on the web. In these slides the main key points which I focused during my preparation are: Parameter Passing, Method, Type, Actual Parameters, Passed, Method Header, Copied, Primitive Parameter Types, Reference Type Parameter, Formal Parameter

Typology: Slides

2012/2013

Uploaded on 04/23/2013

saravati
saravati 🇮🇳

4.4

(29)

162 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Parameter Passing
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Parameter Passing - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Parameter Passing

Parameter Passing – Primitive

Type

  • Java passes all parameters to a method by value.
  • The actual parameters (the values that are passed in) are assigned to the formal parameters (declared in the method header).
  • This means that the value of the actual parameter is copied into the formal parameter.

Parameter Passing – Primitive Type

Example

public class PrimitiveParameter { public void changeValue (int parameter) {parameter = 20;} public static void main (String args[]) { int parameter = 10; PrimitiveParameter test = new PrimitiveParameter(); test.changeValue(parameter); } }

Parameter Passing – Primitive Type

Example

  • In the main method:
    • The “parameter” is declared and assigned the value 10 (the actual parameter).
    • After the new class instance creation the method changeValue is called. As a result of the call a copy of formal parameter is made and sent to the method.

Parameter Passing – Reference

Type

  • When an object is passed to a method, Java passes a reference to that object.
  • The value that is copied is the address of the actual object.
  • Therefore it is important to understand that when a formal parameter object is used it modifies the actual object state permanently.

Parameter Passing – Reference

Type Example

public class RefParam { public void changeObject (Car parameter) {parameter.changeParameter();} ... RefParam change = new RefParam(); parameter = new Car(); change.changeParameter(parameter); }

Parameter Passing – Reference

Type Example

  • In code section:
    • The formal parameter does not exist anymore, but the object still exists in its changed state.
    • In case the value of the formal parameter is changed it points then to a different object.

Parameter Passing – Applet

  • Parameters are also passed from HTML files to Java applets. - The HTML file passes parameters as string types, therefore they need to be converted if used as another type. - The parameters are initialized in applet tag section of HTML file and retrieved in init() method of Java applet.

Parameter Passing – Applet

Example

  • The Java applet code section: init() { String FontName = getParameter(“fontName”); String FontSize = getParameter(“fontSize”); String Leading = getParameter(“leading”); String PaidUp = getParameter(“accountEn”); ... }

Methods and Parameters

  • Methods are the cornerstone of OO programming, the basic unit of action
  • Parameters allow the caller of the Method to affect its outcome by providing a value or values when it is invoked
  • Without parameters, adjusting the Method outcome based upon invoker state would be quite cumbersome, if not impossible

Array Parameters

  • Java has native support for Arrays as parameters
  • A Java array is self-aware of its own length, unlike C/C++. All array information array can be passed as a single parameter, no length parameter is needed.
  • Arrays are passed in the same way as Objects

Object Parameters

  • Anything that is not a primitive or array falls under the umbrella of Object
  • Objects can be passed natively, just like primitives or arrays
  • In Java, handles to anything that is not primitive or an array is a reference.
  • Does this mean Java passes Object parameters by Reference?

Modify a Parameter Value

… Point p = new Point(10, 20); addTen(p); System.out.println(p.x + “,“ + p.y); … void addTen(Point p) { p.x = p.x + 10; p.y = p.y + 10; }

Modify a Parameter Value

  • The output of the System.out.println will be 20, 30
  • What if you do not want an outside method modifying your parameter? Ways to handle this will be explored later