-1

I have simple method doubleChar that receives a String, doubles each character and returns a String. Then I have two invocations in main method. First straightforward invocation gives no result, but second invocation inside println method gives the right string with doubled characters.

Can anyone explain how this works?

static String doubleChar(String str) {
    String result = "";
    for (int i = 0; i < str.length(); i++) {
        result = result + str.charAt(i) + str.charAt(i);
    }
    return result;
}

public static void main(String[] args) {
    String dog = "Dog";
    
    // 1st invocation
    doubleChar(dog);
    System.out.println(dog);//result is Dog
            
    //2st invocation inside println
    System.out.println(doubleChar(dog));//result is DDoogg
}
Gautham M
  • 4,816
  • 3
  • 15
  • 37
Edson
  • 11

4 Answers4

0

I get doubled characters every time. I can prove it by running jshell, which is a REPL provided with recent versions of Java:

$ jshell
|  Welcome to JShell -- Version 11.0.10
|  For an introduction type: /help intro

jshell> String doubleChar(String str) {
   ...>     String result = "";
   ...>     for (int i = 0; i < str.length(); i++) {
   ...>         result = result + str.charAt(i) + str.charAt(i);
   ...>     }
   ...>     return result;
   ...> }
|  created method doubleChar(String)

jshell> String dog = "Dog";
dog ==> "Dog"

jshell> doubleChar(dog);
$3 ==> "DDoogg"

jshell> doubleChar(dog);
$4 ==> "DDoogg"

The reason you do not see doubled characters the first time is because you print out the variable, not the result:

System.out.println(dog);

One way to minimize errors like this is to annotate your output, like this:

System.out.println("dog=" + dog);
System.out.println("doubleChar(dog)=" + doubleChar(dog));

Output is:

dog=Dog
doubleChar(dog)=DDoogg
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
0

I'm guessing that the second output "DDoogg" is happening because you place the doubleChar inside the System.out.println(); which prints DDoogg

0

The invocation of doubleChar(dog); does not print anything to the console nor does is change the value of the String dog. Hence printing out dog after invoking the method only prints the original value in dog. (String are immutable in java). The method doubleChar(dog); just returns a String, which you are not using in case of first invocation. But the second invocation, you have used the returned value and printed it to console using println method.

Gautham M
  • 4,816
  • 3
  • 15
  • 37
0

In the first invocation the value that is returned from the function (doubleChars()) has not been assigned to your variable (dog). So you call the method, it returns a value, but you do nothing with the value that is returned.

In the second invocation you print directly the returned value of the function to the console.

To resume, if you want to print the returned value to the console using the first invocation you do: `

String dog = "Dog";
dog = doubleChar(dog);
System.out.println(dog);`