Input And Output in Java

Input and output (I/O) in java

A statement that is used to program input/output task is called as I/O statement of java is:

  • printf (): – It is used to display the message as well as the value of the variable.

Syntax : –

printf (“ java program”);

  1. out.print (“ Message”):- It is used to display the given message.

Example: –

system.out.print ( “ Java  program ”);

Java program

2. out.print ( variable name ):- It will display the value of the given variable

Example: –

Int x = 10

system.out.print ( x )

10

3. out.print (“ Message “ + variable name ): – It will display both the message and value of the variable at a time.

Example : –

Int x = 10

System.out.print (“ Value of x = “+ x )

Value of x = 10

Here + is called as concatenate operator. It is use to combine the string and non string data together.

  • Println (): – It is used to display the message as well as the value of the variable and place the cursor to the next line.

Syntax: –

  1. out.println ( “Message” )

It will display the given message and place the cursor on the next line.

Example –

system.out.println (“ Java program” )

Java program

_  cursor( next line )

2. out.println ( Variable name )

It will display the value of the variable and place the cursor to the next line.

Example –

Int x = 10

system.out.println ( x )

10

3. out.println (“ Message” + Variable name )

It will display both the message and value of the variable and place the cursor to the next line.

Example –

int x = 10

system.out.println (“ Value of x = “ + x )

Value of x x = 10

 

  • Scanner: – Scanner is the predefined class of Java introduce in the version Java 5.That is used to input the data from the keyboard.

Syntax: –

Scanner < object > = newScanner ( system.in )

It has various method to input different types of data .

  1. Variable = object.nextLine ()

It will input the byte data and store it in the variable.

2.  Variable = object.nextShort ()

It will input the short data and store it input the variable.

3. Variable = object.nextInt ()

It will input the integer value and store it into the variable.

4. Variable = object.nextLong ()

It will input the long data and store it into the variable.

5. Variable = object.nextFloat ()

It will input the float data in store it into the variable.

6. Variable = object.nextDouble ()

It will input the double data and store into the variable.

7. Variable = object.input ()

It will input the string data and store it into the variable.

8. Variable = object.nextLine ()

It will input the string data and store into the variable and the cursor moves to the next line.

Leave a Reply