-7

When I do the first code I'm getting the following output (whereas it is expected to be something else). Now with a small change the result is different. Why is that?

CASE 1

package mydemo;

public class hw {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("HelloWorld");
        int x =10;
        int y =20;
        int result = x + y;
        System.out.println(" result");


    }

}

output:

HelloWorld
 result

CASE 2

package mydemo;

public class hw {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("HelloWorld");
        int x =10;
        int y =20;
        int result = x + y;
        System.out.println(" result = " + result);


    }

}

output

HelloWorld
 result = 30
Mat
  • 202,337
  • 40
  • 393
  • 406
  • no idea what you are asking – Scary Wombat Nov 29 '16 at 06:37
  • Why would you think that it would print out the value of the variable `result` if you don't tell it to? We may be able to help you more if you tell us the expected output. – AntonH Nov 29 '16 at 06:38
  • What did you expect? – aksappy Nov 29 '16 at 06:45
  • 1
    `"result"` and `result` are to different thing, the first is a String representation of the word _result_ the seconds is a variable name. So basicly, you resolved your problem off _How to print the value of a variable_ yourself by telling Java to print the variable and not just a constant String. – AxelH Nov 29 '16 at 06:48
  • 1
    you need to read the book "complete reference java" . – divine Nov 29 '16 at 06:56

2 Answers2

0

When you tell it to:

System.out.println(" result");

it prints the stuff in the parentheses, which is " result". The quotation marks around the word "result" means that it is a string literal. So the output is

 result

In the second case, this is in the parentheses:

" result = " + result

As you can see, we still have a string literal - " result = ". You see the + character over there? That thing connects thing. So here, it connects " result = " with result. What is result? Since it has no quotes around itself, it is not a string literal. Here, result refers to the value of the variable called result. What is the value? 30!

So that's why it prints:

 result = 30
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The only difference in your two cases, is the parameter passed to you "println" function:

In CASE1:

System.out.println(" result");

Here the parameter " result" (double quote should not be omited) is a string literal. It represents the word itself you typed between the double quote symbol and do not related with the int variable result you have defined in the previous line.

Thus when you call prinln, the function received an String variable and print to the console as it is.

In CASE2:

System.out.println(" result = " + result);

And in the above case, however, you passed an expression "result = " + result to the println function, in your case, which is a String "plus" an int variable operation. Java compiler will firstly convert your expression " result = " + result into a String " result = 30" and then pass it to the println function.

What the compiler do to the above expression is just like replacing it with the following code:

new StringBuilder(" result = ").append(result).toString();

Which will generate some kind of java bytecode like the following:

12: invokespecial #26                 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
15: iload_1       
16: invokevirtual #29                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
19: invokevirtual #33                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;

Also find this answer useful if you have interest in how the compiler dealing with the expression:

How does the String class override the + operator?

Community
  • 1
  • 1
Z.Francis
  • 16
  • 1