All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Stdin And Stdout In Java

Last Updated : Mar 11, 2024

Stdin And Stdout In Java

In this article we will show you the solution of stdin and stdout in java, to discover uses of standard input and output, you must import util package that supports your code inside program.

May some people not know what is stdin & stdout means, which intimates standard input and standard output.

In java, which is mostly used by everyone for read and print methods referring this concept. Everyone using scanner object and println() method, but not we aware this is the standard input and output methods.

Step By Step Guide On Stdin And Stdout In Java :-

The ‘system.in’ is the standard input and ‘system.out’ is the standard output.

While creating scanner object you need to pass standard input, then only we can ask user to enter input, same as for output.

In main class ‘Sinout_prgm’ we given main method definition. Here we defined scanner object ‘sc’ and using println() method we displayed text ‘Enter stdin’.

Then we collected integer by binding ‘nextInt’ with scanner object and returned user input to variable ‘a’. After finishing use of scanner object you have to close them. Finally we printed user entered value to the terminal.

//Stdin & Stdout Example
package java_prgms;
import java.util.*;
class Sinout_prgm
{
 public static void main(String args[])
    {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Input");
     int a=sc.nextInt();
       sc.close();
      System.out.println("User Entered Integer Value is "+a);
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class ‘Sinout_prgm’ we gave public static void main method definition. Within main method we created scanner object ‘sc’ with the help of standard input ‘system.in’. To notify user we displayed message ‘Enter stdin’ by standard output of println() method.
  3. With the help of ‘nextInt()’ method by binding this line with scanner object ‘sc’ and then stored user entered input to the variable ‘a’.
  4. By using ‘close()’ method we closing created scanner object after completed its necessary detail. Then at last we displayed result at user input with string ‘User Entered Integer Value is’.
  5. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  6. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion, now we are able to know stdin and stdout methods using java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Sinout_prgm.java’.

After successful compilation you need to execute ‘java Sinout_prgm‘ line for gets the output.

Then you can see message ' Enter stdin', now user needs to enter input for instance user entered ‘12’ value. At last you will get output ‘User Entered Integer Value is 12’.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on stdin and stdout in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪