Java Reference
In-Depth Information
You use the println() and print() methods to print a message on the standard output, as follows:
System.out.println("Prints a new line at the end of text");
System.out.print("Does not print a new line at the end of text");
If you use the System.out.println() method to print text to the console, after printing the text, it also prints a
new line character at the end of the text. The only difference between using the println() and print() is that the
former prints a new line at the end of the text, whereas the latter does not. The println() and print() methods are
overloaded. Until now, you have seen their use only with string arguments. You can pass any Java data type argument
to these two methods. The following snippet of code illustrates how to pass Java primitive types as arguments to
these methods:
int num = 156;
// Prints 156 on the console
System.out.println(num);
// Prints Value of num = 156 on the console
System.out.println("Value of num = " + num);
// Prints a new line character on the console
System.out.println();
Listing 4-1 contains a complete program to demonstrate the use of arithmetic operators and the string
concatenation operator.
Listing 4-1. An Example of Using Java Operators
// ArithOperator.java
package com.jdojo.operator;
class ArithOperator {
public static void main ( String[] args ) {
int num = 120;
double realNum = 25.5F;
double veryBigNum = 25.8 / 0.0;
double garbage = 0.0 / 0.0;
boolean test = true;
// Print the value of num
System.out.println ("num = " + num);
// Print the value of realNum
System.out.println ("realNum = " + realNum);
// Print the value of veryBigNum
System.out.println ("veryBigNum = " + veryBigNum);
// Print the value of garbage
System.out.println ("garbage = " + garbage);
 
Search WWH ::




Custom Search