History and Development of JAVA: INPUT and OUTPUT STATEMENTS



In our previous article, we've discussed the history and development of Java programming language created by James Arthur Gosling, we've discussed the characteristics of Java, different types of Java programming, Compiler and Interpreter, and we've also gone through functions of JVM, Source Code and Byte Code.

In this article, we are going to learn some of the types of Input and Output statements including Comments and were going to get a sharp intro of BlueJ.

Output statement in Java

The output statement in Java produces a result of any execution on programming. The values to be produced on the screen are enclosed with the braces i.e.(). Over it can also be used to accept a value from the user. 

The output statement in Java programming language can be written in two forms:

  • System.out.print(): When the programmer uses this printing statement then the cursor will remain on the same line without jumping to the next line.
Syntax:System.out.print(<message to be displayed>);


System.out.print("Java is fun");
System.out.print("it follows OOPs");
System.out.print("previously it was called Oak")


Output
Java is fun it follows OOPs previously it was called Oak.

  • System.out.println(): When the programmer uses this printing statement than the statement used to pass the cursor in the next line of the screen after producing the result.
Syntax : System.out.println(<message to be displayed>);

System.out.println("Java is fun");
System.out.println("it follows OOPs");
System.out.println("previously it was called Oak")




Output
Java is fun
it follows OOPs
previously it was called Oak.



Comment statements in Java

Sometimes it becomes difficult for the second person or the user to understand the program written by the programmer. In this case, the programmer use Comments to make the second person understand what the following code is meant to do.
  • //       :used for single line comment
  • /*---*/:used for multi line comment
  • /**--*/:used for a brief document.
Single line comment

This type of commenting can be used when the code is not much complex and can be easily be understood in one line.



Multi-line comment

Sometimes the code needs to be understood in a detailed manner using single-line comment again and again can rush between the code and the comments that's why we use Multi-line comment which makes the user convenient and easy to understand the following code.



Documentation comment

While programming the user may need to attach some text documents in his program, in this condition he has to use documentation comment in order to get his desired results.



Input statements in Java

The program that we write in the editor gives the output in the Terminal Window. The Terminal Window only shows the thing present in the System.out.print() or System.out.println() statement.
The process of accepting values from the user is called INPUT. 
We can get the input using either:-
  1. Parameterized input
  2. Scanner input
  3. PrintWriter input.
Parameterized Input

Parameterized input is the BlueJ's unique method of testing methods with values that we pass to it. BlueJ allows us to accept values that are to be taken as input.

For example, we've to add three numbers using Parameterized input.



After the compilation, a pop-up window occurs



We've to click on void add(int a, int b, int c) option from the menu, which will show dialogue to input the values for the variables 'a', 'b' and 'c'.



Now input the values for a, b, and c. For example, we've taken 12,13 and 14 for a, b, and c respectively, then execute.



See the output has been delivered.

Scanner Input

When we want to specify the user what is to be taken as input that it is the Length and Breadth of a rectangle, Name etc.

Scanner Class

The scanner class is a package in java.util, a class that allows the user to read values of different types. In order to use the Scanner for our program, first, we need to create a Scanner class OBJECT.

Scanner Class Object

First, we've to create an Object for Scanner Class, in order to use the function attached to the Class. We can create an object using the following syntax:-
          Scanner x=new Scanner(System.in);
It shows an object X has been created which scans the screen from some input given from the user. This is represented by System.in.

Once an object is created you can now use the function of the System class to take input from the user.
Let us now take an example of a program that takes two integers as input and find their sum using scanner class.



Notice that since the scanner object is named as "x", whenever there is an input we need to use "x.nextInt()"
          a=x.nextInt();
          b=x.nextInt();
By doing this a and b are being scanned by the screen for the input of values inside the variable a and b respectively. Thus when you execute the program it will show the following output on the terminal window.



Now you have to input two integers for which the program will add both of them.



After the execution of the whole program, you will get the result.

PrintWriter Class

The System.out.println() statement recommend displaying the output on the screen in JDK 1.5 version. Now we are going to discuss another powerful tool which can be used for writing to the console when using Java through the Print Writer Class the PrintWriter is a character-based class using a character base class for the console output makes your program more productive and standard.

Creating PrintWriter Object
PrintWriter x= new PrintWriter(System.out, true);

Printwriter uses various constructors. We are going to see it now.
PrintWriter(OutputStream output, 
boolean flushIfNewline)

Here, the output is an Object of OutputStream type and flushIfNewline controls the java system to flush or clear the output stream object whenever a new line character is output. To flush automatically flush new line is applied true if false flushing is not automatic. 

This class allows the function "print()" and "println()" for all type including objects. Thus you can now use this function in the same manner as they have been used by the 
"System. out". If an argument is not a simple type, the PrintWriter function calls the object to String() function and then print the result. 

To write to the console by using PrintWriter, specify System.out for the output stream and flush the stream after each new line. 

Java programming with BlueJ

BlueJ is a free Java environment which was created in Monash University, Australia. It is a windows-based platform for the Java Development Kit(JDK). BlueJ can be freely downloaded from the website www.bluej.org.




Features of BlueJ
  • The compilation and execution process in comparatively easier than JDK version 1.3.
  • Blue is user friendly, it is based on a menu-driven approach.
  • To increase the efficiency level of a programmer a sample program appears on the screen, as soon as a class is defined.
  •  After compilation BlueJ shows the error if any present in the program or else it displays the message "No Syntax error"

The first step to Blue

BlueJ is a menu-driven based program on windows, where you can write and execute programs.
  • Open Blue.
  • A default project will pop in the dashboard or else you can create your own project.


  • Enter the file name in the space given and click "Create". A Window will open with the project name on the title bar.
  • Create a class by clicking on the new class in the space given.


  • Double click on the folder name to open the class a sample structure of the class will be available on the screen.


  • Write your program in the working area.
  • After completion of your program click on " Compile" button it will display a message "Class compiled-no Syntax error" in cases where there is no Syntax error otherwise debug and again compile the program.
Execution of the program

By following the steps you can execute or get the results of your code.
  • Make a right-click on the folder and select "void main(String args[])" from the drop-down list


  • A calling function will finally appear on the screen then click OK.




Thus the output will appear on the screen





If you like the article then please share it and subscribe to our newsletter to get the latest updates.

Comments

  1. I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon… outdoor trader

    ReplyDelete
  2. We will provide deal reviews, deal coaching, and follow up to ensure you win the deals you can’t afford to lose. javascript courses for beginners

    ReplyDelete
  3. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. Chen Zhi Cambodia

    ReplyDelete
  4. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome! https://chenzhicambodia01.blogspot.com/2021/03/chen-zhi-cambodia.html

    ReplyDelete
  5. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... chen zhi cambodia

    ReplyDelete
  6. I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. https://eatatbillybombers.wixsite.com/business-charity/post/cambodian-skills-development-to-help-lower-unemployment

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. chen zhi net worth

    ReplyDelete

Archive

Contact Form

Send