/* SimpleOutput.java CIS 160 David Klick 2015-09-02 Simple demonstration of console output using: - System.out.print: displays String with no linefeed - System.out.println: displays String and then a linefeed - System.out.printf: formats output for display */ public class SimpleOutput { public static void main(String[] args) { // Declare and initialize variables int m = 3, n = 4; // Multiple variables of the same data type // can be declared and initialized together double pi = 3.1415926; String msg = "abcdefg"; // Note: String values are delimited using quotes // System.out.println does not require an argument System.out.println(); // displays nothing and goes to a new line // But normally, print/println have one argument. That's what they will display. // Display: Test output... abcdefg System.out.print("Test output... "); // displays string with no line feed at end System.out.println(msg); // displays value of variable with linefeed at end // Numbers can be displayed easily // Note: System.out.print/println converts them to Strings first System.out.println(9); // displays: 9 System.out.println(m); // displays: 3 System.out.println(n); // displays: 4 // Multiple items can be displayed by concatenating them into one String System.out.println("Hi" + "there"); // displays: Hithere System.out.println("m = " + m); // displays: m = 3 System.out.println("m + n = " + (m + n)); // displays: m + n = 7 System.out.println("m + n = " + m + n); // displays: m + n = 34 // Note: The + sign can mean either addition of numbers or concatenation of // Strings. On the last println, Java chose concatenation because it was // evaluating the expression from left to right and it did not know how // to add m to the String, so it turned m into a String and concatenated the // two Strings. Then it did the same to n. // \n can be used to put a newline in a String // \r can be used to put a carriage return in a String System.out.println("a\nb\nc"); // displays a, b, c on separate lines // Many values can be concatenated for display System.out.println("The sum of " + m + " and " + n + " is " + (m+n)); // Note the parentheses around m+n at the end so it gets calculated // before everything gets concatenated. // System.out.printf is used to format output System.out.println("The value of Pi is " + pi); // displays: The value of Pi is 3.1415926 System.out.printf("The value of Pi is %6.2f\n", pi); // displays: The value of Pi is 3.14 System.out.printf("The value of Pi is %.2f\n", pi); // displays: The value of Pi is 3.14 // Note that System.out.printf has one or more arguments. // The first argument describes what will be displayed and the format. // The list of arguments after that provide the values for any conversion // specifiers in that first String. The conversion specifiers above are: // %6.2f = field has width of six, two decimal positions, floating-point number // %.2f = field width not specified, two decimal positions, floating-point number // You will usually have one extra argument for each conversion // specifier in the format String. System.out.printf("The value of m is %d, the value of n is %d\n", m, n); // Displays: The value of m is 3, the value of n is 4 } }