Java Program Layout, printf and newline

Java Program Layout, printf and newline
Page content

A word on program layout

In the previous article, we met the following program:

public class Welcome { public static void main(String[] args) { System.out.printf(“Welcome to BrightHub”); } }

Java does not require the program to be laid out as in the example. An equivalent program is

public class Welcome {public static void main(String[] args) { System.out.printf(“Welcome to BrightHub”); } }

All that matters is that the order of the words and punctuation is maintained. For this small program, it probably does not matter which version we use. However, as program size increases, it becomes imperative that the layout of the program highlight the logical structure of the program, thus improving its readability. Indentation and clearly indicating which right brace matches which left brace can help in this regard. We will see the value of this principle as our programs become more substantial.

Writing output with printf

Suppose we want to write a program to print the lines (taken from the Gitanjali by Rabindranath Tagore):

Where the mind is without fear

And the head is held high

Our initial attempt might be:

public class Gitanjali { public static void main(String[] args) { System.out.printf(“Where the mind is without fear”); System.out.printf(“And the head is held high”); } }

However, when run, this program would print:

Where the mind is without fearAnd the head is held high

Note that the two strings are joined together (we say the strings are concatenated). This happens because printf does not place output on a new line, unless this is specified explicitly. Put another way, printf does not automatically supply a newline character after printing its argument(s). A newline character would cause subsequent output to begin at the left margin of the next line.

In the example, a newline character is not supplied after fear is printed so that And the head… is printed on the same line as fear and immediately after it.

The newline character, \n (backslash n)

To get the desired effect, we must tell printf to supply a newline character after printing …without fear. We do this by using the character sequence \n (backslash n) as in the following, which must be saved in a file named Gitanjali.java.

public class Gitanjali { public static void main(String[] args) { System.out.printf(“Where the mind is without fear\n”); System.out.printf(“And the head is held high\n”); } }

The first \n says to terminate the current output line; subsequent output will start at the left margin of the next line. Thus, And the… will be printed on a new line. The second \n has the effect of terminating the second line. If it were not present, the output will still come out right, but only because this is the last line of output. A program prints all pending output just before it ends.

References

Reference text: Java Programming – A Beginner’s Course

Related programming references

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions