UNIVERSITY OF SOMALIA (UNISO)
السالم عليكم ورحمة
الله وبركاته
Group D Slide: 1 of 17 November 16, 2024
Outlines
4.8: More about Variable Declaration
and Scope
4.9: The Conditional Operator
Group D Slide: 2 of 17 November 16, 2024
4.8: More about Variable
Declaration and Scope
Group D Slide: 3 of 17 November 16, 2024
Variable Declaration
When you declare a variable in Java, you're essentially creating a
storage location in memory to hold a specific value.
Data type: This determines the kind of value the variable can store (e.g.,
int for integers, double for floating-point numbers, String for text).
Initialization (optional): You can assign an initial value to the variable
during declaration.
Group D Slide: 4 of 17 November 16, 2024
Example
int age = 25; // Declares an integer variable named 'age' and
initializes it to 25
String name = "Alice"; // Declares a String variable named
'name' and initializes it to "Alice"
Group D Slide: 5 of 17 November 16, 2024
Variable Scope
The scope of a variable defines the region of code where it's accessible.
There are two main scopes in Java:
1. Local Scope:
Variables declared within a block (e.g., inside loop, or if-else statement)
have local scope.
They can only be accessed within that block.
Once the block ends, the variable is destroyed.
Group D Slide: 6 of 17 November 16, 2024
Continue
2. Instance/member Scope:
Variables declared outside of any method or block within a
class have instance scope.
They are associated with each instance of the class.
They can be accessed from any method within the class.
Group D Slide: 7 of 17 November 16, 2024
Continue
Group D Slide: 8 of 17 November 16, 2024
Key Points
Variables must be declared before they can be used.
The data type of a variable determines the kind of values it can hold.
The scope of a variable determines where it can be accessed within a
program.
Local variables are typically used for temporary values within a specific
block of code.
Instance variables are used to store data that's associated with each
instance of a class.
Group D Slide: 9 of 17 November 16, 2024
4.9: The Conditional Operator
Group D Slide: 10 of 17 November 16, 2024
4.9 The Conditional Operator
The conditional operator also known as the ternary operator, is a
shorthand way of writing an if-else statement in Java. It provides a
concise way to evaluate a condition and return one of two values based
on the result.
Group D Slide: 11 of 17 November 16, 2024
Syntax
condition ? expression1 : expression2
condition: This is an expression that evaluates to either true or false.
expression1: This is the expression that is evaluated if the condition is
true.
expression2: This is the expression that is evaluated if the condition is
false.
Group D Slide: 12 of 17 November 16, 2024
Example1
Group D Slide: 13 of 17 November 16, 2024
Example2
Group D Slide: 14 of 17 November 16, 2024
When to use the conditional operator
When you have a simple condition and want to
return one of two values based on the result.
When you want to write more concise code.
Group D Slide: 15 of 17 November 16, 2024
be cautious when using the
conditional operator:
Avoid nesting too many conditional operators
within each other, as it can make your code difficult
to read and understand.
If the expressions involved are complex, it might be
clearer to use an if-else statement instead.
Group D Slide: 16 of 17 November 16, 2024
Thank you All of You
Group D Slide: 17 of 17 November 16, 2024