0

I've been trying to make a method that will change a character into the character 'D', however what i've done so far just makes the same character that was inputted to output...Here's my code so far:

    public void changeLetters(char letterFrom){
    letterFrom = 'D';
    }
Tommon
  • 29
  • 7
  • java doesn't support primitive references, so your `letterFrom` is different from character which was passed to the function as an argument – mychalvlcek Nov 17 '13 at 11:30
  • You're just trying to change a value passed by copy, not by reference. So you're only changing the value of the _local_ variable (method parameter). – Lyubomyr Shaydariv Nov 17 '13 at 11:41

6 Answers6

2

Java passes parameter by values. In your case you just changed the value copy in the function.

If you need the behavior you want, you can either make the character a return value, as:

public char changeLetters(char letterFrom){
    return 'D';
}

Or encapsulate the value in a class object and pass that object, like:

class Container {
    public char letter;
}

public char changeLetters(Container letterFrom){
    letterFrom.letter = 'D';
}

EDIT

I have to clarify the method using Character class suggested by others are incorrect.

Just test below code, it will happily print a instead of d.

class a {
    public static void changeLetter(Character ch) {
        ch = 'D';
    }

    public static void main(String[] args) throws Exception {
        Character d = 'a';
        changeLetter(d);
        System.out.println(d);
    }
}

The reason is the same: because Java passes EVERYTHING by value. Even you used the Character object to pass parameter, when you do assignment like ch = 'D', you are only changing the inside copy of the reference ch.

Xiangyan Sun
  • 453
  • 4
  • 11
  • Java only passes value with primitive types like int, char, ... If you pass an object it will pass by reference (as an object is a reference), but in this case that will also be overridden. – Alexander Cogneau Nov 17 '13 at 11:32
  • 1
    No, in fact Java passes everything by value. When you are passing objects, you are actually passing the reference (or pointer) by value. Just try modifying a reference passed by parameter in a method. – Xiangyan Sun Nov 17 '13 at 11:34
  • [This page](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) has descriptions of this behavior in detail. – Xiangyan Sun Nov 17 '13 at 11:37
  • Thanks, but for the one with "return 'D';" how would i then use this method in another class? – Tommon Nov 17 '13 at 12:36
  • @Tommon Let's say the variable you want to change is named `ch`. At the place you would insert the previously incorrect `changeLetters(ch)`, instead you type `ch = changeLetters(ch)` – Xiangyan Sun Nov 17 '13 at 13:24
0

variable letterForm is primitive type, it is not an referance type.

The changed value 'D' only works in changLetter method only.

public void changeLetters(char letterFrom){
letterFrom = 'D';
}
Mengjun
  • 3,159
  • 1
  • 15
  • 21
0

This will only change letterFrom in the local scope, what is, in this case, the function.

You should make this function return it:

public char changeLetters(char letterFrom{
    return 'D';
}

and then use it later:

char oldChar = 'A';
char newChar = changeLetters(oldChar);
Alexander Cogneau
  • 1,286
  • 4
  • 12
  • 25
0

U should know that char is the original type of JAVA,that is, it is NOT an object and you pass its value into this function.Try Charater class.

BUPTOrange
  • 56
  • 1
  • 6
0

The problem is that when you change the letterFrom variable inside the function, you change the value of the one in the function, the function only has a copy of the original character and you changed that copy, not the original value. If you instead copied the letterFrom = 'D'; line into the location you're calling the changeLetters function, it should work.

Alice Ryhl
  • 3,574
  • 1
  • 18
  • 37
0

char is a primitive datatype in java, thus its value is always copied when passed to a function. So when you change the value of letterFrom, you actually change the value of the local copy on the stack (which is discarded once the function call ends).

If you want that particular behaviour (although considered bad practice) you could use a object-wrapped char: the class Char, since this would change the referenced class:

void changeLetter(Char characterReference) {
    characterReference = 'D';
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mattis
  • 23
  • 3