Primitive pass by value,

Primitive pass by value,

Primitive pass by value, object pass by reference? Not exactly true, preferable statement should be Java variable hold reference to an object, they’re not hold objects.

Primitive Argument Test

Run the program, the result show the primitive int has hashcode value 1297685781, and the method’s parameter hashcode 1705929636. Thus changing value in method does not change to the origin value.

[Initially]
origin = 333 , hashcode= 1297685781

[change999] arg = 333 , hashcode = 1705929636
[change999] b = 333 , hashcode = 1221555852

origin = 333 , hashcode= 1509514333

String Pool Effect

Let’s try another example, and observe the variable hashcode define in main method and compare the hashcode in function’s body. If the string exist before in string pool the hashcode has not changed otherwise it will create a new string object and value return back to the variable. Any how, the original variable will not get change from the method called.

public static void main(String[] args) {
	String text = "hello";
	String word = "world";
	String more = word;
	
	System.out.printf("Text: %s, Hashcode: %d \n", text, System.identityHashCode(text));
	System.out.printf("Word: %s, Hashcode: %d \n", word, System.identityHashCode(word));
	System.out.printf("more: %s, Hashcode: %d \n\n", more, System.identityHashCode(more));
	
	changeText(text, word, more);
	
	System.out.printf("Text: %s, Hashcode: %d \n", text, System.identityHashCode(text));
	System.out.printf("Word: %s, Hashcode: %d \n", word, System.identityHashCode(word));
	System.out.printf("more: %s, Hashcode: %d \n\n", more, System.identityHashCode(more));
}

public static void changeText(String _text, String _word, String _more) {
	System.out.printf("_Text: %s, Hashcode: %d \n", _text, System.identityHashCode(_text));
	System.out.printf("_Word: %s, Hashcode: %d \n", _word, System.identityHashCode(_word));
	System.out.printf("_More: %s, Hashcode: %d \n\n", _more, System.identityHashCode(_more));
	
	_text = "hello";
	_word = "greeting";
	_more = _text;
	
	System.out.printf("_Text: %s, Hashcode: %d \n", _text, System.identityHashCode(_text));
	System.out.printf("_Word: %s, Hashcode: %d \n", _word, System.identityHashCode(_word));
	System.out.printf("_more: %s, Hashcode: %d \n\n", _more, System.identityHashCode(_more));
	
}
## Output ##

Text: hello, Hashcode: 1297685781 
Word: world, Hashcode: 411631404 
more: world, Hashcode: 411631404 

_Text: hello, Hashcode: 1297685781 
_Word: world, Hashcode: 411631404 
_More: world, Hashcode: 411631404 

_Text: hello, Hashcode: 1297685781 
_Word: greeting, Hashcode: 897913732 
_more: hello, Hashcode: 1297685781 

Text: hello, Hashcode: 1297685781 
Word: world, Hashcode: 411631404 
more: world, Hashcode: 411631404 

Object pass reference as value

Run the program and observe the output. The person’s object has hashcode 1297685781. When it pass to the method’s parameter, which hold the same copy of a reference to an object, thus making changes in the method cause the value changed.

[initially]
Age = 12 , hashcode = 1297685781

[changeAge99] Parameter's hashcode = 1297685781
Age = 99 , hashcode = 1297685781

[changeAge88] age hashcode = 1705929636
Age = 99, hashcode = 1705929636

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.