• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assign new value to object ref in method

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am assigning new value to my whereStatement in the appendToWhere method and the value of whereStatement is null when returning from appendToWhere,
Isn't this call ByRef?
I will be glad to get explenation.


The function body:



Thank you
[ July 29, 2008: Message edited by: Sharon whipple ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you get a NullPointerException as soon as you call appendToWhere ? The first thing you do in there is dereference the whereStatement parameter which is null.

The reason this is not call by reference (which Java never uses) has been discussed many times. A quick search should give you the answer.
[ July 29, 2008: Message edited by: Joanne Neal ]
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the method so i don't get a NullPointerException

Is this case will be the same in C++ or C# ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sharon whipple:
Is this case will be the same in C++ or C# ?



I don't know about C#, but C++ allows call by reference, so it would be possible to do what you are trying to do, but it also allows you to write the method so it works the same way as Java does.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C++ it does the same as Java by default; you have to explicitly specify it is a reference (using & I believe).
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
In C++ it does the same as Java by default; you have to explicitly specify it is a reference (using & I believe).

I thought C and C++ were call-by-value too. But you can pass a pointer to a method, as you say, with the & operator.But I thought you are actually passing the value of the pointer, thereby mimicking pass-by-value. You can't alter the pointer, but you can alter its contents.

[edit]Last but one line should read "mimicking pass-by-reference."[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I thought C and C++ were call-by-value too. But you can pass a pointer to a method, as you say, with the & operator.But I thought you are actually passing the value of the pointer, thereby mimicking pass-by-value. You can't alter the pointer, but you can alter its contents.

[edit]Last but one line should read "mimicking pass-by-reference."[/edit]

[ July 30, 2008: Message edited by: Campbell Ritchie ]


That's not quite what I meant. That's not using a reference but a pointer. Something that does not exist in Java.

In C++ (not C!) you can do the following:

This will print "x = 10, y = 5". Remove one or both &s and there is no more swapping.
Note how you do not have to use any special syntax in the method call, just the method definition. A real improvement over the pointer syntax.
reply
    Bookmark Topic Watch Topic
  • New Topic