A variable stores a value that your program can use and change. In test automation, variables hold test data — URLs, usernames, expected values, element counts.
public class VariableBasics {
public static void main(String[] args) {
String url = "https://demo-bank.example.com";
String username = "testuser";
int loginAttempts = 3;
boolean isLoggedIn = false;
System.out.println("Testing: " + url);
System.out.println("User: " + username);
System.out.println("Max attempts: " + loginAttempts);
System.out.println("Logged in: " + isLoggedIn);
}
}Every variable has a type, a name, and a value. The type determines what kind of data it holds. Java is strongly typed — you cannot put text into an int variable.
Java has 8 primitive types. In test automation, you primarily use four: int, double, boolean, and char. The rest (byte, short, long, float) are for specific cases.
| Type | Stores | Size | Example | Automation Use |
|---|---|---|---|---|
| int | Whole numbers | 4 bytes | 42, -7, 0 | Counts, indices, scores |
| double | Decimals | 8 bytes | 3.14, -0.5 | Prices, percentages, duration |
| boolean | true/false | 1 bit | true, false | Element visible, test passed |
| char | Single character | 2 bytes | 'A', '$', '7' | Rarely used directly |
| long | Large numbers | 8 bytes | 9999999999L | Timestamps, large IDs |
| float | Decimals (less precise) | 4 bytes | 3.14f | Rarely used — use double |
| byte | -128 to 127 | 1 byte | 100 | File handling |
| short | -32768 to 32767 | 2 bytes | 30000 | Rarely used |
public class DataTypes {
public static void main(String[] args) {
int itemCount = 5;
double price = 29.99;
boolean inStock = true;
char grade = 'A';
long orderId = 1234567890L;
String productName = "Laptop";
System.out.println(productName + " | Qty: " + itemCount + " | Price: $" + price);
System.out.println("In stock: " + inStock);
System.out.println("Grade: " + grade);
System.out.println("Order ID: " + orderId);
}
}String is NOT a primitive — it is a class (starts with uppercase S). Primitives start with lowercase (int, double, boolean). This distinction matters when comparing values.
public class VarLifecycle {
public static void main(String[] args) {
// Declare — creates a variable with no value
int retryCount;
// Initialize — first value assignment
retryCount = 3;
// Reassign — change the value
retryCount = 5;
// Declare and initialize in one line (preferred)
String browser = "Chrome";
double timeout = 10.0;
// Multiple variables of same type
int x = 1, y = 2, z = 3;
System.out.println("Retries: " + retryCount);
System.out.println("Browser: " + browser);
}
}Using a variable before initializing it causes a compile error: variable retryCount might not have been initialized. Java does not assign default values to local variables.
loginAttempts, isVisible, getPageTitleLoginPage, TestRunner, HomePageMAX_RETRIES, BASE_URL, TIMEOUTelementCount not ec, isDisplayed not flagThe final keyword makes a variable a constant — its value cannot change after assignment. Use it for values that should never be modified during test execution.
public class Constants {
public static void main(String[] args) {
final String BASE_URL = "https://demo-bank.example.com";
final int MAX_RETRIES = 3;
final double TIMEOUT = 30.0;
System.out.println("URL: " + BASE_URL);
System.out.println("Max retries: " + MAX_RETRIES);
// BASE_URL = "https://other-site.com"; // Compile error: cannot assign to final variable
}
}Q: What is the difference between int and Integer in Java?
A: int is a primitive type that stores a raw 32-bit number on the stack. Integer is a wrapper class — an object that contains an int value. Integer can be null (useful for database fields that might be empty), works with Collections (ArrayList<Integer>), and provides utility methods like Integer.parseInt(). Java auto-boxes between them: Integer x = 5 automatically wraps the int into an Integer object.
Q: What happens when you divide two integers in Java?
A: Java performs integer division — the decimal part is truncated, not rounded. 7 / 2 returns 3, not 3.5. To get a decimal result, at least one operand must be a double: 7.0 / 2 returns 3.5, or cast one side: (double) 7 / 2.
Exercise 1: Create variables for a test scenario — store a URL, username, password, expected page title, and timeout. Print all values in a readable format.
Exercise 2: Declare an int variable, print it, reassign it to a new value, and print again. Then try adding final and reassigning — what error do you get?
Exercise 3: What is the output?
``
int a = 10;
int b = 3;
System.out.println(a / b);
System.out.println((double) a / b);
``