The methods System.out.print and System.out.println are predefined
for primitive types as well as for strings:
int i = 1;
double d = 5.2;
System.out.print(i);
System.out.println(d);
Furthermore, automatic
conversion to String type is performed by the concatenation operator +:
System.out.println("d = " + d + "and i = " + i);
The print statements can not print an entire array, so a method must be written:
public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i]);
System.out.println();
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
print(a);
}
Since the number of elements of an array can be large, it is better to write the method so that it inserts a newline after printing a fixed number of elements:
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
if (i % 8 == 7) System.out.println();
}
System.out.println();
}
You can put this static method in a publicly accessible class and use it to print any integer array. Similar methods can be written for the other primitive types.




