Transcript for:
Understanding Java Print Methods

Hello friends and welcome back. In this lecture we will see how we can display messages in Java. So here is our outline. We will talk about strings, we're going to talk about println and print methods and we'll talk about the system class. So let's get started. So first of all what is a string? Simply a string is a group of characters. So it is some text. So if you want to type some text in Java this text will be a string. Now All strings in Java should be put in double quotes. So let me give you some examples. Here is a list of strings. Over here, we have this word, hello, and it is put inside double quotes. So it is a string, okay? Another example, this sentence over here. It is a group of characters put between double quotes. So it is also a string. Now over here, we have the number one between double quotes, and we have 125 also between double quotes. So these are not numbers, these are strings. We are putting the character 1 between double quotes, and we are putting these characters between double quotes. So these are strings, alright? And this over here is also a string. It contains characters between double quotes. So all the characters that you can see on your keyboard, whenever you put them between double quotes, they will be a string in Java, okay? And this over here is an empty string. So as you can see, we have double quotes. but it is empty. So in summary, a string is a group of characters put between double quotes. Now let's talk about the print method. We will see how we can call the print method or how we can use it. So first of all, what does this method do? This method displays its parameter on the console. So whatever we put between parentheses will be printed on the console window. What is the console window? As you will see later on, it is a small window where we can see the output of our program. So let's see how we can call this println method. So let's have a look at this java code. First of all it ends with a semicolon so this is a statement. We are saying system.out.println and between parenthesis we are passing this string. So first of all forget about system.out we will talk about this in a little bit. Just focus on println hello okay. So as we said before, in order to call a method, we will put the name of the method followed by the parameters. And this is what we're doing over here. The name of the method is println, and between parentheses, we are giving this method a string. So Java developers wrote this method to display whatever we give it as a parameter. So this string should be printed. Let's see another example. Suppose that we pass 1, 2, 3 as a parameter to the println method. So, this string should be printed. Also, if we print an empty string, it should be printed. But because it is an empty string, we will see nothing. Okay? Another example, we can print this string over here. 456 between double quotes. Okay? So now, let's see the output of this Java code whenever it gets executed. First of all, we will print hello. And as you can see, it is printed without the double quotes. The double quotes. are just to tell Java that you are going to print a string. But the value that will be printed is the value between the double quotes. Okay, so this is why we will see hello printed. Now this statement will finish executing. Then we will execute this statement over here. Now we want to print 1, 2, 3. And because we are using print ln, this ln over here means that we want to get to a new line. So whenever we print the word hello, we will go to a new line so when we print this string over here we will see it on a new line like this one two three will be printed without the double quotes and also we will get to a new line because we have an over here so now whenever we are executing this statement we are going to print an empty string on the new line over here so we will actually see nothing because it is an empty string but we will get to a new line okay so we will print nothing over here and then we will move to a new line So now 456 will be printed over here. So this will be the output of these statements over here. So to call the println method, we type its name and we give it the parameter that we want. And up until this point, we are giving it strings. Later on, we will see how we can display numbers in Java. Okay. And remember, ln is to break to a new line. So whenever println is executed, it will print the parameter and break to a new line. Okay. Now let's have a look at the print method. It works the same as println but it does not break to a new line. So it also displays the parameters on the console window. So suppose we have the same code as before and now we are using the print method not the println method. So all the output will be printed on the same line. We will not get to a new line. So the output will look like something like this. First we print hello. So the first statement is executed. And now we will execute this statement. So we print 1, 2, 3. Right? After that, you are printing an empty string. So we will not print anything. And after that, we will print 4, 5, 6. So as you can see, we have 4, 5, 6 over here. And between 3 and 4, we have nothing because it is an empty string. Okay? So the only difference between println and print is that there is no line break. So as you can see also whenever we use the print method we type system.out.print So let's see what is this system.out First of all this out over here is an object of the print stream class So in java we have a class which is print stream And the java developers created an object from this class which is called out okay Now this object contains some methods It contains the print method and the println method. Okay. So now you know that an object can contain methods. Just like a class. So this class over here contains some methods. And whenever we create an object from this class. This object will also contain methods. So if you want to access the print and the println methods of the out object. We will use the dot operator. Okay. So if I want to access the print and the println method. we will type out. the name of the method. Now this object refers to the standard output device which is the screen. This is why whenever we use the print or print alarm method, we will see something printed on our screen on the console window. Okay, so these are the information I want you to know about the out object. Now let's talk about this system class. First of all, note that it is named according to the Pascal case convention. So we can conclude that it is a class. This S over here is a capital S. And look at this out over here. It is named according to the camel case convention. The first letter is a small letter. And this is basically a variable or a field. Alright? We will talk about this later on. So now what I want you to know. Out is inside the system class. It is a field of the system class. So in order to access out of system. We will also use the dot operator. So now we will do something like this. We want to access our system class. So we use the dot operator. And inside our system class, we have the out object or the out field. And we want to access the methods from inside this field. So we will use the dot operator. And then, for example, we can access the println method or the print method. So now I hope that you understand why we write system.out. dot print or println. Alright, so this is it. Congratulations. Now you know how to print some messages in Java. Thanks for watching and I'll see you in the next video.