In this course, we touch on key topics that should be known before learning the basics of Java.
- Java Output
- Java Input
- Java Comments
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this video, we will learn simple ways to display output. We can simply use System.out.print(), System.out.println() or System.out.printf() to send output to the screen. Let's learn the features of these output methods. The print() method, prints string inside the quotes. It belongs to the PrintStream class. The println() method, prints string inside quotes similar to the print() method. It's an overloaded method of the PrintStream class. It accepts the string as a parameter.
However, after printing, the cursor goes to the beginning of the next line, the ln represents the line. So, the print line is the main difference between the println() and the print() method. The printf() method provides string formatting. It's used if we want to print the formatted string to the console using the specific format string and arguments. It's also an overloaded method of the PrintStream class. The method behaves the same as the invocation of the format method of the string class.
Now, let's move on to the Eclipse and do some examples. First, let's create a new Java project. I click File and select New, Java Project. The project name can be Exercise. And without changing anything else, I'll click the Finish button. The exercise project has been created. Now, I will create the Java class in this project. In Exercise project. right click on the SRC i.e., Source folder and select New, Class. Specify package name as inputoutput and class name as BasicOutput, and select the checkbox for the main method. Lastly, to create this class, I click the Finish button. The first message will be "Welcome to Java programming" by using the println() method. Eclipse has a quick shortcut for you to write the println() method. Inside the main method, I write sout and press Enter. The println() method was created automatically. This is known as code completion. It allows us to quickly complete statements in our code. We can use this combination in all other parts of the program such as variable, method, class, etc.
Now, I will write whatever I want to appear on the console screen between the double quotes. So, the message will be "Welcome to Java programming". Here the println() is a method that displays the string inside quotes. Let's run the code. The result is true. Now, I will copy and paste this line once. Also, we can concatenate the strings by using the plus operator. So, the first string will be the "Welcome to" and + operator. And the second string, will be "Java Programming". Let's run the code. As you can see, the result is the same.
Now, let's look at the use of the print method. Let's copy and paste this line four times, and change the message like that. This will be only Welcome and a space. This will be to and a space. This will be Java and a space. And lastly, this will be programming. Also, these last two println() methods will be print. Here we have two println() methods and two print() methods. The first two methods are the println() method. And the last two methods are the print() method. Now, let's observe them on the console screen. Let's run the code.
You see the result. After the println() method, the cursor moves to the next line. But after the print() method, the cursor remains at the same line. Let's look at the printf() method. First, I will define three variables. Of course, you have not yet learned what a variable is and how it is defined. We'll talk about variables in details in our next lessons. For now, you can only follow. The first variable will be x, and the type of x will be the integer, and the value of x will be five. The second variable will be y and its value will be three. And the last variable will be z and its variable will be the sum of the x and y. So, x + y.
Now, we can use the printf() method, sout and press Enter. This is the println() method. Now, I will convert it to the printf() method. In the printf() method, we use the % mark with a d character that represents a decimal integer. Now, we will write the variables namely x, y, and z. This means handling variables in %d format i.e., in decimal format. Let's run the code and show the output. As you can see, the cursor did not move to the next line because the previous method was the print() method and the result we printed with the printf() method was printed on the same line.
And as an output, the sum of 3 and 5 is written as 8. We can think of the printf() method as the format method of the string class. To understand this better, let's do one more example and end the lesson. I will create one more printf() method. This time, I will use %S as the format. As a value, I write java in lowercase. This format will print the word java in lowercase to the console as uppercase. Let's run the application and see. As you can see, there's the word JAVA written in capital letters in the console. Yes friends, these are the Java output commands. Let's take a short break here. See you in our next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.