Learn Java , Selenium

Java Intro

Java is a popular, object-oriented programming language used for building platform-independent applications. Java code is compiled into bytecode, which runs on the Java Virtual Machine (JVM), enabling cross-platform compatibility.

Console Output: print and println

  • System.out.print() — prints text without moving to a new line.
  • System.out.println() — prints text and moves to the next line.

Example:

System.out.print("Hello ");
System.out.println("World!");

Output:

Hello World!

Comments in Java

  • Single-line comment: Use //
    // This is a single-line comment
  • Multiple-line (block) comment: Use /* ... */
    /* This is a
    multi-line comment */

Data Types

Data type Description Example
int Integer numbers int age = 25;
char Single character char letter = 'A';
double Double-precision floating point double price = 10.99;
float Single-precision floating point float pi = 3.14f;
boolean True or false values boolean isJavaFun = true;
String Sequence of characters (not primitive) String name = "Alice";

If-Else Condition

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Example:

int num = 10;
if (num > 5) {
    System.out.println("Greater than 5");
} else {
    System.out.println("5 or less");
}

If-Else-If Ladder

if (condition1) {
    // code for condition1
} else if (condition2) {
    // code for condition2
} else {
    // code if none of above conditions are true
}

Operators

Operator Description
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
&&Logical AND
||Logical OR
!Logical NOT (negation)
++Increment (increase by 1)
--Decrement (decrease by 1)
%Modulus (remainder of division)

Example:

int a = 5;
int b = 2;

System.out.println(a % b);  // Outputs 1

String Comparison

  • equals() — checks if two strings are exactly equal (case-sensitive).
  • equalsIgnoreCase() — checks if two strings are equal, ignoring case.
String s1 = "hello";
String s2 = "Hello";

System.out.println(s1.equals(s2));           // false
System.out.println(s1.equalsIgnoreCase(s2)); // true

Switch Statement

int day = 3;
switch(day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

Loops

For Loop

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

While Loop

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}

Do-While Loop

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);

Break and Continue

  • break — exits the loop immediately.
  • continue — skips the current iteration and proceeds to the next.

Example:

for (int i = 0; i < 10; i++) {
    if (i == 5)
        break;  // loop stops when i == 5
    if (i % 2 == 0)
        continue;  // skip even numbers
    System.out.println(i);  // prints odd numbers less than 5
}
Scroll to Top