Next: Integer expressions Up: Unit 04 Previous: Reading of numbers of

## Writing of numbers of type int

To write a number of type int, we can directly use the print() or println() methods:

Example:

```int i = 1;
System.out.println(4);
System.out.println(i);
System.out.println(i + 4);
```

Note: the symbol + can be used both for the sum of two numbers and to concatenate two strings: "aaa" + "bbb" corresponds to "aaa".concat("bbb").

Note the difference between the following two statements:

```System.out.println(3 + 4);   // prints 7 (as int); + denotes sum
System.out.println("3" + 4); // prints 34 (as String), since the integer 4 is
// first converted to a String; + denotes concat
```

• In the first statement, ``+'' is applied to two integers, and hence denotes the addition operator. Hence, the argument 3+4 of println() is of type int.
• In the second statement, , ``+'' is applied to a string and an integer, and hence denotes string concatenation. More precisely, the integer 4 is first converted to the string "4", and then concatenated to the string "3". Hence, the argument "3"+4 of println() is of type String.

Both statements are correct, since the method println() is overloaded: the Java library contains both a version that accepts an integer as parameter, and a version that accepts a string as parameter.

Next: Integer expressions Up: Unit 04 Previous: Reading of numbers of