Java Essentials - Input and output techniques in java
3K views
Nov 18, 2024
Java Essentials - Input and output techniques in java
View Video Transcript
0:00
In this video we are discussing input output techniques in Java
0:05
So what are the different functions are available for input and output? We'll be discussing on that
0:10
So we can get the output on the console using these functions
0:15
So these are the set of functions which with the help of which you can put the output
0:19
on the console. And we have to use the system dot out before using the function name
0:26
For example, it is like your system dot out dot print. LN this is a method we are using so system is one class which is residing under the java dot
0:36
lang package and out is one object under this particular class so this out is an object
0:44
of the class print stream and that object is one of the instance variable under this system class
0:51
and that print stream class is having a method that is a print ln so that can be accessed using
0:58
out of print all and And this out is one of the static member variable under this system class and this system class is residing under Java
1:09
So instead of print LN also you can go for print or print F
1:15
Now what are the differences between this print LN print print print print print print print print print print print and print F? In case of print LN, the print the text and then go to the next line
1:23
That means it is having a new land character. and this print print the text but do not go to the next line so it is not having any new line
1:32
character in built it is not having that one so the cursor will be staying in the same line
1:37
where we did the print last and then we're having this print f so this function is very much
1:43
useful for formatted string output now how to how to do the formatting of the output string so let us go
1:50
for the details so for the formatted string we can use some specific format specific
1:55
for different data types. As an example, we can go for percentage D for integer type data
2:03
percentage F for floating type data, percentage small E or also can use percentage capital
2:10
e for floating point data in exponential notation. We can have this percentage C, so C stands for
2:16
the character type data. We're having this percentage small S or percentage capital S for string type
2:25
of data we're having this percentage small b or percentage capital b for bullion type of data
2:32
also you can have this percentage percentage percentage to display percentage symbol once on the
2:37
console output so these are the different formatting strings are possible with the help of which
2:43
we can do the formatting in the output input techniques in java so there are so many different
2:49
ways we can take inputs from this in this java code and the most popular one is our scanner class so to take input from the user we will use the scanner class object it is the most popular one but
3:03
apart from this scanner class we can use different other methods and classes
3:08
to take inputs from the keyboard from the user's keyboard to use the scanner class we need to import into the Java source code
3:16
and there is an import should be there there is a input java dot util
3:20
scanner scanner is the name of the class and this Java dot util is the respective package name so before
3:27
using this scanner class objects in our Java program we must be importing that
3:32
is the Java dot utile dot scanner class here so let us go for one demonstration for
3:38
the better understanding that how these functions and methods can be used in our
3:42
Java programming so here is that demonstration for you in this demonstration we're
3:48
discussing input and output in Java So that is one message that is this is an output line this message this particular string is going to get printed using system
4:00
Now what is system? System is nothing but one class which is defined under the package java
4:08
dot lang. That means the package name is Java. and the class name is system
4:15
So let us go for exploration of this. So this is our input output Java program
4:20
program and here you can find that we'll be going for this Java dot Lang you can easily
4:25
find Java dot Lang is there so you shall go for RT dot jar and then we shall go for
4:32
Java dot Lang so different packages are given here so let us go for Java
4:40
dot lang so it they are addings in the alphabetical order so I shall go for this Java
4:44
and then Lang so that is my Java. under this Java.tlang we'll be going for the class that is our system so if you go
4:53
for the class system they are also arranged in the alphabetical order so that is our
5:00
system class so this is our system class has got opened so now you see here we're
5:05
having system dot out now what is out and this out is nothing but one print stream class
5:11
object and this print stream class object is there and this particular object
5:17
must be of the static scope and it is written here also there is a public static
5:22
final print stream out so this is a declaration that is out is nothing but one
5:28
instance variable under the class system and these instance variable is nothing but one object of the class print stream as this particular object is
5:38
having the static declaration so as a result of that we can access this object
5:44
directly from the class name dot operator so let us go for this out so now we going for the system you can find that here we having this out here we having this out here declared there is a public static vinyl
6:00
java.io print stream class object out is there as this out is having the respective
6:06
static declaration so this out can be accessed directly from system. class system class dot out here so now this is about Java
6:16
dot Io dot print stream so let us go for Java. dot I.O
6:20
printstream to check that this particular Java. dot I.O. dot print stream class where the Java
6:26
dot I.O is a package name printstream is the class must be having a method that is our
6:30
print LN. So that's why we can we can access it. So we are going for this Java
6:35
dot I.O. So that is our Java. .io we are going for that
6:42
so this is our Java.io and from here we'll be going for the print stream
6:53
so there is a class called print stream this is a respective class and this
7:01
particular class is having a method that is a print ln and this method is not
7:07
static so that's why I should I cannot be calling this method print ln directly from
7:12
print stream dot print ellen because the method is not static here so that's why I
7:17
should declare one object under the print stream class then object named
7:21
print ln can take place so that's why this system is having our out is the
7:27
object of this print stream class so out dot print all n is allowed here so that's
7:33
why we're getting the respective help here also we're having this Java dot lang
7:38
system and this is a out this out we're having and this out is nothing but one print steam class this print stream class object that is
7:48
we're having this one as java.d lang. system dot out and it is declared under the static scope
7:55
so that's why we can directly access this out from this system dot out and print ln is nothing but one
8:02
method this method is having the prototype that is a void he returns void it is defined under
8:09
the package java dot i.o and it is having the respective class that is a print stream and dot print LN is a
8:16
respective method and it takes a string as input argument string object and
8:20
this is a string object we have passed so in this way the code is actually
8:25
getting executed so I have given you the explanation of the system dot out
8:29
or print all which is a very common question in the interview or in the exam
8:34
viber so if I go for the execution you can find that this is an output
8:38
line this string has got printed here okay so this string has got printed next one we are having this particular x is equal to 10 x is of the type integer so value of x we are doing this one as concatenation so string concatenation integer so ultimately it will
8:57
be converted implicitly to the string and that's why the output is coming as the value of
9:03
x colon 10 here next one is that we are not using print ln we are printing using print and
9:09
print so what is happening the first string and the second string these things have got
9:14
got printed in the same line but if you put here LN here if you put here LN
9:19
here LN then obviously these two lines will get printed in two separate rows
9:27
so there is a first string there's a second string so if you make this LN omitted
9:32
you can find that the second string this string is getting printed in the same
9:36
line in which the first string got printed next one is that system dot out dot print
9:42
if this is the value of X pi is equal to this in this way we can go on printing so x will have this particular
9:50
formatting that is a decimal type and this is nothing but 22 f means 22 will be
9:56
created as a float number by 7 so it will be printed as percentage f so we can find
10:02
that it is getting printed as 10 and 3.142857 so in this way we have explained
10:08
that how this outcome is coming now scanner class object so this is a scanner class
10:13
object so this is a scanner class we're having so it is under the Java dot util
10:18
scanner so Java dot util is a package name scanner is the class name under this
10:22
class we have defined one object this object has got instantiated is getting
10:26
instantiated using this constructor so new and then the constructor name which is must be same as the class name here the class name is scanner so the
10:35
constructor name is also the method is also known as the will be called by the
10:39
scanner and this is a system dot in there is a scanner class of
10:43
to read from the input device and here the stream of characters will be coming
10:47
from the systems in default input device that is our keyboard here so system dot
10:54
outer print ellen entered data using keyboard so when the data will be entered
10:59
so that can be accessed using scanner dot next I and you see here the input
11:05
will be converted to the integer and that will be assigned to I and the data
11:09
and later the data is getting printed here and scanner dot close
11:13
to score the scloose the scanner object when the inputs are obtained so let us go
11:19
for the execution you can find that it is asking for some input I'm giving the
11:22
input as 100 now you can find that the data that has been read is 100 here so
11:29
in this way in this particular demonstration we have discussed that what are the
11:33
different inputs and output how we can do the inputs and outputs in our Java
11:38
coding and we have given you the detail description what is system dot out
11:43
print ellen thanks for watching this video
#Java (Programming Language)