Input and Output in Java

Input and Output in Java

In Java, to run a program One thing is the very important and main thing in the whole code file - Objects. These objects store data and provide methods for accessing and modifying the data. Every object is an instance of a class, it defines the type of the object. ( These concepts are to be covered in Object oriented programming section. )

To avoid confusion, we just consider a few keywords from the syntax, which can help you to go further to write code and also we cover the missed topics in the Object Oriented programming part. Because most of the Java concepts are at the OOP level itself.

Syntax of Java:

This is the syntax of Java programming, let's try to understand a few concepts that are important to know before you code.

  1. In Java, everything is in objects, to utilize the objects we need to have classes also. (Don't worry, we cover objects and classes in OOP)

  2. There is a thumb rule in Java, the file name and the main class name should be the same. In the above example, the name of the class is Universe, so the file name also would be Universe. Moreover, the first letter in a class name should be in capitals.

  3. For now, please just ignore the public static void main (String [ ] args )

  4. The important line of code is System.out.prinln();, this is used to print something on the output terminal. We can also use print() instead of println(), but while printing the statements in the terminal, it just prints all the characters or elements in a single line. You'll get to know the usage of it in the Controls section in this series of blogs itself.

  5. To print a variable value in output, we just put the variable name in the System.out.println( <variable name> ); and then we get to display the value in the output terminal.

  6. For example, to print a sentence we must put those string characters ( sentences ) in double inverted quotes, as shown in the image. These quotes help the compiler distinguish between the variable name or built-in keyword and the sentence you need to print.

  7. In case you are required to print a statement that includes the value of the variable, and both need to be printed in a single line, then we use the technique called " Concatenation ".

    Concatenation:

    The primary operation for combining strings is called concatenation. While we are printing two different statements in the System.out.println( ); the compiler converts this variable type into the default string. So that all the statements and values can be easily printed in the terminal.

    Strings = " Hello " + " World " ;

    System.out.println( Strings);

    The output would be - Hello World that's printed on a single line.

    int first = 12;

    System.out.println(" Hello, it's about " + first + " ' O clock");

    The output would be - Hello, it's about 12 ' O clock

    In this way, we can concatenate things in java and print them together.

    Output:

    To take input from the user, we need to import a class from the built-in library of Java called " Scanner class"

    This scanner class enables us to take input from the user. We had imported a class, so now we need to create an object to store all the values entered by the user.

    Scanner <object name> = new Scanner (System.in);

    here, new is used to create memory using objects. Your object name is the reference variable for all the values to be stored in the program memory.

    Example:

    import java.util.Scanner;

    public class Example{ public static void main (String [ ] args ) {

    Scanner scan = new Scanner (System.in);

    int x = scan.nextInt(); // here, nextInt() IS USED TO DECLARE THAT WE ARE GOING TO INPUT ONLY INTEGER VALUE IN THE VARIABLE. WE WILL BE LEARNING HOW TO INPUT ALL OTHER DATA TYPES INTO A VARIABLE.

    System.out.println("You had entered - " +x);

    The output would be - You had entered - 12

    In this way, we can input and output the data in Java programming. IN the next blog, we going to cover the control statements in Java, which helps us to know about some other important topics in java programming. That would be the one-stop solution for you to learn or revise the control statements in java.

Did you find this article valuable?

Support Manjunath Irukulla by becoming a sponsor. Any amount is appreciated!