Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Automation Testing with Java for Beginners

You're reading from   Hands-On Automation Testing with Java for Beginners Build automation testing frameworks from scratch with Java

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781789534603
Length 156 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rahul Shetty Rahul Shetty
Author Profile Icon Rahul Shetty
Rahul Shetty
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. First Programming Steps in Java FREE CHAPTER 2. Understanding Classes, Objects, and Their Usage in Java 3. Handling Strings and Their Functions in Java 4. Building Blocks for Java Programs – Loops and Conditions 5. Everything You Need to Know About Interfaces and Inheritance 6. Learn Everything about Arrays 7. Understanding Date Class and Constructors in Java 11 8. Importance of the super and this Keywords and Exceptions in Java 9. Understanding the Collections Framework 10. The Importance of the final Keyword, Packages, and Modifiers 11. Other Books You May Enjoy

Writing your first executable program in Java

Let's start with our basic coding in this section. If we want to print something in our output, there is a command in Java called System.out.println(). This command will print the output in the console. Let's say we would like to print hello world, and when we run the following code, hello world will be printed in our output console:

Firstclass.java

So let's run the code. There are two methods to run the code:

  • Right-click on the filename in the Project Explorer, click on Run As, and select Java Application.
  • Or, we could click on the run icon given in the toolbar and click on OK on the Save and Launch window. The icon looks like this:

This will run our code and print our output. The following screenshot shows the hello world message on our editor:

Output displaying hello world as per the code

In short, System.out.println() is used to print in our console. We will be using this in almost all our examples for our demonstration of practical examples. If we remove ln from the statement, it will not print the output in the next line.

Let's try printing a statement that will display the output of two print commands on the same line. Here, we add a System.out.println("hi") statement before the hello world statement. If we run the code, the output will be as follows:

Output is displayed on two separate lines

Observe how hi is displayed on one line and then hello world is displayed on the next line. Here, ln displays the output in the next line. If we remove ln from both the statements and run the code, the message will be displayed as follows:

Output is displayed on the same lines

We see, hihello world printed on the same line.

If we write our code, and then we want to check the output partially, we don't need to remove the line of code; all we need to do is just comment it out. We can comment it out by simply putting double slashes (//) at the beginning so that Java will not pick the line. This is shown in the following screenshot:

Commenting using double slashes

If you remove the slashes and the statement is just some random words, then it will throw an error. We will see a red underlined code. This means there is an error at the line with a cross mark. This is shown in the following screenshot:

Error is flagged with a cross mark besides the line number

Add the backslashes again to comment out the error.

Remember, here we are writing our actual code in the main block only. What if we want to print an integer?

Let's say we want to print the number 4. To print it, we first need to store it in a variable and then we will print the variable. So when we print the variable, the value presenting that variable will be printed automatically. For this example, we pick the number 4, and we assign the number in a variable called a. The problem here is that a does not know what data type is being assigned to it. So, we have to explicitly mention that a is an integer. If we do not mention that a is an integer, it throws an error.

In short, we are first creating a variable called a which only acts an integer and then places an integer value 4 into this. The following screenshot illustrates the example we are talking about:

Value 4 is assigned to the variable a

So, with this type of code, we can type it outside, but if we want to print it, we will have to type it in the main block. In this example, we want to print the value of a so we add another System.out.println(a) statement. The editor will throw an error for the variable a in the print statement. To know what the error is, we hover our mouse over the error and a pop up is displayed showing the error with a possible fix, as shown in the following screenshot:

Error details is displayed when the mouse is hovered over it

There will be an option to click on in the error detail. This will automatically resolve the error by adding the required content. This is an amazing feature that the editor has and it is very helpful as we move onto more complex examples.

In our example, when we click on Change 'a' to 'static' in the error detail pop up, static is added to the variable a and we are able to run the code. On running the code, the console will look like this:

Output displaying the value of a as per code

We will be getting into the details of what exactly static is in the later chapters

You have been reading a chapter from
Hands-On Automation Testing with Java for Beginners
Published in: Sep 2018
Publisher: Packt
ISBN-13: 9781789534603
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image