Results 1 to 2 of 2
- 12-17-2009, 03:38 AM #1
Member
- Join Date
- Sep 2009
- Location
- http://www.kouje.com
- Posts
- 16
- Rep Power
- 0
entities are passed by value or passed by reference
Are these past by "Value" or "Reference"?
Integer variables are passed by:
Value
Integer arrays are passed by:
Value
String variables are passed by:
Value
Object variables are passed by:
Value
i'm saying that they are passed by Value, right?
ps.
i know the code seems wierd or coded like a wth,,, but it was provided by my instructor. I'm guessing trying to make things tricky?
Java Code:/** * @author Carlos Saavedra * @version 8.1.5 2009/12/15 */ public class Assignment8_B { public static void main(String[] args) { int int_test = 10; int int_array[] = {100, 200, 300}; String str_test = "CIS 150"; MyClass object_test = new MyClass("Carlos", 15); System.out.println("int_test BEFORE method call...\t\t" + int_test); test_passing_int(int_test); System.out.println("int_test AFTER method call...\t\t" + int_test); print_array(int_array, "Int_array BEFORE method call...\t"); test_passing_array(int_array); print_array(int_array, "int_array AFTER method call.....\t"); test_passing_array(int_array); System.out.println("str_test BEFORE method call...\t\t" + str_test); test_passing_String(str_test); System.out.println("str_test AFTER method call...\t\t" + str_test); System.out.println("object_test BEFORE method call...\t" + object_test.toString()); test_passing_object(object_test); System.out.println("object_test AFTER method call...\t" + object_test.toString()); } public static void test_passing_int(int _i) { _i *= 2; } public static void print_array(int _arr[], String msg) { System.out.print(msg); for(int i=0; i<_arr.length; i++) System.out.print(_arr[i] + "\t"); System.out.println(); } public static void test_passing_array(int _arr[]) { for(int i=0; i < _arr.length; i++) _arr[i] *= 2; } public static void test_passing_String(String _str) { _str = "CIS150"; } public static void test_passing_object(MyClass _obj) { _obj.setNameHours("Pam\t", 16); } }
Java Code:public class MyClass { String name; int hours; public MyClass(String _name, int _hours) { name = _name; hours = _hours; } public void setNameHours(String _name, int _hours) { name = _name; hours = _hours; } public String toString() { return "Name: " + name + "\tHours: " + hours; } }
Last edited by syntrax; 12-17-2009 at 03:50 AM.
- 12-17-2009, 08:13 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Java has one passing mode, and one passing mode only, and that mode is pass by value.
See these two articles:
JavaRanch Campfire - Cup Size: a Story About Variables
JavaRanch Campfire - Pass By Value, Please
Similar Threads
-
Can anyone help me to create a INSERT INTO based on a class passed?
By victoreadward in forum New To JavaReplies: 7Last Post: 12-16-2009, 12:51 PM -
java certification passed
By natasha in forum Java CertificationReplies: 0Last Post: 12-05-2009, 09:24 AM -
Cannot get passed these syntax errors
By MrKP in forum New To JavaReplies: 1Last Post: 05-12-2008, 08:05 AM -
Null array when passed to MouseListener
By stevemcc in forum New To JavaReplies: 2Last Post: 04-02-2008, 11:42 PM
Bookmarks