wood burning stoves 2.0*
The moose likes Java in General and the fly likes String Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Doubt" Watch "String Doubt" New topic
Author

String Doubt

Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182

The above code prints Name after = Test2. I thought it will be printing Test1 as String is an Object and hence passed by reference.


Thnx
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hello Nischal,

you may find useful to read this

Hope that helps,


Lionel Badiou
CodeFutures Software
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
The name in the method has scope of local so it will as well take name in the method.


"There are no mistakes, only lessons"
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by Amol Fuke:
The name in the method has scope of local so it will as well take name in the method.


Not justified to say that. If thats the case then there will be no such concept of Pass By Reference
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Nischal Tanna:
there will be no such concept of Pass By Reference
I hate to give anyone bad news, but it's true: Java has no concept of pass-by-reference. Everything is pass-by-value -- even when you're passing a reference.

The name reference has a value which you can think of conceptually as the address of the "Test2" String. When you execute swap(name), a copy of that value is placed onto the stack for the swap() method to access. When swap() changes that value, it's changing the copy of the reference, so it has no effect on the main() method's name reference.

It seems like Java has pass-by-reference because you can alter objects through references passed to methods. Yet this is no different than passing a pointer to a struct to a C function. In that function you can alter the struct's fields but you cannot alter the pointer itself. To do that, you'd have to pass a pointer to the pointer in C or change the argument to a pointer reference in C++.

Neither of these are possible in Java.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by David Harkness:
I hate to give anyone bad news, but it's true: Java has no concept of pass-by-reference. Everything is pass-by-value -- even when you're passing a reference.

It seems like Java has pass-by-reference because you can alter objects through references passed to methods. Yet this is no different than passing a pointer to a struct to a C function. In that function you can alter the struct's fields but you cannot alter the pointer itself. To do that, you'd have to pass a pointer to the pointer in C or change the argument to a pointer reference in C++.


Semantics question:
But isn't passing a reference by value the exact same thing as passing and object by reference? Isn't that how pass-by-reference is defined?

Neither of these are possible in Java.


Depends on what hoops you're willing to jump through. You could wrap a reference to an object in another object and pass that reference. Let's see are there any good single-object wrappers already? An ArrayList or other Collection seems like overkill for just one object.

Ryan
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
No, passing an object by reference and passing a reference by value are not the same thing...



If Java were pass by reference, this program would print "Some other string" as opposed to "Hello, World!". Pass by reference would allow you to change which object the calling code is referring to.


James Carman, President<br />Carman Consulting, Inc.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by me:
To do that, you'd have to pass a pointer to the pointer in C or change the argument to a pointer reference in C++.

Neither of these are possible in Java.


Originally posted by Ryan McGuire:
You could wrap a reference to an object in another object and pass that reference.
Yes, but here you've fallen back on pass-by-value: passing the value of a reference to a struct. This is not one of the two pass-by-reference methods above. Yes, it seems like semantics, but at the heart it really isn't. Java has no pass-by-reference.

As an example, pretend that Java didn't have the ++ and -- operators. You could say that Java still has an increment operator because you can use "x = x + 1" and "x += 1", but those really aren't increment operators.
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
I think that in the swap() function the copy of name is directed to point at whatever the String constant expression "Test1" points to. When control leaves the function, the original variable's value is unchanged. It's just like a C function, when you pass a pointer into a function, then when inside the function make it point to something else (instead of dereferencing it and changing its contents). Keep in mind that there's no such thing as dereferencing in Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Doubt
 
Similar Threads
I am getting clone() has protected access in java.lang.Object
Java Rule Round-Up
Code within a curly braces
Class Reference
find creator of an object