Download presentation
Presentation is loading. Please wait.
Published byBuck Gilbert Modified over 9 years ago
1
2.2 Information on Program Appearance and Printing
2
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.
3
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"); }
4
Notice how the examples so far have indents. It is true that the code will run the same without them. However, it’s good practice (and extremely helpful) to indent so that it is clear where a block of code begins and ends. Eclipse should do this for you automatically. All examples given will be shown in this form.
5
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");
6
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);
7
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");
8
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
9
However, you could also do this: System.out.println("Hello" + " World"); Or this: System.out.println("Hello" + " " + "World"); And you would see: Hello World
10
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
11
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
12
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.
13
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
14
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.
15
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("\\");
16
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.