Podcast Lookup

Java Input & Output Explained | InputStream, System.in, Scanner vs BufferedReader

The Java Developers Hub

  • December 3, 2026
  • 1 h 11 min
  • Episode 36
Episode page
About this episodeInput and Output (I/O) operations are essential in Java programming because they allow applications to receive data, process information…
Input and Output (I/O) operations are essential in Java programming because they allow applications to receive data, process information, and communicate with external sources.In this episode, we take a deep dive into Java I/O, exploring how Java handles user input through different input mechanisms. You'll learn how System.in works, understand the role of InputStreams, and compare two commonly used input classes: Scanner and BufferedReader.Through practical examples, you'll discover how to read user input, process different data types, and choose the right input method based on performance and application requirements.Whether you're a beginner learning Java or preparing for professional software development, this tutorial will strengthen your understanding of Java input handling.What You'll Learn
  • Understanding Input and Output operations in Java.

  • Introduction to Java I/O architecture.

  • Understanding System.in and standard input.

  • What InputStreams are and how they work.

  • Understanding InputStream and InputStreamReader.

  • Reading user input from the keyboard.

  • Using the Scanner class to collect user input.

  • Understanding BufferedReader and how it works.

  • Differences between Scanner and BufferedReader.

  • Comparing input performance and efficiency.

  • Handling strings, integers, and other data types.

  • Understanding character-based and byte-based input.

  • Handling input exceptions and invalid data.

  • Best practices for reading user input in Java.

Who Should Watch?
  • Beginners learning Java programming.

  • Computer science and software engineering students.

  • Developers interested in Java I/O operations.

  • Students practising Java programming exercises.

  • Java developers preparing for technical interviews.

  • Anyone interested in understanding Java input handling and performance.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Input & Output Explained | InputStream, System.in, Scanner vs BufferedReader

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to talk about a very interesting topic.

  2. Speaker 2

    And that is Standard Input Output.

  3. Speaker 1

    Basically, when we were writing Java programs till now, we didn't even talk about it once. That how we will take input from the user in Java. If we want to add value from the user, then how will we do that? And till now we didn't talk about it because if we had understood it till now, then maybe we wouldn't have been able to understand it in depth. Because all the knowledge we have grasped till now will be used in it. So let's go to the screen and you will enjoy it a lot. We will read it in depth, we will not only understand the syntax because you can easily learn syntax from chat.

  4. Speaker 2

    Jbtc or anywhere.

  5. Speaker 1

    We will understand it in depth that how it is used. You will enjoy it a lot. So let's go to the screen quickly. Yes, so we are going to talk about the standard input output in java. If we are writing code till now, then we used to focus on it. Let's say we wrote a code of our java. We didn't take any input from the user from outside. We used to assume that everything will already happen in our program.

  6. Speaker 2

    But this will not happen.

  7. Speaker 1

    Our program will have to contact the external environment at some point. We will have to communicate in some way that some data can be taken from outside and some can be given to the outside. So let's see how we handle these inputs and outputs in Java. First of all, let's see what types of inputs and outputs we have. Types of inputs and outputs. Types means the input or output you are taking. From where you are taking it and from where you are giving it. For example, if you want to take input from the keyboard and output on the console, then we call it console-based. Console input output. Like we used to print on the screen, something used to show on the screen. It shows on the console. If we want to take an input from the keyboard from the user, it also becomes a part of the console. Next, we may want to take an input from the user from the file or output from the file. So, file can be input output. The next one can be your network input output. Now, what is this network input output? If you are taking an input from a user over the internet, which we also call API calls. We will learn all this from Spring Boot. Basically, you can consider it as client-server architecture. If someone is sending a request on the front-end or back-end in the sense of API, then if you are taking an input from there, then we call it network input-output. Apart from this, we can also have one, memory input-output. Suppose you have a memory, a memory buffer, and you are taking input from there, then it means that we can have a lot of multiple input-output sources. But in this lecture, particularly, we are going to focus on console input output. How we print things on the screen, how we use things on the keyboard, etc. We will study file input output in a separate video. And network input output, you know, when we are studying Spring Boot, then how you handle network calls, API calls, etc. You will understand all that there. So, let's start with our console input output. Console input output. I will write it here.

  8. Speaker 2

    Okay. First of all, let's talk about the output.

  9. Speaker 1

    If we had to print anything on the screen, then our syntax would be system.out.println. Right? We would write something like this. And here we would write whatever we had to print. Let's say we have to print hello. So we wrote a string hello here in double quotations. And we said our screen output would be hello. Exactly when we would run it, our output would be hello. We know this, but we haven't understood how this works internally. So now that time has come. First of all, we understand it from the first thought principle. What will this println be? Now look carefully, it has parentheses and it is taking an argument here. So this will be a function only. And what will it do? It will take a string argument like hello and as soon as it gets called, it will have some code written in it, some code of println which will print hello on our screen, on our console. Now we don't know how that code is written, but I am assuming that I am calling a function here, println.

  10. Speaker 2

    Very good.

  11. Speaker 1

    But we know that whenever we call a method or a function, then it can be done through an object. Right? I mean, if I have an object named student, then S1 is its reference variable and here I had written new student. Now if I If I want to call a method on this S1, then I will write S1 dot something, let's say print.

  12. Speaker 2

    Something like this, right? So there should be an object of this print as well.

  13. Speaker 1

    It is written here, out. See, it starts with a small letter, so obviously there will be an object. So out dot println means that we have a reference variable named out. We are calling the println method on it. Now out must be pointing to some object. We will see which object it points to. Now what is this system? Now obviously we understand that the system is starting with a capital letter So obviously the system must have a class So basically what will happen is that there must be a class system in memory And what we have written here is system.out We know that whenever we write className, dotOperator, or anything else, then basically whatever that is, like in this case, there is an out variable, it must have been statically declared, that's why we are accessing it from the className. What I mean to say is that in our class system, there will be a variable named out. What type of variable will it be? That means, whose object will it be? Who will it refer to? We don't know that yet, we will go and see that now. But here it must come, whatever name that variable will be. Let's say for now, I'll leave it.

  14. Speaker 2

    As x out.

  15. Speaker 1

    What is x? It can be any user-defined data type that we don't know what it is yet. But I understood that in the system class, there will be a variable named out. And I also understood that it will be statically defined. So, a static keyword will also be written here. That's why we are able to write system.out. So what did we do? We have to access the variable called class. And we know that this variable is a reference variable. And it has a method named println. Now think like this, I said that out is a static variable. And out is a reference variable also, right? Because that's why we are able to do out.println. So out must be referring to an object. Which object must it refer to?

  16. Speaker 2

    Of x type.

  17. Speaker 1

    So now we will see what x is in the code. But I am saying that here we must have a class x also. Something like this.

  18. Speaker 2

    Right?

  19. Speaker 1

    And let's say a void in it. It will be a method named println. Something like this. Something must be happening inside. So now I understand what I am trying to do. Basically, now I have two classes. Basically, I have an x class. We don't know what x is right now. It has a method named printTalent. And I have a system class. And I have made an object of this x, which I call out. See, x out. And where have I put it statically? In the system class. So now, how can I access this printTalent method? First of all, I write the name of my class, which is system. dot out means out is my static variable I have done dot out so what did I get? I got the object of this class now I have to access the next println so in this out dot I will do println And whatever I have to print, I pass it and my work is done So I understood how system.out.println works There is a static variable out in the system class Which is an object of some other class, we have considered that class as x And there is a method named println in it So I got the object system.out.println, I called that method And whatever I had to print on the screen, I passed it in the parameter Very good So now we know how the entire output goes to the screen. Now let's go to the code and see which class is the x that I am talking about. So first of all I have made my own project named demo.java. So here I write system.out.println and let's say we print hello on the screen. We have seen this before, we used to run it and our screen used to print hello, no problem. Now I just have to understand that this system is my class and it has a reference variable named out and I know that it is static because I have referred it with the class name. Now I just have to understand that this reference variable named out, which object will it refer to? Basically what will be its class? So first of all, what did I do? I brought my mouse cursor on the system, I pressed command on the Mac and right clicked and I reached this system.java class. This is an internal class that Java has written by itself, right? So this is a very big class, there will be many methods in it. If you scroll through it, you will find a lot of things. But we don't have to explore the whole class, it will be very complicated. We have to go towards out, so I search out here. So as soon as I searched out, I reached here. Now let's explore it once. See what is written here. Public static final print stream out equal to null.

  20. Speaker 2

    Okay.

  21. Speaker 1

    Here out is declared which I said is a reference variable. And I said it is of x type. Now we got to know that the value of x is print stream. So there must be a class named print stream. Whose out is a reference variable. And I have declared it static and final. We know static so that we can access the class name.

  22. Speaker 2

    And final so that we can't change it.

  23. Speaker 1

    Okay. There must be some logic and public so that we can access it publicly. So out comes to refer to a class named printStream. If we go inside printStream, then we will get to know that there is a proper class named printStream inside java which is already a big class. We don't have to explore the whole class. We just had to understand that what type does our out refer to? So now we understood that if I write it here that system is a class in which printStream There is an object named class called out And the print stream class There must be a method called println in it. We can see what will be the method of println in printstream. If I hover over println and right click, then I directly reached the method of println of printstream.java. It takes a string in the argument and will print it somewhere. Now how it will print, what logic it is doing from inside, we don't have to understand that. We just have to understand that there is a method called println. It takes a string and prints something on the screen, on the console, whatever we give it. Okay. So, we completely understood what system.out.println means. Okay. So now we understood the meaning of this line Now let's talk about the system class So what I said is that the system is a class So obviously in Java, the class is made up of the system Which we had seen, right?

  24. Speaker 2

    In that, what was there?

  25. Speaker 1

    This out name was our reference variable Which was of what type? Of print talent type So basically if we go back to our code So we saw the x Now we can replace this x using print stream This was our print stream name Object.

  26. Speaker 2

    Okay, very good.

  27. Speaker 1

    Now, let's explore a little more that how did we get access to our system class? Did we write something special in the code? Did we import something? We didn't import, right? We wrote system.out.printl and hello on our screen. Hello got printed. So, how did our JVM, our Java compiler find out that where is the code of this system class coming from? So, basically, let's understand it that where is our system class declared? It is declared in our java.lang package. This is called java.language. This is an internal package of java that java has already written in java.lang package. So our system class is declared there. Now this java.lang package is always imported by default. Now whenever you are writing some code, suppose this is your file by doing demo.java. You have written some code in this. So java compiler internally will import here. What will it import?. langPackage will automatically be imported. And what does import mean? All the classes in it, like System class, its code will also be referred here. So basically whenever we call the system, whenever we write system.out in our code, our Java compiler will know which class this system is. It won't say that I don't recognize this class. It recognizes it because java.langPackage is automatically imported. Now the next thing we saw, this out object, This is a special class object which we call printStream.

  28. Speaker 2

    printStream.

  29. Speaker 1

    Okay, what class is this? Look, printStream is obviously a class which prints the output on the screen. There was a method called println inside this.

  30. Speaker 2

    Whatever statement we give, it prints it on the screen.

  31. Speaker 1

    Okay, in this printStream class, if we go inside and see, we will get another method whose name will be just print.

  32. Speaker 2

    What is the difference between this println and println? There is a small difference.

  33. Speaker 1

    Whenever it prints on the screen, it attaches a new line with a new line. And this is our without new line. So if you write aditya-tunnel, then what will println do? Aditya-tunnel will print on the screen.

  34. Speaker 2

    Turned on. This is printed.

  35. Speaker 1

    Okay?

  36. Speaker 2

    Okay.

  37. Speaker 1

    And then it will bring its cursor on a new line. So that whenever we have to print something next, it will print on a new line. We already know that. And what does print do? It keeps printing on the same line. This is just a small difference. Apart from this, there is another function in the third one. Which we call printf.

  38. Speaker 2

    Printf.

  39. Speaker 1

    Okay?

  40. Speaker 2

    Now what is the work of printf? Nothing.

  41. Speaker 1

    Whatever it is, if we want to print a formatted output on the screen, then we use printf. So basically, what does it give? It gives us formatted output in a particular format. We'll see when we get the use case. I guess we've used it in one lecture where we showed how Java stores numbers internally. We used printf there to print it up to 20 digits of a big decimal place.

  42. Speaker 2

    Right?

  43. Speaker 1

    So, we'll see if there's any formatting. So, it means that printstream has multiple methods. There's println, printf, print. Which prints on the output screen in different ways. If we go to printf, we'll find all these methods there.

  44. Speaker 2

    Okay?

  45. Speaker 1

    Now one question may come to your mind that java said that the out reference variable in the system is static. What if it is not static at all? Is it possible that java says that it is not static at all? Absolutely it is possible. If this is the case, then you can think what would have happened. If we had to print something on the screen and basically what we have to say is that we have a class system. This is a class in java. Now what happens is that our output variable is static. So it is something like this. PrintStream refers to PrintStream and its name is out. Something like this. What if it wasn't static? No problem, if it wasn't static, what would we do? We would have to make an object of system. So we would write something like this. Let's say system s equal to new system. We already know this. And then through this s, we can call that out. We write s.out. Once we get out, we can put a println function on it.

  46. Speaker 2

    Out.println.

  47. Speaker 1

    And here whatever we want to print, something hello, like this. So basically our code changes a bit. First we have to get the object of the system. Then we have to do s.out.println. But we don't have to do all this. So Java has given us a static variable out in the system. Very good. Now if we go back to our code, If we go back to our system class, we can see that there is a static variable called out defined in our system class. Apart from this out, which is the object of the print stream, we have another variable defined which we call err.

  48. Speaker 2

    Now see, here is err. This is also a print stream type object. It is final declared, static declared, and public.

  49. Speaker 1

    It is exactly the same, but its name is different.

  50. Speaker 2

    Its name is ERR.

  51. Speaker 1

    In fact, we can write it like this in our class. System.ERR.println If you think that ERR is also a print stream object, then you must also have a function called println, right? So I can print something here too. Let's say bye. And I'll fix this once. Now if I print this on the screen and see, So, of course, my buy is also printed.

  52. Speaker 2

    Very good.

  53. Speaker 1

    Now, if I have a variable named out, of print stream type object, Why did java give me another object named err which is a print stream type object. That means it also has all the println, printf and print functions. See, what does err generally mean? Let me write it here. Err generally means error. And out generally means, the upper variable out means output. So java says, I have given you two objects, one is err and the other is out. If you want to print any normal business logic on the screen, then use out. If you want to print an error, then in that case you.

  54. Speaker 2

    Can use ERR.

  55. Speaker 1

    Why is that?

  56. Speaker 2

    Because of this, there is a separation of concern.

  57. Speaker 1

    I would know whenever there is an ERR in my code, that means it is an error. Whenever there is an OUT in my case, then it is not an error, it is a normal logic.

  58. Speaker 2

    So Java says this.

  59. Speaker 1

    But obviously, when we are writing code by ourselves, we write system.out.println in everything.

  60. Speaker 2

    Even if it is an error. But what is a good practice?

  61. Speaker 1

    We are making an app in which we have to test if someone is of legal age to vote or not. So this will be int age. Assume this comes from a user. So if we check whether someone has entered negative age. If age is less than zero.

  62. Speaker 2

    So you know what this is.

  63. Speaker 1

    This is an exceptional case. This is a wrong case. This should never happen. So if this happens in any case. So what you can do here is system.out.print. Sorry not out. Err.println. Let's say invalid age. Something like this.

  64. Speaker 2

    Okay. This will tell.

  65. Speaker 1

    Us that the message we have given here is an error message. In that case, if it is normal, then you can do system.out here. Here error is coming because we have not declared it as age. Let's say I have given minus 3 here.

  66. Speaker 2

    Okay.

  67. Speaker 1

    So in that case, we will enter this if and our print will be invalid age. In this case, our error is helpful. In normal case, our out is helpful.

  68. Speaker 2

    Obviously, there is no rule.

  69. Speaker 1

    If you want, you can put out in both the cases. If you want, you can put error in both the cases. But Java has given you a chance to do this. The second advantage of this is that ERR tells the operating system that this is a different type of error message.

  70. Speaker 2

    So if we want, we can properly log it.

  71. Speaker 1

    We can show it from the red line so that all the errors come from the red line. So basically when we see a log file, we will see all the error messages on the red line.

  72. Speaker 2

    So this is very useful there.

  73. Speaker 1

    Although when we make a modern application, we are not printing any input output on the console. We use a proper log file. And again, we have different libraries that give us logs. But anyway, if we are writing our own code in pure Java, then this.

  74. Speaker 2

    Is very useful.

  75. Speaker 1

    Okay, we understood very well how we give output on the screen. Till now, we had written system.out.printer, but now we understood its real meaning.

  76. Speaker 2

    Now, let's move ahead.

  77. Speaker 1

    And let's talk about our main question. How to take input from user? How to take input from user? So now we are going to talk about input properly. Earlier we talked about output.

  78. Speaker 2

    Okay.

  79. Speaker 1

    Now before talking about input, I want to tell you one thing. What Java has done is that the console input and output methods and their libraries have been made very complicated. So the first time you read Java, you get very overwhelmed. Do we have to memorize so much syntax to get some input from the screen or the user? See, there is a specific reason for this. When Java was invented, it was done with the purpose that Java will be either a server language that will run on our backend, right? So from where will it take the input? It will take the input from the network call, which I told you, from the API call, right? Which we get in JSON these days, or sometimes it was also found in XML, right? Apart from this, it used to say that either I will take my graphic user interface, or I will take my input.

  80. Speaker 2

    For example, I made a form, okay?

  81. Speaker 1

    In that form, enter some username, enter some password, whatever it will enter here, I will take the value from there, okay? So basically, it has never kept itself in such a way that it will remain a console language. But obviously, every language should know how to take input and output from the console. So, in the very beginning, it didn't provide us with a lot of libraries by itself. So, we have to go to a very low level to understand it. Now, because Java has become very advanced, we have a clean interface of Java. input. But we will start our entire journey very primitively, what Java used to say in the beginning, because if you understand that, then only the latest class of today, which we call scanner class, those who have heard, they will know. How did we reach the scanner class? Because if you don't know this, then you won't be able to understand the scanner. Because the scanner is still using the same stuff from inside.

  82. Speaker 2

    So, okay.

  83. Speaker 1

    So, this is the story. Now, let's go to Java's input. Now, look. I had said that we have a class in Java, which we call system class. Okay. In that class, there was a variable called out, which was of print stream type.

  84. Speaker 2

    Print stream out.

  85. Speaker 1

    Apart from this, there was a variable called print stream, The variable declared as R was ERR. Now, these two were for our output, for the output on the screen.

  86. Speaker 2

    So, is there any input in this system class?

  87. Speaker 1

    Or, I mean, I want to say that such an object IN, if OUT is for output, then there should be an IN for input. And what class type would that be? So, let's go back to the code and see if there is anything like this. So, look, we have come back to our code and we go to our system class and search IN here.

  88. Speaker 2

    So as soon as we searched, we got a variable named.

  89. Speaker 1

    IN which is an input stream type object and this is also final, static, public, all are declared. So here it means that there is an object named IN which can take input from the user. But yes, this is not a print stream type, it is an input stream type. Okay, what is an input stream? Okay, this is also our proper class. So now let's understand this a little bit. So now we have seen that our system class In this, a variable named in is defined which is static as well. So we can call this as system.in. And what type of object is this? This is an object of a class named inputStream. These two were of printStream and this is our inputStream. So I will write this as inputStream. So System.in is an object named inputStream Just like System.out was an object named printStream Whenever we used to write System.out What did we get?

  90. Speaker 2

    What was out?

  91. Speaker 1

    It was an object named printStream Now we used to call a method named. println To print something on the screen Similarly, if we write System.in Whose object is this? This is an object named inputStream If we will do a dot in this too And we will have a method To take some input from the user Yes, of course The name of the method is read This readRAM method is useful to take input from the user. Okay, let's move on to this for a while. Before that, I want to talk to you about streams. Whenever we talk about input and output, we will be talking about streams a lot. So let's first understand what streams mean. If it is said in very simple words, then stream means the flow of data.

  92. Speaker 2

    So I will write it as flow of data.

  93. Speaker 1

    If you imagine that you have a pipe and somehow data is flowing from it. So you will call it stream of data. Like if water is flowing from here, then we call it stream of water. So you assume that this is the data that is flowing from this pipe. You can visualize it like this. Stream means flow of data. Now we have two types of stream. One is our input stream. What does this mean? This means that data will flow inside our program. So data flow into program. Okay? Obviously, you can understand it from the name. And the second one we have is output stream, which is completely opposite to this.

  94. Speaker 2

    Here, what will happen? Data flow will be outside the program.

  95. Speaker 1

    Okay?

  96. Speaker 2

    So, I will write data flow out of program.

  97. Speaker 1

    Input stream means data is flowing inside the program and output stream means data is flowing out of the program. So into the program means we are taking input from the user. Out of the program means we are giving some output to the user. Let's say we are showing an output on the screen. So this is the output. If we carefully define the input stream and output stream, then these two things These are classes of Java. What are these? Java classes. Okay.

  98. Speaker 2

    If I speak specifically, then both of them are abstract classes. Okay. Both of them are abstract classes.

  99. Speaker 1

    It tells how to take input and how to give output. Now if both of them are abstract classes. First of all, why are they abstract? They are abstract because it tells that read and write should be there. Read means you can read the input from the user and write means you can give the output to the user. But it won't tell how it is implemented. What does it mean? It won't tell where to take the input or where to give it. For example, what did we read in the starting? That we can take input-output from the file, we can take input-output from the console, we can take input-output from the network. There are many ways to take input-output. But our two classes, our input stream and output stream, will not tell us where to take it from, from the file, from the console, from the network. Because what will it say? We are giving you two functions. Input stream will give you, let's say, read. And output stream will give you, let's say, write. He will just declare it and leave it as we have read in abstraction. Now who will be responsible for defining them? Their child classes. One child class will be made for file, one for console, one for network, one for memory, etc. I hope you have understood why our input stream and output stream are abstract. Okay. So let's go back. We have seen three things till now. One, we have seen dot in in system class, a reference variable which is of input stream type. One, we have seen system dot out which is of print stream type. And one, we have seen system dot ERR which is again a print stream. So we can say that system.in is used to take input from the user. So this is an input stream. And these two are used to give output to the screen. So this is of type output stream. So this will be the type of input stream. Our system.in and these two will be the output stream. Output stream means the stream is going out. The data from our program is flowing out. And you know the input. Our data is flowing inside the program. Okay.

  100. Speaker 2

    But we saw system.out and system.air.

  101. Speaker 1

    These two are the type of print stream. Remember we saw above. Their type is print stream. See this.

  102. Speaker 2

    But here we are saying these two are the type of output stream. Obviously, what did I say?

  103. Speaker 1

    What is output stream? It is an abstract class. That means somewhere, somewhere, somewhere, Print stream. It won't have a child class. It won't have a hierarchy. Output stream will come up. Print stream will have a subtype. Which will tell you how to give output on console. So I told you these are abstract classes. They have read and write method. One has read and one has write.

  104. Speaker 2

    Which they just declare.

  105. Speaker 1

    Child classes will be responsible for defining. So print stream will be a class. Which will be a output stream subclass. It will tell you how to define write. And how it will define write. It will tell you that if we have to show output on console. Then how will we show that. Okay, we understood this hierarchy. So why did we read this? Why did we see what a stream is? We read this because the input and output of java is stream-based. Now, because of stream, Java can read data from multiple sources. Now, what did I say? It can read data from console, file, memory, network, etc. But it can read data from different sources using streams. As I said, we already have a inputStream and outputStream parent abstract classes. InputStream has a method of read and outputStream has a method of write. Now they just declare it because they don't want to show the output or take the input. They want input and output. Now if we want to show it from a file, then there must be a particular class of a file, of a console or of a network.

  106. Speaker 2

    Right?

  107. Speaker 1

    So we got to know that our data moves in streams, and the entire input and output of java is stream-based. Okay? So now you can take the input from anywhere, from a console, from a file, from a memory, from a network, we take all those inputs in streams, and those streams, we call all those streams, all of them are stream of bytes. Meaning, they are streams of bytes. What does the byte stream mean? If you imagine your pipeline, let's say the data is flowing in it. Now, let's say the data is flowing in the form of bytes. You know, one byte has 8 bits. So, let's say this is one byte, this is the second byte, this is the third byte and the data is coming from here and the data is flowing. So, what will you say after seeing this? By seeing this, I know that there is a stream and this is my This is my one byte, this is my one byte.

  108. Speaker 2

    This is my one byte. Why is it so big?

  109. Speaker 1

    Because 8 bits are stored in it. So, my data is flowing in bytes. Now, let's go back a little bit. And the two classes that we saw above, one we saw as input stream. In which we said data moves inside the program. And one we saw as output stream. Data moves away from the program. Or outside the program. Let's see their hierarchy once. I said these are abstract classes. So, this is also abstract. And this is also abstract. So, let me first make its hierarchy. So, look. What I did here is that I wrote the hierarchy of both of them completely. That we had an input stream and an output stream. And these four are all their classes. One is file input stream, byte array input stream, buffered input stream, data input stream. And their respective output classes. File output, byte array output, buffered output. And this is a little different.

  110. Speaker 2

    Print stream. Okay. Very good.

  111. Speaker 1

    Now, obviously, we don't need to go into every class. It matters to us in the console. Right? Just we should understand this. the file input stream do? It talks about the input in the file. We know that there was a method in the input stream, read, which it only declared, because it was our abstract class. Now the responsibility of defining this read will be of all of them, that is, it will have its own read function, which it will define, it will have its own read function, which it will define, and so on.

  112. Speaker 2

    Now, how do we define file input streams?

  113. Speaker 1

    If we want to take input from a file, then how do we read it? Byte error input stream, buffered input stream, and data input stream tell us something else. We don't want to go into their depth, but we want to see how input stream tells us how to read data. And our system.in object in the system class, which is a reference variable of the name in, what type was it?

  114. Speaker 2

    It was also of the name inputStream.

  115. Speaker 1

    Now you will say, if system.in is of the inputStream type, it is abstract itself, then what is its specific object? Means we will write it like this, inputStream in equal to new What? There will be a concrete class here. Now which is the concrete class? The answer is, the concrete class is bufferedInputStream. Well, you don't need to know this much. But yes, you can assume that bufferedInputStream is the concrete class of system.in. But we will write it as inputStream. Because it is its parent class.

  116. Speaker 2

    Okay, very good.

  117. Speaker 1

    Now let's do the same in outputStream. Here we used to have a method write. which was only declared by this. Again, who was responsible for defining it? It was their child classes. So again, all these child classes define it in their own way. File output stream must know how to write in file, byte array output stream must know how to write in bytes, etc. Here, our main class is printStream. Because we know that our system.out object or system.err object.

  118. Speaker 2

    What type were they? They were of our print stream type.

  119. Speaker 1

    Right? That means it tells how to write output on console. So the right one will tell that I know how to write on console.

  120. Speaker 2

    How to write output.

  121. Speaker 1

    Okay? Now you'll say, but to print, it calls println, right? Or printf. It calls some methods like this. You're saying it overwrites write. Again, if you go internally, and if you open println, then it'll call some method, you'll open it, it'll call some method, and finally you'll get write. Okay? You'll definitely get this internal method, because it'll have to overwrite write. Okay?

  122. Speaker 2

    Why?

  123. Speaker 1

    Because output stream says, I'm giving you write access, write will have to be done by everyone. So this is very internal but overall if you want to know InputStream gives read access and outputStream gives write access Rest all our classes talk about how they overwrite read and write To read and write data from different places And you understood what are streams Data flow is called stream And we divide it into two parts InputStream and OutputStream That's why Java made two classes for input and output Rest all are their subclasses I hope you understood this Now you must have understood that system.in means that in is an object. What type of object is it? It is the object of our input stream. Which is statically declared in the system class. Now we had said that this input stream can read data from anywhere. From file, console, network etc. But by default our system.in object is made to read the data from the keyboard. So by default, it is attached to the keyboard. If the input stream is an abstract class, then its concrete class would be buffered input stream. It would be from inside. We don't need to know that. We just need to understand that whenever we write system.in, from inside, we are focusing on our keyboard because from there, we will take the input. You can also change it. You can say that I don't want to take input from the keyboard. Let's say I want to take input from a file, etc. So you can do all this. But by default, it takes input from the keyboard. Now let's understand the flow of the input. If you understood the story, then things will be clear now. We know that in this input stream, we have a method called read, which will try to read the data from the keyboard. And we also know how it reads, stream of bytes. So we know that it reads the information in the form of a stream.

  124. Speaker 2

    And which stream?

  125. Speaker 1

    Stream of bytes. Okay, very good. Now it will happen that I will give some input on the screen. Let's say I gave input A. How will I give input? I will press A on the keyboard. Now internally, the keyboard will give me the value of this A. So you know what the ASCII value is, we discussed that we assign a number to every character. Unicode is more complex because it can assign a number to every character in the world and ASCII value can only be assigned to English alphabets and digits. So the ASCII value is 65. So the keyboard will give me 65 internally in my system. So you can assume that my 65 Now because I said that data comes in streams, then my 65 will be coming in one byte of stream. Now what will happen in this? In this, I will have a binary of 65, whatever it is. Okay, but let's do one thing for now. I will write it as simple 65. Assume that here is a binary of 65. But in 65, our data will be coming in the stream. This is our input stream. And this read method will read this stream. So basically, if we look at it, our code will look something like this. If we were printing something, how would we write it? System.out.println Something like this. Now if we want to take some input from the user from the keyboard, then obviously we will need system.in. This is the object of the input stream. Now, what did we say in this? There is a method called read. What will it do? Whatever you type on the keyboard, it will try to read it. How will it read? I said in stream of bytes. So, let's go to the code and see how it looks.

  126. Speaker 2

    So, what did I say?

  127. Speaker 1

    I will write system.in.read What will it do? Whatever we enter on the keyboard, it will try to read it. Now, there is an error here. And let me tell you why that error is coming. For that, We have to import something here. Import java.io.ioexception Now why do we have to import this exception? Because our system.in.read can throw this exception. Now we don't have exception handling so you can ignore this code. Throws ioException, for now I am writing Throws ioException. You can assume that we have to write this code like a black box so that we don't get any error. When we read exception handling, you will understand what it means.

  128. Speaker 2

    Now what we have.

  129. Speaker 1

    Done is system.int.read If we go and see, what is this read giving us? It is giving us a value of int type. So let's say we write int x equal to system.int.read And we print system.out.println Whatever it has read, let's say x Something like this. So it will read something from here. It will give it to this int and we will print it. But what if we don't give a number but give a character. Like we said A, B, C something like this. So can't we take it as a character? Can't we take it as charX? We can't take it, it will be an error.

  130. Speaker 2

    Why?

  131. Speaker 1

    Because its return type is int. See, if I go, then type mismatch cannot convert from int to char. It can only give integer value. Why does it give integer? We just told you. So if I write a on the screen, then keyboard will give me 65 in its ask value. And how will this 65 come? In binary form. So I will write binary form of 65, binary form of 65. It will be in the stream. And our read will read that stream and sign x. Okay. So let's run it once and see. As soon as I run the code, it is asking me for some input. So let's say I input A. My answer is 65. Okay, very good.

  132. Speaker 2

    But this is a problem. I don't need 65.

  133. Speaker 1

    I need A. I took input A, so I need A output. But I can't find the character here. So what should I do? But I can typecast. What if I typecast in the character? So there is no problem. I didn't get an error. Now if I run it, let's say I gave A.

  134. Speaker 2

    A output came. Very good.

  135. Speaker 1

    So from here, I can take input from the user in this way, right? What is the problem? The problem is that what if I want to print aditya, but the print is only A.

  136. Speaker 2

    Why is that?

  137. Speaker 1

    Look, let's understand this. This was our code. We read system.in.in.in.in.in.in.in.in.in.in.in.in.in.in.in.in.in.in.in. It reads only one byte at a time. So again, our read is one byte at a time. I have written system.in.read only once. So what happened? I gave Aditya to print on the screen. What did it do? System.in.read read only one byte. It got only A in one byte. So it printed A on the screen. And what happened to the rest of the D, I, T, Y, A? These are our was inside the input buffer. What is an input buffer? We haven't talked about this. An input buffer is nothing. When you write your name on a keyboard, let's say you wrote A, then D, then I, then D, then Y, then A. Internally, the keyboard makes a buffer. You can think of a buffer as an array. It makes A, then D, then I, then D, then Y, and then A. And then comes the slash end. We didn't talk about it but slash end means new line. So if you ever want to print a new line, you print slash end. In fact, you can also print slash t for new tab space. Basically, your tab space will come. Similarly, we call it escape sequences. There are a lot of them. Whenever they are used, we will see them. But generally, slash end means new line. So as soon as I wrote A, D, I, T, Y, A, after that I said does enter mean, we always come to a new line, so here slash n came, so this is our buffer, But you know that these are characters. Internally, our stream is a stream of bytes. So this information will be converted in our bytes. So what we will do is, we will write the ASCII code of A here. We will write the ASCII code of D here. And so on. Let me write the ASCII code of everyone. So see, I have written the ASCII code of everyone here. So internally, we will have a buffer like this. Now from this buffer, when system.in.read If we call system.in.read, it will read the first byte and give us a or 65 on the screen. This means if I want to read all the bytes, then I have to call system.in.read again and again. I can call it in a loop and my work will be done. That is, my code will look something like this. So, I will write the code like this. First, I will define a string that I have to bring the output. I will take an integer c and run my loop until I see c is not equal to slash n. Because I know that I have to fetch all these values 65, 100, 105, 116 one by one. So, I will see if my c is not equal to slash n. Initially, my c will not be equal. As soon as I did system.in.read, I got 65 from the buffer, I put it in C. Now as soon as I put it in C, there is an integer, I typecast it in the character and concatenate it in the string. So my first string would have been A, right? After that, this loop will run again. This time I will read 100, 100 means D, right? So this time my concatenate will be D. In this way, I will read A, D, I, D, Y, A till the time my slash n comes and my screen will be printed completely. If you want, you can also write it in the code yourself.

  138. Speaker 2

    Okay, very good, brother.

  139. Speaker 1

    So will I have to work so hard to print the entire output? In other languages, like C, C + +, if you talk about C + +, it has C in to take the input. Similarly, we have scanf in C to take the input. If you talk about any other language, it's so simple. Have you written such a complicated code in Java? If I want to take a string input, I have to read each character by going through system.in.read. This is a very complicated and ugly code. So when Java thought that it will be very complicated, Java brought a new type of classes that solved this problem. Before going on that, you must have had a question in your mind when I told you that we read one byte of data, but you will say that the character takes two bytes in Java.

  140. Speaker 2

    Two bytes.

  141. Speaker 1

    If it wants to read A, then it will have to read 2 bytes to get A, but that's not the case. In Java, the character takes 2 bytes because it is in Unicode. Unicode means that it can take all the languages of the world. So to represent that, Java needs 2 bytes. But if we talk about the ASCII value that we input on the keyboard, which can be a normal English character or some special symbols, they come in our 1 byte. If we want to fetch A from Java, we will have to fetch only one byte of information and our work will be done. Now let's move on to our second type that Java said that it will not be possible like this, I have to bring something else so that the normal user can take input easily. After that, the reader class was invented. What did Java do? It created a new type of abstract class, reader. In the same package, in the java.io package, in the input-output package, a different type of class was introduced, which was called the reader class. And a hierarchy was created, which would solve the problem that we had to read the character one by one.

  142. Speaker 2

    And we had to use loops. Okay? First of all, let's see.

  143. Speaker 1

    The hierarchy of the reader. So this is the hierarchy of our reader. Buffered reader, input stream reader and file reader. These are our three concrete classes. Reader is our abstract class. The most interesting thing about reader is that it reads the stream of characters directly. It doesn't read the stream of bytes. The most interesting thing is that we don't need to type cast. It deals with the stream of characters directly. So for this, the stream we were imagining doesn't contain bytes of information. It reads the information in the same way like AD, IT, YA It reads the information in the same way In this, there are two classes BufferedReader and InputStreamReader Both of these are useful for console input and output And you must be understanding fileReader by its name It deals with files So we will study it when we will study file input and output So first of all, let's see BufferedReader Look, we know what was the problem with system.in.read, the problem with our input stream was that it used to read one character at a time.

  144. Speaker 2

    So you think, first it read one character, then it.

  145. Speaker 1

    Read one character, then it read one character, and we have thousands of characters. If we are taking a very big string, then it can have thousands of characters. To read that, we will have to call 1000 OS calls, we will.

  146. Speaker 2

    Have to call the operating system. Why?

  147. Speaker 1

    Because look, what is the normal flow? Whenever we goes to the buffer of the operating system. We saw this above, the input buffer we talked about. And from there, our system.in.read, i.e. the input stream, will read from that in our program. Our program, Java's program.

  148. Speaker 2

    So, what is the flow?

  149. Speaker 1

    First, the operating system went to the buffer and we read from the program one by one. So, if we type A on the keyboard, it goes to our buffer, A comes here, then our program reads A. Then, if I type D again, it goes to our buffer again, then when it reads A, then our program reads D.

  150. Speaker 2

    So, what is it doing?

  151. Speaker 1

    Every time to read a character, it asks the operating system buffer to bring a character and the operating system buffer gives a character to the program. This was happening till now. So if there are 1000 characters, then 1000 operating system calls will be made, which is a very slow process. It will be very slow to fetch one character and we will be making so many calls on the operating system. So what does BufferedReader do? BufferedReader does a very interesting thing that he introduces a buffer. He introduces his own buffer. So it says that I will read each byte one by one, I will keep a certain chunk of data in my own buffer. And where does my buffer exist? In the program memory. That means if the program needs some character, it will take it directly from me to the operating system. Okay, what does this mean? This flow in which keyboard, operating system buffer and program was there, it added one more thing to it that now any character will go to the operating system buffer first. Let's assume this is an operating system buffer. This operating system buffer will read our java buffer in chunks.

  152. Speaker 2

    So, there.

  153. Speaker 1

    Will be a buffer in java, it will read this chunk of data together. And from here, whenever our program needs some input, it will take it from our java buffer. It doesn't need to call the operating system buffer again and again. Because the memory of java buffer is inside the program. So, it is accessing the memory of the program. It's same as whenever we declare a variable like int x equal to 4 So what happens is, in a container called x, 4 gets stored And whenever we need x, we call it, we call system.out.println If I want to print x on the screen, I write x, 4 gets printed there So this 4 that we declared x, this came into the computer's memory, same as the java buffer, this is a part of the program memory So it reads everything in its own memory from the operating system buffer So if let's say So let's say Aditya was read in this buffer, he read Aditya here. Now when our program needed these characters, it can read them directly from here. This is its own memory, this is its own memory. It won't need to call any operating system. This will improve our performance a lot. So our buffered reader that we are talking about is very optimized. Comparatively, our input stream was very slow because it used to call so many operating systems. So here I have mentioned all the points that we understood that read a chunk of memory from operating system buffer, store it in the memory and then give it to the program whenever it is needed. Okay, very good. This means we need to use buffered reader. Now we will not call the read method of our system.in directly. But we also understood that our standard input, whatever is our standard input, Input is of input stream type What was its object? System.in System.in is an InputStream type object And it can grasp any input from our keyboard This is standard, we have studied this So this standard cannot be changed So buffered reader will have to work internally with this only But here comes a problem So the problem is that our buffered reader is not directly compatible with InputStream Okay, why isn't it compatible? Remember, what is Buffered Reader? It's a stream of characters. Stream of characters? And our input stream is stream of bytes. Since these two work on different streams, they are not compatible with each other. We have to make them compatible. We have to use the buffered reader, but it will work with the input stream because it can take the input. The buffer will put a buffer on it so that we can read more chunks at once. But now they are not compatible with each other, we have to make them compatible. We need a class that converts this stream of bytes into a stream of characters. We need this conversion. This class is called InputStreamReader. InputStreamReader is the class which converts stream of bytes into stream of characters so that buffered reader can read them in the buffer. With our operating system buffer and can put them in the java buffer. This class we just saw. Where did we see this class? reader and the input stream reader. I said that we are going to use these two classes to take.

  154. Speaker 2

    Input from the user.

  155. Speaker 1

    So we have seen what the buffered reader will do. It will read the chunk of data from our operating system buffer. And the main work of the input stream reader is to make the compatibility issue of the input stream and buffered reader compatible. So it will somehow convert the stream of bytes to the stream of characters. So that the input stream and buffered reader can work together.

  156. Speaker 2

    Okay.

  157. Speaker 1

    So our input stream reader's job is to convert byte stream to character stream. So let's see how it does it. Before looking at the code, let's understand what will happen. First of all, we will make an object of input stream reader. The object of input stream reader will convert this character stream to byte stream. How will it do it? It will do something like this. We will write input stream reader equal to Let's name the object isr equal to new And again we will write input stream reader And this constructor will take an object named input stream What was the input stream? Our system.in We know what type of object system.in is This is the object of input stream Input stream So it will take an object named inputStream in its constructor And by constructing it, it will make ISR, InputStreamReader And this InputStreamReader, now what it did? It started working in characters, earlier it was working in bytes Now as soon as we got an ISR, InputStreamReader object, what did we get? We got a buffer of characters But now what we have to do is, we have to apply buffering on it Now the bufferedReader will be of use Now what is the work of bufferedReader? Once we get InputStreamReader What did ISR do? It converted stream of bytes into stream of characters. Now buffered reader will apply buffer on it. It will read stream of characters in chunks. How will it do it? Again, what will we do in code? We will make buffered reader object. So we will write buffered reader. Let's say br equal to new. Again buffered reader will come here. Now what will it do? It will take an ISR, an object named input stream in its constructor. So what will it take here? It will take ISR. Something like this. Now the object of buffer reader br has done the buffering work. What has it done? Just like it took ISR in constructor, it has read a huge chunk of data from our operating system buffer and converted it in our java buffer. Now when we want to print something, we have a function br.readline. There is a function called readalign, which we will call and we will get some output. So let's see it properly in code. Yes, so I have taken my demo3 file, we are going to read buffered reader here. So first of all, I import java.io and this time I do io.star.

  158. Speaker 2

    What does star mean?

  159. Speaker 1

    I am telling java that I am not telling which class I have to import. All the classes in the java.io package, import everything. Okay, again I will have to write throws our IOException because it throws the exception. Again, we will understand it when we see exception handling.

  160. Speaker 2

    Okay.

  161. Speaker 1

    So, what I said, first of all, we will make an object of our input stream reader. So, we will make input stream reader, let's say, isr equal to new input stream reader.

  162. Speaker 2

    Okay, very good. Now, what will we do?

  163. Speaker 1

    Sorry, let's put this. Now, look, we will have to pass something in this input stream reader, in its constructor. You can see, there is an error. So, let's see what its constructor is expecting from us. So this is the constructor of the input stream reader and it is asking us for an object of an input stream i.n So we know what we have to give here, an object of an input stream which is system.in Now as soon as we gave it this, what did it do? It converted this input stream, basically this stream of bytes into a stream of characters.

  164. Speaker 2

    Okay, very good.

  165. Speaker 1

    Now once I got this input stream reader, now I have to make an object of buffered reader. So I will write buffered reader br equal to new buffered reader something like this. Okay, very good. Buffer Trader's constructor is also asking for something, so let's go to his constructor So, its constructor is expecting a reader type object from me. We understood that reader is a base class, an abstract class and it has multiple classes. It has buffered reader, input stream reader etc. So, it needs a reader.

  166. Speaker 2

    Type object only.

  167. Speaker 1

    So, I have passed ISR because it is also an object of input stream reader and input stream reader itself is a subtype of buffered reader. So, I can pass ISR to it. Now, mine has become buffered reader. Now I can read any line directly from the input. Do you know what my buffer reader does? Whatever ISR has converted stream of bytes, stream of characters, it puts a buffer in it, that is, it brings the buffer together in the operating system. Now suppose if I take a variable, let's say string name, and I write br. We have a method readLine. So it will read the entire line. Now if I try to print it on the screen, system.out.printlname, then our name will be printed. Okay, let's run it once and see. Okay, so now I have to give some input. Let's say I gave an input, Aditya, I got Aditya, very good. Okay, if I give my full name Aditya Tandon, then this also got a.

  168. Speaker 2

    Full name, very good.

  169. Speaker 1

    So our code is working perfectly fine. Okay, let's see its flow once, what will actually be happening from the back. So let's look at some points. First of all, I wrote Aditya on the screen. So as soon as I wrote Aditya, our operating system buffer converted it to what? To the team of bytes. So our OS buffer converted it to bytes. And bytes means that I will first remove the value of ask and again convert it to binary. So now I will ignore binary and see what will be the value of ask. So if I just write the value of ask. So I wrote the value of ask.

  170. Speaker 2

    This is my A, D, I, D, Y, A.

  171. Speaker 1

    So it must have converted to this. Now see, the system.in which was our input stream object Will receive these bytes Receive bytes from OS And after that, what will happen? After that, our input stream reader will be used Because the input stream reader which was ISR, what was its function? To convert this stream of bytes into stream of characters So what would he have done? He would have done a conversion like this.

  172. Speaker 2

    A then D.

  173. Speaker 1

    So it converted into stream of characters Then comes bufferedReader BufferedReader will read the stream of characters and store them in its buffer Whenever we call readLine, it will form a string from that buffer Aditya will form a string from that buffer Aditya will store it in the name field and when we name it system.out.println Aditya will be printed on our screen So this is the whole flow from Aditya's input to Aditya's output. You can understand everything from the inside, how it works. It is possible that many people have seen its code, but they still don't understand how it works from the inside. In fact, many times you will see, you don't always write two different lines like this. In fact, most of the time you don't write like this. What you do is, this whole part, pass it directly to this constructor. You can also do this. After that, you don't need this line.

  174. Speaker 2

    Okay? What did you do here?

  175. Speaker 1

    In the constructor of the new BufferedReader, a new input stream reader is passed directly. You can also do this.

  176. Speaker 2

    It's not necessary.

  177. Speaker 1

    First, make an object and give it a reference variable. You have made an object directly in heap memory. Because if you put a new keyword here, then the object will be made in heap memory. And the object that will be made, where did you give its reference directly?

  178. Speaker 2

    You gave it in this parameter.

  179. Speaker 1

    Now, the constructor of the BufferedReader, whatever variable is kept in its parameter, its reference will come in it. So you can do it directly like this. In fact, most of the time you will see this code. Now I hope you understood BufferedReader well. You understood its ins and outs. Now if you understood BufferedReader well. Now it's time to see its limitations. Which is very important. Then also we will understand. Why did we go to Scanner class further than this. Which Java brought today. The biggest problem of BufferedReader is. That it can only read string. So we always write br.readline. Something like this. where br is a reference variable of a buffer reader which points to an object of a buffer reader. So here we can write string s like this. But suppose we want to read an integer, let's say user input is 25. So how will we read this 25? First we will obviously read this br.readline in string. Then we will have to typecast this string in integer. For that we have a function called integer.parseInt. If you put this function and give any string here, you can convert it. In integer, you can write like this. Here you wrote s and converted it to int. Let's say in i. So first you wrote this line of code. String is equal to br.readline, you read a string and convert it into an integer. And then if you want, you can print this i in system.out.println and your number will be printed.

  180. Speaker 2

    25 if the user gave string 25.

  181. Speaker 1

    So you have to do this long process which is a two step process. Secondly, it has one more disadvantage. You must have understood that by now. Our code is very complicated.

  182. Speaker 2

    Let's see how.

  183. Speaker 1

    We write it. First, we make an object of InputStreamReader. Then we pass it in BufferedReader. InputStreamReader we pass an object of system.int and then call readLine in bufferedReader. And that too can only read a string. Then if we want to do it in integer, then we call it in integer.parseInt. So again, it's still very complicated.

  184. Speaker 2

    We need to simplify it more.

  185. Speaker 1

    Right? Because if you compare it with other languages, Java is still very difficult to work with. It gives us very low-level classes. So here, Java realized and finally, in Java 1.5, Java introduced Scanner class.

  186. Speaker 2

    Scanner class. Scanner class solved all the limitations.

  187. Speaker 1

    First of all, it simplified the entire input process. It simplified the input process so that we don't have to write such a big code. Secondly, it gave us many methods so that we can read integers, doubles, boolean variables, and strings. We can read everything easily so that we don't have to do casting again and again. So it gave us this. Now let's understand Scanner class. What is Scanner class? Again Java must have made a class Scanner from inside. And it must have given a lot of good methods. First of all, understand that our Scanner class does not come in our java.io package. It does not come in the input output package. It comes in a separate package whose name is java.io.

  188. Speaker 2

    Utils package.

  189. Speaker 1

    In this, all the utility functions come. This means that scanner itself is a utility function. It has nothing to do with input and output directly. This means that the hierarchy of input stream. There were many classes in the hierarchy of input stream. Because it was an abstract class itself. Similarly, there was a reader class which was abstract itself. It had multiple classes. Like buffered reader and input stream reader. With these two hierarchies, Our scanner class has nothing to do with it. It is completely different from them. This scanner class works with them as a wrapper. The scanner class works on top of them. But it is completely different from this hierarchy. Now let's see directly how the code of the scanner class looks and how it simplifies it. Okay. So here we are going to understand our scanner class. So scanner class. Okay. So first of all, what did I say? We will have to import java. util package because our scanner class comes under util package and under util we import. scanner ok we have imported now here we need to write throw io exception because input output exception comes only when we are in java.io package so see now how do we collect input from user first of all we make object of scanner class so scanner let's say sc equal to new scanner something like this we make object ok now this object is expecting something in its constructor what is it expecting let's go and see So look, it is still expecting an object of an input stream in its constructor. Okay, very good. That means we still have to give it system.in. It means that no matter how many advanced classes we make, it will work with the input stream. Because what we actually have to do is read the data from the keyboard. And this work is of the input stream. So we will have to do this work again with the input stream. But our syntax ends here. Now if we want to read a string, let's say we need a name. So we have a function sc.nextline. We can print this and read it. And here we can write System.out.println And here we can print the name So our syntax got smaller We didn't have to write any big things Now if I try to print it Let's say I want to print Aditya My Aditya got printed And Same if I want to print Aditya Tandon So our Aditya Tandon also got printed Very good Now let's go inside the scanner class So see, we just saw that even in Scanner class, our Scanner class still expects the object of InputStream which is System.in which means it is working on this only. It has only one wrapper, Scanner class. Scanner was made to provide a utility function which we don't get in buffered reader. We have to cast it every time because it only reads string and it.

  190. Speaker 2

    Looks complicated in syntax.

  191. Speaker 1

    But the work will still be the same from inside. And you know what happens in this hierarchy. We have input stream reader on it. And buffered reader works on input stream reader. So all these hierarchies are known. So what is the scanner class doing? It works on top of these hierarchies. So basically scanner class works on top of these hierarchies.

  192. Speaker 2

    This is different.

  193. Speaker 1

    Scanner class is a different package. This is a different package. Remember this. How does scanner class work with all these? If you want to know a little more internally, then assume it has an interface access which we call readable. Readable. Readable is an interface which again will keep the same functionality.

  194. Speaker 2

    As in a reader class.

  195. Speaker 1

    And you know that in a reader class, you get input stream reader, buffer reader, etc. You don't have to go that far, but you should know how to reach the scanner class. Now, we saw how to create a scanner. Creating scanner. We had a simple method. We wrote scanner sc equal to new Scanner. We made an object of scanner. And what did I say? What does the object of scanner expect in its constructor? It expects an object of input stream in its constructor. So we passed system.in here.

  196. Speaker 2

    Okay, very good.

  197. Speaker 1

    Now scanner doesn't work only with keyboard. Scanner can work with anyone. It can take input with file too. Or it can take input with any string too.

  198. Speaker 2

    Right?

  199. Speaker 1

    So whoever it takes input from, it will depend on what you passed in its constructor. So here we passed system.in in constructor because we were taking input from keyboard. So we write here this is for keyboard. For keyboard, you will write this to take keyboard input. Okay, what if you want to take input from a file? So suppose if you want to take input from a file, then the syntax will change a little, the syntax will become scanner.

  200. Speaker 2

    So in that case, what will we put here?

  201. Speaker 1

    We will put a reference of a file, so new file and whatever the file name is, let's say sample.txt. But again, we are not talking about file input output, so we don't need to understand it. We don't need to understand it. But you can understand that it can also take an input of this file. Similarly, let's say if it wants to work on a string, if it wants to take input from a string, if it wants to take input from a string, then you can also write new scanner and pass a string here. Let's say 10, 20. 30, pass it like this. Now when you will do this, it will read 10, 20, 30 from here. So maybe you want to read something from a string. There are many different ways to read. Generally, we use scanner to read from console. So we write this. If we want to read from file, we will write this. Now see, if we want to understand how scanner works from inside. We know that it works from inside. But scanner does one more thing. So scanner works on the basis of tokenization.

  202. Speaker 2

    Tokenization.

  203. Speaker 1

    What it does is, whatever string we give in the input, let's say I gave in the input, hello, I am Aditya. So what it will do is, it will divide this string into tokens. And how will it identify with the token? Like this is a token, this is a token, this is a token, this is a token. Why four? Because it saw white spaces in between all these. So what did it use for delimiter? Delimiter means, what did it use to divide? It used space. So it said, there are four tokens, hello, I, M, Aditya. So, he divided it into tokens. Now, he can print it one by one if he wants. Now, it depends on which method you have used on the scanner. Let's say, the scanner object SC is your reference variable. On that, you have used SC dot next line or next etc. So, let's see its methods once. Methods of scanner. So let's look at all the scanner methods. Let's say we have one next line and one simple next. What's the difference between next and next line? Next reads a word once and next reads the entire line until we press enter. If I choose next and I give aditya then it will print. But if I run this code again and I give aditya tundad So this will only print Aditya. So this is the difference between next and next line. Now I told you that this can print everything except string, integer etc. There is no need for type casting by itself. So let's take an integer, age. So I took sc. I have a function nextInt. Now as soon as I call nextInt, it will take the integer value from my keyboard. Here I can print it. Let's say I gave it 28, so I output it as 28. Very good.

  204. Speaker 2

    Do I have next double near next int?

  205. Speaker 1

    Yes, absolutely. I can take a double value here. Let's say let's take double age. Okay. Obviously, now if I give a value of 34.56, then my answer will be 34.56. So if you see, you have all kinds of functions.

  206. Speaker 2

    You just type next.

  207. Speaker 1

    So you see, you have next line, next int, next simple next, next double, next byte, next float, next long, next short, You have a lot of next, big and decimal, whatever it is, we haven't talked about it yet, but there is a class, next boolean.

  208. Speaker 2

    You have all kinds of methods.

  209. Speaker 1

    In fact, if I put next boolean here, and here I take a boolean variable, let's say, instead of h, we take an x named boolean variable.

  210. Speaker 2

    X, okay? So, this will also work for me. Now, assume, I gave value here, false, so my print will be false. Very good.

  211. Speaker 1

    Okay? So, next, I can use anything, I don't need any type casting, so scanner class gives us very good utility functions, okay?

  212. Speaker 2

    And we can print anything for it.

  213. Speaker 1

    I hope you have understood all the methods of next, next line, next integer, next float, next double, next boolean. You have all the methods provided through scanner class. Now I have to do one more thing. Always remember that your scanner class is slow. From which?

  214. Speaker 2

    From buffered reader.

  215. Speaker 1

    Many people don't know this, but this is very useful. Scanner is slow because it does a lot of things internally. It tokenizes, when we read regular expressions, it uses regular expressions from inside. It also does type conversion from inside because we don't have to do it. It does everything from inside. Now see, buffer reader neither tokenizes, nor does it use regex, nor does it use type conversion. Since BufferedReader doesn't do much, it gives us whatever is written in the string. So this class is faster than our Scanner class.

  216. Speaker 2

    OK?

  217. Speaker 1

    BufferedReader class just reads our characters. It doesn't do much work on it. Now do you know where this works? Obviously, this is a modern class and it is quite simplified. But if you are writing a code that should be very optimized, it should run in a very short time, then you prefer BufferedReader. Or let's say you are entering in competitive programming, you are writing some code in it, where milliseconds also matter. Then again, you use BufferedReader rather than Scanner because Scanner is slow.

  218. Speaker 2

    Now this was the complete input output.

  219. Speaker 1

    We saw how input output is done in Java, how we came from input stream to BufferedReader and finally from BufferedReader to Scanner. And we also learned all the internal classes. Once you understand the Java input and output, it will be very helpful in your interview and you will never have any question about what a buffered reader does, how does it work from inside, etc. All doubts will be cleared because we understood it from the very beginning that what used to happen before and now what happens in modern Java. I hope you enjoyed it. See you in the next video. Till then, thank you so much.