JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

BufferedReader vs Scanner in Java

BufferedReader vs Scanner in Java

Here is the 5 key differences between the Scanner and BufferedReader class of Java API:

1. Scanner is a much more powerful utility than BufferedReader. It can parse the user input and read int, short, byte, float, long and double apart from String. On the other hand BufferedReader can only read String in Java.

2. BuffredReader has significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from file, you should use BufferedReader but for short input and input other than String, you can use Scanner class.

3. BufferedReader is older than Scanner. It's present in Java from JDK 1.1 onward but Scanner is only introduced in JDK 1.5 release.

4. Scanner uses regular expression to read and parse text input. It can accept custom delimiter and parse text into primitive data type e.g. int, long, short, float or double using nextInt()nextLong()nextShort()nextFloat(), and nextDouble() methods, while BufferedReader  can only read and store String using readLine() method.

5. Another major difference between BufferedReader and Scanner class is that BufferedReader is synchronized while Scanner is not. This means, you cannot share Scanner between multiple threads but you can share the BufferedReader object.

.