Home : Course Map : Chapter 2 : Java : Print
String Output to Console
JavaTech
Course Map
Chapter 2

Introduction
Essentials
Structure

Keywords
Primitive Types
Comments
Literals
Expressions
Operators
Statements
Casts & Mixing
Strings
Console Output 
   Demo
Exercises

    Supplements
Conditional: if-else
Repetitions
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Java provides tremendous tools for creating graphical user interfaces, but we want to deal initially with the basics of the language. So we postpone most graphics programming until Chapter 5. Until then we will run programs from the command line console and send output to the console. By console we refer to the command line window where you run applications or to the Java console window in the browser holding the applet.

Java possesses a host of Input/Output tools and capabilities but, unfortunately, Java I/O is fairly complicated. So we will deal with I/O in several sections in the coming chapters and also devote all of Chapter 9 to I/O.

The available print commands include:

  System.out.print( string ) - no line return
  System.out.println( string ) - includes line return
  System.out.println() - line return (used to insert a blank line)

where string denotes any String object that you create as explained in the Strings section.

For example,

  int i = 5;
  int j = 3;
  System.out.println( "i * j = "+ (i*j) );

will result in the following output to the console:

  i * j = 15

The i*j expression resulted in an integer value, which the "+" append operation will convert automatically to a string and attach to the preceding string.

Note: The parenthesis around the x*y is not necessary according to the higher precedence of the multiplication operator compared to the "+" append operator. For the sake of clarity, however, there is no harm in placing the parenthesis.

Another example,

  double = 5.0;
  int y = 3.0;
  System.out.println( "x * y = "+ (x*y) );
  System.out.println( "x / y = "+ (x/y) );

results in console output like this:

  x * y = 15.0
  x / y = 1.6666666666666667

Note the variation in the number of digits in the fraction. The basic print method doesn't provide a way to specify the formatting for output, such as specifying the number of decimal places. Unlike many other languages such as C/C++, which has the printf(,,) function, Java separates the tasks of formatting and I/O. In Java you format strings and then send you can send the formatted strings wherever you need them such as to the console or to graphical elements such as labels and text boxes. We will wait till Chapter 5: Tech to discuss formatting of numerical strings.

Print Demonstration Programs

The following applet PrintApp1.java, illustrates the use of the Start_App1.java starter code to create a new app with the above code. The print will go to the same console where you entered the command lines to compile and run the program:

> javac PrintApp1.java
> java PrintApp1

ConsolePrintApp1.java

public class ConsolePrintApp1
{

  public static void main (String args[])
 {
    // Put code between this line
 
  //------------------------------------------

    double x = 5.0;
    double y = 3.0;
    System.out.println ("x * y = "+ (x*y));
    System.out.println ("x / y = "+ (x/y));


    //------------------------------------------
    // and this line.
  }
}

 

The following applet Print_Applet1.java derives from StartApplet1.java take from the set of starter codes. The Java Console will display the print output.

public class ConsolePrintApplet1 extends java.applet.Applet
{
  public void init ()
 {
    // Put code between this line
    //------------------------------------------
    double x = 5.0;
    double y = 3.0;
    System.out.println( "x * y = "+ (x*y) );
    System.out.println( "x / y = "+ (x/y) );

    //-------------------------------------------
    // and this line.
  }


  // Paint message in the applet window.
  public void paint (java.awt.Graphics g) {
    g.drawString ("ConsolePrintApplet1",20,20);
  }
}

We will explain in later chapters the details of these programs such as the meaning of the keywords class, extends, public, etc.

References & Web Resources

Latest update: Oct. 14, 2004

            Tech
Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
  
Demo 1
More Mix/Cast
  Demo 2
Exercises

           Physics
Differential Eq.
Euler Method
  
Demo 1
Predictor-Corrector
  
Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.