File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes passing by refernce   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "passing by refernce   " Watch "passing by refernce   " New topic
Author

passing by refernce

Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
Hi i am learning java. I want to write a small function(value should be pass by reference) to convert a string to Upper case

my coce is here


-------------------------***************-----------------------------------
ublic class rem {

String check ;
static String v ;
public void remove()
{
String v;
v = check.toUpperCase();
// = check.toUpperCase();

System.out.println(v);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(check);
rem r = new rem();
r.check = args[0];
r.remove(); // throws me error here
System.out.println();
System.out.println(r.check);


}
}

--------------------------*****************_____________-----------

the output is
IAMSMALL

iamsmall

i want both the output to be in Upper case see i thingk so there is no permanenr change in "check" value it is onlu changing in function and when it comes out of function t again becomes small

plz help
Michael Lloyd Lee
Greenhorn

Joined: Sep 07, 2005
Posts: 22
Java is pass by value (refrences are passed by value).
Combined with String which is imunable (unchangable) gives you the result you are seeing.

http://www.javaranch.com/campfire/StoryCups.jsp / http://www.javaranch.com/campfire/StoryPassBy.jsp


Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
yeh string in immutable but when i tried this code it worked (by trial and error) but i cannot understand wht did i do but i managed to get the output I would be obligd if you would explain me how did it work

----------------------***************-----------------------------
public class rem {

String check ;

String v;
public String remove(String c)
{

check = c.toUpperCase();
return v;
// why do i have to use a "return v" i know i have specified String as a return value


}
/**
* @param args
*/
public static void main(String[] args) {
rem r = new rem();
r.check = args[0];

r.remove(r.check);
System.out.println(r.check);

}
}
--------------------***************____________
Questions 1) why do i have ti use RETURN V; i know i have used a String as return type ....... but when i removed t hat return type it gives me error
rETURN TYPE IS MISSING
and correspondingly in the statement R.REMOVE(R.CHECK) it givesme error THE METHOD REMOVE(STRING) IS UNDEFINED FOR THE TYPE REM.

2) if we use the above code which is working then i have some fundamentals to be clear

i am calling r.remove(r.check) and corresponding to that line in the called function is public Strin remov(strin c) .......... domnt u think i should pass the whole class here.........
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Strings are immutable. once they are created, you cannot change them.

everything in java is pass by value. there is nothing you can do to change that.

when you pass any object (and a String IS AN object), you pass the value of the reference. you now have two variables referring to the same string in memory - the v in your method, and the member variable "check".

when you to the

v = check.toUpperCase();

java creates a new string, and changes v to point to the new string. "check" is still pointing to that old string. you print v, giving you the upper case.

you then return to your main, where "check" still points to the lowercase version, and that's what gets printed out.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Let's look at your code one line at a time in the order that it is executed:

This line creates a new object from the rem class. The variable named "r" is a reference to this object.



First, "args[0]" refers to a String object in the array named "args". On the other hand "r.check" stands for a member variable named named "check" in the object referenced by the variable named "r". The line of code assigns the reference on the right to the reference on the left. This means that both "r.check" and "args[0]" now refer to the exact same String object.



This calls the remove() method on the object that the variable named "r" refers to. It uses the member variable named "check" from the same object as the parameter.



This is the declaration for the remove() method. Technically, it is not executed. However, it shows us that the variable named "c" is a reference to a String object. Given the above method call, "c" now refers to the same String object as "r.check" does in main().

The key to the odd behavior is the next line of code:



The return value of toUpperCase() is a NEW String object with the same letters as the original, but they are all changed to lower case. So after this line is executed, "c" and "r.check" refer to DIFFERENT String objects. (Remember that before this line of code, they referred to the same String object.) The original object does NOT change since Strings are immutable.



This doesn't do anything useful since "v" is automatically null. Also if you look back at the line that calls this function, the return value is ignored anyway.



This prints out the new value of "r.check" from the assignment above.

After this lengthy description, it seems that the surprising result comes from using member variables. Can you figure out a way to do this using only local variables?

Layne


Java API Documentation
The Java Tutorial
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608



>java Main abc
abc
cba


.. though I doubt that this is what you want.


Tony Morris
Java Q&A (FAQ, Trivia)
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
Raj,
The most important about your question (in case you havent figured it out yet !) ... is not achieving the objective you had planned but learning the concepts that you inadvertently hit upon:
1. that Java is always pass-by-value
2. that String is immutable
3. use/abuse of instance variables (you havent answered to Layne's poser yet !?)
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
This in response to your specific queries (others have mentioned the fundamental concepts above, understanding which is a must for any java newbie )



Raj said
Questions 1) why do i have ti use RETURN V; i know i have used a String as return type ....... but when i removed t hat return type it gives me error
rETURN TYPE IS MISSING
and correspondingly in the statement R.REMOVE(R.CHECK) it givesme error THE METHOD REMOVE(STRING) IS UNDEFINED FOR THE TYPE REM.

2) if we use the above code which is working then i have some fundamentals to be clear

i am calling r.remove(r.check) and corresponding to that line in the called function is public Strin remov(strin c) .......... domnt u think i should pass the whole class here.........


Answer 1 :
You need not "return v" - just make the return type of your "remove" method "void" and try out the same code after commenting out "return v".
The thing is since you have explicitly mentioned your return type as String so you MUST return a String.

Answer 2 :
Another important aspect is your vocabulary ... "pass the whole class here" ... you can never pass a class. Please learn how to phrase that question correctly. Though you have been given the answer by Layne.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing by refernce