0% found this document useful (0 votes)
2 views

Java

The document explains the scope of variables in Java, detailing four main types: Local Scope, Instance Scope, Static Scope, and Block Scope, each with specific access and lifetime characteristics. It also discusses different types of operators in Java, including Unary, Arithmetic, Shift, Relational, Bitwise, Logical, Ternary, and Assignment operators. Additionally, it illustrates the if-else statement with syntax and an example that checks if a number is even or odd.

Uploaded by

yegera2358
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java

The document explains the scope of variables in Java, detailing four main types: Local Scope, Instance Scope, Static Scope, and Block Scope, each with specific access and lifetime characteristics. It also discusses different types of operators in Java, including Unary, Arithmetic, Shift, Relational, Bitwise, Logical, Ternary, and Assignment operators. Additionally, it illustrates the if-else statement with syntax and an example that checks if a number is even or odd.

Uploaded by

yegera2358
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q 1] Explain scope of variable in detail?

Ans: - The scope of a variable in Java refers to the part of the program where
the variable can be accessed. It defines the lifetime and visibility of a variable
within the code. Once, a variable is declared, we can access and manipulate its
data according to our needs. Java has four main types of variable scopes.
1. Local Scope: - A local variable is declared inside a method, constructor, or
block and can only be accessed within that method or block. These variables
are created when the method starts and destroyed when the method ends.
They do not have default values, so they must be initialized before use.
2. Instance Scope (Global Scope): -An instance variable is declared inside a
class but outside methods and is associated with an object. Each object of the
class gets its own copy of instance variables, -which are stored in heap memory
and exist as long as the object exists.
3. Static (Class) Scope: - A static variable is declared inside a class using the
static keyword and belongs to the class itself, meaning all objects share the
same variable. These variables are stored in the Method Area and remain in
memory for the program’s lifetime. They can be accessed using the class name,
without needing an object.
4. Block Scope: - A variable declared inside a loop, if statement, or {} block has
block scope and only exists within that block. Once the block execution ends,
the variable is removed from memory, helping to keep temporary values
confined and prevent interference with other variables.
Q 2] Discus different operators in java?
Ans: - Java operators are special symbols that perform operations on variables or values.
They can be classified into several categories based on their functionality. These operators
play a crucial role in performing arithmetic, logical, relational, and bitwise operations etc.
There are many types of operators in java which are given bellow: -
Unary , Arithmetic , Shift , Relational , Bitwise , Logical , Ternary , Assignment
Q 3] Illustrate if else statement with syntax and example?
Ans: - The if-else statement in Java is a powerful decision-making tool used to
control the program’s flow based on conditions. It executes one block of code if
a condition is true and another block if the condition is false.
Syntax of if-else Statement:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example to Check Even or Odd Number.

public class IfElseExample {


public static void main (String [] args) {
int number = 10; // Declare and initialize a number

if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
Output: - 10 is even.

You might also like