How to read input from console in Java

How to read Console Input in java

There are 3 different ways to read input from console in java. We can read input from console by using any one of the following Class.

Read Console Input using Scanner Class

Possibly this is the most preferred way to read input from console in java because of it’s simplicity and convenient methods like nextInt(), nextByte(), nextFloat(), nextLine() …etc it has to read targeted inputs. Application of this class is not just limited to reading input from console, it can operate on several types of data sources like String, File, InputStream, Readable, ReadableByteChannel and path.

Best thing is that regular expressions can be used to parse and retrieve tokens from input accordingly

Pros

  • Convenient methods (nextInt(), nextFloat(), nextByte()) to read primitive data types
  • Regular expressions can be used to read custom patterns.

Cons

Scanner class is not safe for multithreaded use since reading methods are not synchronized.

This Class is available to import at java.util.Scanner

Where System.in is Standard input stream which is by default console

Read Console Input using BufferedReader Class

This method was introduced in the JDK 1.0. This is relatively the complex way to read input compared to Scanner class. We have to use Both BufferedReader and InputStreamReader combined to read input from console. You can trade off this little complexity with efficiency.

Pros

This method of reading console input using BufferedReader is efficient.

Cons

Relatively complex code compared to other methods

Following Classes are required to import

java.io.BufferedReader
java.io.InputStreamReader

Read Console Input Using Console Class

The class Console was introduced in java 1.6. It is preferred way, if you plan to read secrets or passwords from command line since it provides the way to mask input through method readPassword(). Console class methods allow you to pass string for prompt as well which supports string formatting syntax. So you can use format specifiers like %s and %d in the format string.

Pros

  • Read password or secret phrases without echoing the entered characters on to console
  • Read and write operations are synchronized
  • String formatting can be used

Cons

  • It doesn’t work in non-interactive environments like IDE and jshell

Here is the complete code which demonstrates reading input from console by using above mentioned 3 ways ReadConsoleInput.java

Difference between JDK, JRE and JVM explained – Java

JDK vs JM vs JRE

If you are new to programming language java, you might be wondering what is difference between JVM, JRE and JDK.  Often many people are confused with these terms, let’s see what  exactly are these.

JDK vs JRE vs JVM

JDK

JDK stands for Java Development Kit it is the subset of SDK which stands for Software Development Kit. JDK is what used by developer to write, compile, test, debug and trouble shoot the (code)programs he is creating in java.  It is comprised of development tools + JRE.

JDK is required for the developer to write code,  compile, test, debug and monitor.

It contains tools to create distributable  jar files, profiling and more. It is nothing but a set of tools .

JRE

JRE stands for Java Runtime Environment. It’s job is to provide runtime environment and libraries to the program which is getting interpreted by JVM. JRE Contains JVM.  Java is  platform independent, infact JRE is what making it possible to compile once and execute anywhere. To execute java one should need JRE. If user wants to run java program he should need JRE installed on his machine but JDK is not necessary.  JRE is available to download at Java SE downloads .

JVM

JVM stands for Java Virtual Machine. It is the critical part, it is part of JRE. If you install JRE, it includes JVM.  If you install JDK for development, which includes JRM which includes JVM.

JVM is the interpreter, which  executes the java (JVM) bytecode. Java source code is compiled into bytecode by java (javac) compiler. These compiled files typically have .class extension.  These .class files can be share across different computers and executed by Java Virtual Machine.

As explained above unlike C/C++,  java  class (compiled/bytecode) files are not directly executed on processor. JVM takes care of hardware specifics and platform. So, we don’t need to worry about recompiling code again and again for each targeted platform and hardware. Java source code if often compiled into .class files and all of those files and resource are aggregated into one file  called jar (Java Archive) to distribute.

 

 

Understand JDK vs JRE vs JVM
Understand JDK vs JRE vs JVM