Java Println with Example and Programs

Println in java: Println() method places the cursor in the next line after printing the current output. So the next output will be printed in the next line. Println is a method present in PrintStream Class where PrintStream class present in Java.io.package.

There are 10 Println methods in PrintStream Class. They are:

1) void println(): Terminates the current line by writing the line separator string.
2) void println(boolean x): Prints a boolean and then terminates the line.
3) void println(char x): Prints a character and then terminate the line.
4)  void println(char[] x): Prints an array of characters and then terminate the line.
5) void println(double x): Prints a double and then terminate the line.
6) void println(float x): Prints a float and then terminate the line.
7) void println(int x): Prints an integer and then terminate the line.
8) void println(long x): Prints a long and then terminate the line.
9) void println(Object x): Prints an object and then terminate the line.
10) void println(String x): Prints a string and then terminate the line.

Example of Println Program:


class PrintlnExample
{
  public static void main(String args[])
 {
  System.out.println("Int statement.");
  System.out.println("Double statement.");
  System.out.println("String statement.");
  System.out.println("Boolean statement.");
  System.out.println("Object statement.");
  System.out.println("Float statement.");
  System.out.println("Long statement.");
  }
}

Output:

Int statement.
Double statement.
String statement.
Boolean statement.
Object statement.
Float statement.
Long statement.

What is the difference between System.out.println() and System.out.print()  in Java?

Println(): Println() method places the cursor in the next line after printing the current output. So the next output will be printed in the next line.

Print():  Print() method places the cursor in the same line after printing the current output. So the next output will also be printed in the same line.
Example using both System.out.println() and System.out.print() methods:

Pyramid Program:
import java.io.*;
import java.util.*;
class PyramidExa1
{
  public static void printPyramid(int n)
  {
  int i, j, k = 2*n-2;
  for(i=0; i<n; i++)
   {
  for(j=0; j<k; j++)
     {
      System.out.print(" ");
     }
   k = k - 2;
    for(j=0; j<=i; j++)
     {
      System.out.print("* ");
     }
      System.out.println();
   }
  }
public static void main(String args[])
{
 Scanner in = new Scanner(System.in);
  System.out.println("Enter a number");
  int n = in.nextInt();
 printPyramid(n);
}
}

Output:
Enter a number
6
          *
        * *
      * * *
    * * * *
  * * * * *
* * * * * *

Explaining in detail about the System.out.println() method :

System: System is the name of the class present in java.lang package.

Out: out is an object of the print-stream class and declared as static in System class. Present in java.lang package.
Println(): println() is a method of the PrintStream class. Present in java.io package.
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive