
Understanding Program Appearance and Printing
Learn about comments in programming, formatting code for readability, and important aspects of printing in Java. From using comments to understanding the difference between println() and print(), this guide covers essential concepts explained with helpful visuals.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
2.2 Information on Program Appearance and Printing
Comments are explanatory notes that a programmer can include in a program. They are not code and they do not affect how the program runs. On any line where // appears, whatever follows this pair of symbols is treated as a comment. Likewise, anything that appears between /* and */ will be treated as a comment.
Here is a commented version of the first program: /* This is my first program. */ public class FirstProg { public static void main(String[] args) { // A method is called here. System.out.println("Hello World"); } }
When writing a program it is helpful to indent every set of matched braces so that it is clear where a block of code begins and ends, and whether or not there are nested sets of blocks. Eclipse will do this for you automatically, and all examples given will be shown in this form.
Here is a brief summary of some of the aspects of printing in Java when using println() and related methods. 1. The method println() prints a line of output followed by a new line or carriage return. You can also make use of the method print(), which prints a line without a carriage return. The complete call would be of the form: System.out.print("Some Parameter");
2. You can print arithmetic constants as well as strings of characters. A call such as this would print out the numeric value 3: System.out.println(3);
Note that what would appear on the screen for the following call would be exactly the same, but that there is an important distinction in the meaning of the program code. The example above prints a numeric value, while the example below prints a string containing the symbol 3 : System.out.println("3");
3. You can print out more than one parameter at a time. The symbol that accomplishes this is the + sign. When printing strings, if you want blank spaces between things, you need to supply them explicitly. If you did this: System.out.println("Hello" + "World"); You would see this: HelloWorld
However, you could also do this: System.out.println("Hello" + " World"); Or this: System.out.println("Hello" + " " + "World"); And you would see: Hello World
4. Using the + sign when printing numeric values has a different effect. It does the arithmetic. If you did this: System.out.println(3 + 4); You would see this: 7
5. You can also combine a parameter in quotes with a numerical value without quotes using a + sign. In this case the system automatically converts the numerical value to a string and does concatenation. If you did this: System.out.println("Hello" + 7); You would see this: Hello7
The full explanation of the different ways in which the + sign works will be given later. For the time being you should simply be able to use it correctly with strings and numbers.
6. Although the dot has a special syntactic meaning in various places in Java code, in places where simple parameters in the form of numbers are allowed, such as when printing, it has its customary meaning that of a decimal point. Thus, if you did this: System.out.println(3.4); You would see this value: 3.4
7. In Java printing, the backslash \ is used as the escape sequence. This means that any symbol immediately following the backslash is simply treated as a printable character. That character is not treated as having any syntactic meaning. This allows you to print characters that would otherwise be ambiguous or syntactically incorrect in a printing parameter.
If you wanted to print a double quote, you could do this: System.out.println("\""); If you wanted to print the backslash itself, you could do this: System.out.println("\\");
The backslash can also be used to insert special printing instructions into a printed string. \n represents a new line. Thus, if you did this: System.out.println("Hello\nWorld"); You would see this: Hello World