Java Println with Examples.

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 terminate 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 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 print stream class and declared as static in System class. Present in java.lang package.
Share:

Search This Blog

  • ()

Our Services

Powered by Blogger.

Top 20 Websites like Bookzz | Bookzz Alternatives Websites

The domain  Bookzz.org  isn’t into work anymore, and the world’s largest ebook library  Bookzz  has got a new domain name or its Alternati...