Java Print with examples and programs


Print in Java: The print() method prints the required output on the same line continuously again and again on the screen. The print is a method present in PrintStream Class present in Java.io.package. 
There are 8 Print methods in PrintStream Class. They are:
1) void print(Boolean b): Prints a Boolean value.
2) void print(char c): Prints a character.
3) void print(char[] s) : Prints an array of characters.
4) void print(double d): Prints a double-precision floating-point number.
5) void print(float f) : Prints a floating-point number.
6) void print(int i) : Prints an integer.
7) void print(long l): Prints a long integer.
8) void print(Object obj) : Prints an object.
9) void print(String s): Prints a string.

Example of Program:
class PrintExample
{                
  public static void main(String args[])
 {
  System.out.print("Int statement. ");
  System.out.print("Double statement. ");
  System.out.print("String statement.");
  }
}

Output:
Int statement. Double statement. String 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:

Java program to print pyramid:
import java.io.*;
import java.util.*;
class PyramidExa
{
  public static void printPyramid(int n)
  {
  int i, j;
  for(i=0; i<n; i++)
   {
    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 the number");
  int n = in.nextInt();
 printPyramid(n);
}
}

Output:
Enter a number
7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Explaining in detail about the System.out.print() 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.
Print(): print() is a method of the PrintStream class. Present in the java.io package.
Share:

Search This Blog

  • ()

Our Services

Powered by Blogger.

FSU Webmail and login into the FSU Webmail

FSU Webmail FSU Webmail (Florida State University) Webmail is email administrations to the customer to get to messages anyplace through a...