Java

📘 Java Topic 1: What is Java?

🔹 Java Definition

Java is a high-level, object-oriented, platform-independent programming language used to develop software, web, desktop, mobile, and automation tools.

📖 Nepali Insight:
Java एकदमै popular programming language हो जुन एकपटक लेखेर धेरै platform मा चलाउन सकिन्छ (write once, run anywhere concept)।

🔹 Why Java is Important for QA Engineers

ReasonQA Use
Selenium SupportMost Selenium frameworks use Java
Test FrameworksTestNG, JUnit built in Java
Backend TestingUsed in REST API, database layers
OOP ConceptsImportant for Page Object Model
Industry DemandJava QA jobs are in high demand

🔹 Java Program Structure Example

java

CopyEdit

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println(“Hello, Lok!”);

    }

}

📖 Explanation:

  • public class HelloWorld: Class name
  • main(): Entry point
  • System.out.println(): Print to console

🔹 Key Java Features

FeatureDescription
Object-OrientedEverything is based on objects
Platform IndependentCompile once, run anywhere (via JVM)
Strongly TypedEvery variable must have a type
MultithreadedSupports parallel execution
Secure & RobustBuilt-in memory management

🔹 Basic Java Terms

TermMeaning
ClassBlueprint or template
ObjectInstance of a class
MethodAction performed (like function)
VariableStores value
Data TypeType of data (int, String, boolean)

📘 Java Topic 2: Data Types, Variables, and Operators

🔹 What is a Variable?

A variable is a container in memory that stores data. You can change the value stored in it during program execution.

📖 Nepali View:
Variable भन्नाले data राख्न प्रयोग गरिने memory को नाम हो। यो changeable हुन्छ।

🧠 Example:

java

CopyEdit

int age = 30;

String name = “Sam”;

🔹 Types of Variables in Java

TypeDescription
Local VariableDefined inside a method
Instance VariableDefined inside a class but outside any method
Static VariableShared by all objects (declared with static)

🔹 What is a Data Type?

A data type defines the type of value a variable can hold (integer, decimal, character, etc.).

📖 Nepali Insight:
Data Type ले variable भित्र कस्तो प्रकारको value राख्न सकिन्छ भनेर define गर्छ

✅ Java Data Types

🔸 1. Primitive Data Types (8 total)

TypeSizeExampleUse
int4 bytesint age = 25;Whole number
float4 bytesfloat pi = 3.14f;Decimal with f
double8 bytesdouble salary = 9999.99;Large decimal
char2 byteschar grade = ‘A’;Single character
boolean1 bitboolean status = true;True/False
byte1 bytebyte x = 100;Small number
short2 bytesshort y = 30000;Medium number
long8 byteslong z = 123456789L;Big integer with L

🔸 2. Non-Primitive Data Types

TypeDescriptionExample
StringTextString name = “Lok”;
ArrayMultiple valuesint[] nums = {1, 2, 3};
ClassCustom typePerson lok = new Person();

🔹 Java Operators

Operators are symbols that perform operations on variables and values.

📖 Nepali Insight:
Operator भन्नाले कुनै काम गराउने चिन्ह हो, जस्तै जोड्ने, घटाउने, compare गर्ने आदि।

✅ Types of Operators

CategoryOperatorsExample
Arithmetic+, -, *, /, %a + b, a % b
Relational==, !=, <, >, <=, >=a > b
Logical&&, ` 
Assignment=, +=, -=, *=a += 5
Unary++, —i++, –i
Ternary? :a > b ? a : b

🧠 Example:

java

CopyEdit

int a = 5;

int b = 10;

int max = (a > b) ? a : b;

✅ Summary

ConceptExampleQA Use
Variableint age = 30;Store dynamic data
Data Typeboolean isValid = true;Define type of value
Operatorif(a > b)Perform logic in code

📘 Java Topic 3: Conditional Statements (if, else, switch)

🔹 What are Conditional Statements?

Conditional statements allow your program to make decisions — to execute certain code only if a specific condition is true.

📖 Nepali Insight:
Conditional statement ले भन्न सक्छ — “यदि यस्तो भयो भने यस्तो गर, नभए अर्कै गर।”

✅ 1. if Statement

Executes a block of code only if the condition is true.

java

CopyEdit

int age = 18;

if (age >= 18) {

    System.out.println(“Eligible to vote”);

}

📖 Nepali: यदि उमेर १८ वा बढी छ भने मतदान गर्न मिल्छ।

✅ 2. if…else Statement

Adds an alternate block to execute if the condition is false.

java

CopyEdit

if (age >= 18) {

    System.out.println(“Eligible to vote”);

} else {

    System.out.println(“Not eligible”);

}

✅ 3. if…else if…else

Used to check multiple conditions.

java

CopyEdit

int score = 75;

if (score >= 90) {

    System.out.println(“Grade A”);

} else if (score >= 75) {

    System.out.println(“Grade B”);

} else {

    System.out.println(“Grade C”);

}

📖 Nepali: कुन-कुन level मा के काम गर्ने भनेर step-wise check गर्न।

✅ 4. switch Statement

Used to select one of many code blocks to execute — better than using many else if for specific cases.

java

CopyEdit

int day = 3;

switch (day) {

    case 1: System.out.println(“Sunday”); break;

    case 2: System.out.println(“Monday”); break;

    case 3: System.out.println(“Tuesday”); break;

    default: System.out.println(“Invalid”);

}

📖 Nepali: दिन अनुसार के देखाउने भन्ने कुरा चुनिन्छ।

🔹 Notes on switch:

  • break prevents fall-through (stops execution after match)
  • default is optional but handles unmatched cases

✅ When to Use What?

Use CaseStatement
One simple conditionif
Either this or thatif…else
Multiple conditionsif…else if…else
Multiple known fixed valuesswitch

🧪 QA Use Example

You may use these in:

  • Test data generation logic
  • Conditional validation scripts
  • Flow control in automation framework (TestNG, Selenium)

📘 Java Topic 4: Loops (for, while, do-while)

🔹 What is a Loop?

A loop is used to execute a block of code repeatedly until a specific condition is met.

✅ 1. for Loop

Used when the number of iterations is known or fixed.

java

CopyEdit

for (int i = 1; i <= 5; i++) {

    System.out.println(“Hello ” + i);

}

📖 Example Output:

nginx

CopyEdit

Hello 1

Hello 2

Hello 3

Hello 4

Hello 5

✅ 2. while Loop

Used when the number of iterations is unknown, but depends on a condition.

java

CopyEdit

int i = 1;

while (i <= 5) {

    System.out.println(“Hello ” + i);

    i++;

}

📖 Nepali: while ले पहिले condition check गर्छ अनि मात्र loop चलाउँछ।

✅ 3. do…while Loop

  • Executes the loop at least once, even if the condition is false at first.
  • Condition is checked after one full execution.

java

CopyEdit

int i = 1;

do {

    System.out.println(“Hello ” + i);

    i++;

} while (i <= 5);

🧠 Use this when you want the action to happen at least one time.

🔹 Loop Flow Summary

Loop TypeCondition CheckedRuns at least once?
forBefore startOnly if true
whileBefore startOnly if true
do…whileAfter first execution✅ Always once

🔹 Common QA Use Cases

  • Data-driven testing: Loop through data
  • Retry logic: Keep trying until status = “Success”
  • Batch verification: Loop over rows, items, or fields

🔹 Example – Print Even Numbers from 1 to 10

java

CopyEdit

for (int i = 1; i <= 10; i++) {

    if (i % 2 == 0) {

        System.out.println(i);

    }

}

📖 Output:

CopyEdit

2

4

6

8

10

✅ Bonus: Enhanced for-each Loop (For Arrays/Collections)

java

CopyEdit

String[] cities = {“Dallas”, “Austin”, “Houston”};

for (String city : cities) {

    System.out.println(city);

}

📘 Java Topic 5: Arrays and Strings

🔹 What is an Array?

An array is a data structure that stores multiple values of the same type in a single variable, using an index to access each element.

📖 Nepali Insight:
Array भन्नाले एकै नाममा धेरै समान प्रकारका data राख्न मिल्ने list हो — जसमा प्रत्येक item को number (index) हुन्छ।

✅ Array Declaration & Initialization

java

CopyEdit

int[] numbers = {10, 20, 30, 40, 50};

🧠 Indexing starts from 0:

  • numbers[0] = 10
  • numbers[1] = 20

🔹 Accessing Array Elements

java

CopyEdit

System.out.println(numbers[2]); // Output: 30

🔹 Looping Through Array

java

CopyEdit

for (int i = 0; i < numbers.length; i++) {

    System.out.println(numbers[i]);

}

✅ Output:

CopyEdit

10

20

30

40

50

🔹 for-each Loop

java

CopyEdit

for (int num : numbers) {

    System.out.println(num);

}

📖 Nepali: Array का सबै elements एक-एक गरेर access गर्न सजिलो तरीका।

🔹 String in Java

A String is a sequence of characters. It is an object in Java, not a primitive data type.

java

CopyEdit

String name = “Lok QA”;

✅ String Methods

MethodPurposeExample
length()Length of stringname.length() → 6
charAt(index)Character at indexname.charAt(0) → ‘L’
toUpperCase()Convert to capsname.toUpperCase()
toLowerCase()Convert to smallname.toLowerCase()
equals()Compare (case sensitive)“Lok”.equals(“lok”) → false
equalsIgnoreCase()Compare (ignore case)→ true
contains()Check substringname.contains(“QA”) → true
substring(x, y)Extract partname.substring(0, 3) → “Lok”
split()Break string into parts“A-B-C”.split(“-“)

🔹 String Concatenation

java

CopyEdit

String first = “Lok”;

String last = “Subedi”;

String full = first + ” ” + last;

System.out.println(full);  // Sam Wilson

🔹 Immutable Nature of Strings

Strings in Java are immutable — once created, they cannot be changed.

📖 Nepali: String लाई change गरेजस्तो लागे पनि Java मा actually नयाँ object बन्छ।

✅ Use StringBuilder or StringBuffer for mutable strings in performance-heavy applications.

🧪 QA Use Examples:

  • Parse test data (e.g., split values)
  • Validate text from UI/API
  • Loop over data sets stored in arrays

📘 Java Topic 6: Object-Oriented Programming (OOPs) Concepts

🔹 What is OOP?

OOP (Object-Oriented Programming) is a programming paradigm where real-world entities like objects, classes, and relationships are used to design software.

📖 Nepali Insight:
OOP भन्नाले सबै कुरा object (वास्तविक वस्तु) को रूप मा सोची software design गर्ने तरिका हो।

Scroll to Top