Java for Testers – Java Tutorial Series for Selenium Testers
Welcome to our Core Java Tutorial series! This comprehensive Core Java for Testers Series is designed specifically for beginners who wants to learn Java and also for the Selenium testers who are learning Java. Understanding Java is essential, it empowers you to write automated test scripts, effectively interact with various testing tools, and significantly improve your testing efficiency.
Throughout this tutorial, we will cover fundamental concepts of core Java, tailored to the needs of testers. Whether you are a beginner or looking to refine your Java skills, this tutorial series will guide you through everything you need to know to become proficient in Java programming for testing purposes.
To start you automation testing journey, check out our Selenium WebDrive with Java Tutorial.
Java for Testers
Let’s see what we are going to learn in this Core Java Tutorial now
- 1. Java Introduction
- 2. JDK, JRE and JVM
- 3. Java Environment Setup
- 4. Java Syntax
- 5. Print in Java
- 6. User Input In Java
- 7. Comments in Java
- 8. Read From File
- 9. Write To File
- 10. Variables In Java
- 11. Variable Naming Convention in Java
- 12. Types of Variables in Java
- 13. Data Types in Java
- 14. Operators In Java
- 15. Control Flow Statements
- 16. Conditional Statements
- 17. if statement
- 18. Nested if statement
- 19. if else statement
- 20. Switch Case
- 21. For Loop
- 22. Enhanced For Loop
- 23. While Loop
- 24. Do While Loop
- 25. Continue Statement
- 26. Break Statement
- 27. OOPS Concept
- 28. Class
- 29. Object
- 30. Method
- 31. Modifiers
- 32. Access Modifiers
- 33. Constructor
- 34. Inheritance
- 35. Polymorphism
- 36. Method Overloading
- 37. Method Overriding
- 38. Abstraction
- 39. Abstract Class
- 40. Abstract Method
- 41. Interface in Java
- 42. Encapsulation
- 43. Arrays in Java
- 44. ArrayList in Java
- 45. How To Convert Array to ArrayList
- 46. Collections Framework in Java
- 47. Map in Java
- 48. LinkedList in Java
- 49. Exception Handling
- 50. Java Interview Questions
- 51. Java Quiz
Java Introduction:
Let’s see a brief introduction to Java. Java is a high-level programming language originally developed by Sun Microsystems in 1995. Java is a platform independent language. Yes, it runs on a multiple platforms such as UNIX, Windows, Mac OS. The Java language’s programming is based on the concept of OOP. We will see this in detail in later part of this Java Tutorial.
JDK, JRE and JVM:
We need to understand three terminologies for sure in Java such as JDK, JRE and JVM. Here I give basic idea about these terms in the next post we will see detailed explanation.
What is JDK?
JDK stands for Java Development Kit.
Using JDK, we can develop, compile and execute (run) new applications and also we can modify existing applications. We need to install JDK in developers machine where we want to develop new applications or modify existing applications.
JDK includes JRE and development tools (environment to develop, debug and monitor Java programs).
What is JRE?
JRE stands for Java Runtime Environment.
Using JRE, we can only execute already developed applications. We cannot develop new applications or modify existing applications.
As the name suggests, JRE only provides Runtime Environment.
What is JVM?
JVM stands for Java Virtual Machine. JVM drives the java code. Using JVM, we can run java byte code by converting them into current OS machine language.
Java Environment Setup:
Download and Install JAVA
Go to the below mentioned link and download the latest version of JAVA
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Accept the license agreement and choose the right ‘JDK’ file to download based on your system requirement.
Once downloaded. Go ahead and verify the Java version. To do this, open command prompt and type “java -version” and hit enter
Java Syntax:
1. Java is a case sensitive language
Ex: NAME and name are not same as per Java Language
2. Java file name should be same as its Class name
3. Class name should start with upper case letter
4. Method name should start with lower case letter
5. Every statement should end with semi colon
6. Java program execution starts from main method which is mandatory in every program
public static void main(String [] args){
}
Print in Java:
In Java, we use print to output required text directly to the console of IDE
Syntax:
Simple print statement:
System.out.print(“Learning Java from SoftwareTestingMaterial”);
Simple print statement with new line:
System.out.println(“Learning Java from SoftwareTestingMaterial”);
Check this link to read more on Print in Java
User Input In Java:
Sometimes, we may face a situation where we need to get the input from the user in runtime. We use “Scanner” class to accept input from the user.
Syntax:
import java.util.Scanner; Scanner userInput = new Scanner(System.in); variable = userInput.next(); userInput.close();
User Input in Java with sample programs
Comments in Java:
In Java, we have two types of comments. We use comments to write some text within our code. Compiler will ignore these comments.
Syntax:
// Single line comment
/* Multi line comments – Line 1 Multi line comments – Line 2 */
Note: Comments in between the code gives more readability
Do you want to show auto generated code whenever you create a new class as shown below.
Follow the below steps:
I assume, you are using Eclipse IDE.
1. In eclipse, Go to Window – Preferences
2. From the left panel, Select Java – Code style – Code template
3. Under ‘Configure generated code and comments’, Expand Comments – Select Files and Click Edit and Enter your text and Click OK.
Whenever you create a new class, you can see comments.
Read From File:
To read a text file, we use FileReader and wrap it in a BufferedReader.
In the below example, we read a file named “FileToRead.txt” which is located in my local system and output the file line by line in my eclipse console.
Sample program on Read From File in Java
Write To File:
To create a new file and write text on it. We can write to a file using Java in different ways but I show you how to write text to a file using BufferedWriter.
Sample program on Write To File in Java
Variables In Java:
In Java, variable is a name given to a memory location and this variable is associated with a value.
int x = 99;
int – data type
x – variable
99 – value
variable x holds integer values and its current value is 99.
Let’s see how to declare variables in Java
Syntax to declare a variable in Java:
data_type variable = value;
Example:
int x = 99;
Variable Naming Convention in Java:
Earlier we have learnt that Java is a Case Sensitive Language. Even variables have their own naming convention to follow.
1. Variable name can starts with special characters such as _ or $
Example:
int $myAge;
2. Variable name should begin with lower case leter
Example:
Wrong way: int Age;
Correct way: int age;
3. If the variable name consists of more than one word, it’s a best practice to capitalize the first letter of each subsequent word.
Example:
Wrong way: int myage;
Correct way: int myAge;
4. Variable name should not contain white spaces
Example:
Wrong way: int my Age;
Correct way: int myAge;
Types of Variables in Java:
There are three types of variables in Java.
1. Local variable
2. Instance variable
3. Class/Static variable
Read more on Variables in Java with Sample programs




