• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String Doubt

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code prints Name after = Test2. I thought it will be printing Test1 as String is an Object and hence passed by reference.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nischal,

you may find useful to read this

Hope that helps,
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name in the method has scope of local so it will as well take name in the method.
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Your mother was a hamster and your father was a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic