Core Java Concepts for Selenium Automation
Welcome to your first Java class! We'll start by laying the foundation with some basic concepts that are essential not just for learning Java, but also for becoming a successful automation engineer using Selenium. Let's dive in!
1. Data Types in Java
Data types define the kind of data that can be stored in a variable. In Java, data types are categorized into two main types: Primitive and Non-Primitive.
- Primitive Data Types These are the most basic data types. Java has 8 primitive data types:
- int: This is used to store integer values (whole numbers) without any decimals. Example:
int age = 25;
- float: This is used to store decimal numbers. Unlike
int
,float
can hold fractional values. Example:float price = 19.99f;
(Note: Thef
at the end is required to denote a float.) - char: This is used to store a single character, such as a letter or symbol. Example:
char initial = 'A';
(Note: Single quotes are used for characters.) - boolean: This can hold only two values:
true
orfalse
. It's used for simple flags that track true/false conditions. Example:boolean isJavaFun = true;
- int: This is used to store integer values (whole numbers) without any decimals. Example:
There are other primitive data types like byte
, short
, long
, and double
, but the ones above are the most commonly used.
Non-Primitive Data Types These include classes, arrays, and interfaces. We'll focus on these later as we progress.
int
variable to store your age, a float
to store your height, a char
to store your initial, and a boolean
to store whether you like Java (yes, you will!).
2. Variables in Java
Variables are containers for storing data values. Java has three main types of variables:
- Local Variables: These are declared inside a method or a block of code. They can only be used within that method or block.
public void printName() { String name = "John"; // Local variable System.out.println(name); }
- Instance Variables: These are declared inside a class but outside any method. They belong to an instance of the class, meaning each object of the class has its own copy of the instance variables.
public class Person { String name; // Instance variable }
- Static Variables: These are declared with the
static
keyword inside a class. Unlike instance variables, static variables are shared among all instances of the class.public class Person { static String species = "Human"; // Static variable }
Car
with instance variables for brand
, model
, and year
. Also, create a static variable called wheels
(since all cars have wheels).
3. Operators in Java
Operators are special symbols used to perform operations on variables and values. Here are some basic types:
- Arithmetic Operators: Used to perform basic math operations.
+
(Addition):int sum = 10 + 5;
// Result: 15-
(Subtraction):int difference = 10 - 5;
// Result: 5*
(Multiplication):int product = 10 * 5;
// Result: 50/
(Division):int quotient = 10 / 5;
// Result: 2%
(Modulus):int remainder = 10 % 3;
// Result: 1
- Relational Operators: Used to compare two values.
==
(Equal to):5 == 5
// true!=
(Not equal to):5 != 3
// true>
(Greater than):5 > 3
// true<
(Less than):3 < 5
// true
- Logical Operators: Used to combine multiple conditions.
&&
(Logical AND):true && false
// false||
(Logical OR):true || false
// true!
(Logical NOT):!true
// false
&&
.
4. Type Casting in Java
Type Casting is the process of converting one data type into another. There are two types:
- Implicit Casting (Widening): Automatically converting a smaller type to a larger type.
int x = 10; double y = x; // Implicit casting from int to double
- Explicit Casting (Narrowing): Manually converting a larger type to a smaller type.
double a = 9.78; int b = (int) a; // Explicit casting from double to int
int
to a double
and then back to an int
.