Meaning of + symbol with Strings in Java println statement

后端 未结 7 979
孤独总比滥情好
孤独总比滥情好 2021-01-16 09:57

I\'m new to Java. What does the below mean?

(addition) + sign in println

System.out.println ("Count is: " + i);

7条回答
  •  天涯浪人
    2021-01-16 10:17

    The + in arithmetic adds 2 numbers together, like this:

    2 + 2 = 4
    

    now apply the same thing to a string:

    "hello " + "world!" = "hello world!"
    

    now adding strings and variables will do this:

    int number = 4;
    String string = "what was the number? oh yeah: "+number;
    System.out.println(string);
    

    if all goes well you should get "what was the number? oh yeah: 4"

    Java took the value of the variable and put it into the string for you, hope this helped!

提交回复
热议问题