Basic Java Language: Difference between revisions
wikademia>Historybuff m →Primitive Data Types: slight reword, whitespace |
wikademia>Historybuff m →Using the Displayer with Variables: added forward ref, clarified literal ref |
||
| Line 200: | Line 200: | ||
<code>System.out.println(VARIABLE IDENT + "")</code> | <code>System.out.println(VARIABLE IDENT + "")</code> | ||
This is because that method's parameter requires a string, and to change a variable to a string you say +"". Note that this only works on primitive types. On objects you have to have a method toString() | This is because that method's parameter requires a string, and to change a variable to a string you say +"". Note that this only works on primitive types. On objects you have to have a method toString(), which we will learn about later. | ||
Example: | Example: | ||
| Line 210: | Line 210: | ||
You can put it anywhere: <code>System.out.println(false + "" + 54)</code> | You can put it anywhere: <code>System.out.println(false + "" + 54)</code> | ||
Note that this won't work for two | Note that this won't work for two literal numeric values: | ||
<code>System.out.println(54 + 32)</code>. It would mistake it for the adding operator for values. For strings, the operator is different. So you would have to do: <code>System.out.println(54+""+32)</code> | <code>System.out.println(54 + 32)</code>. It would mistake it for the adding operator for values. For strings, the operator is different. So you would have to do: <code>System.out.println(54+""+32)</code> | ||