IT103 - Java Methods II Flashcards
________ are used to carry out specific actions
and are sometimes called functions.
Methods
It is group of code that performs a specific task or operation. It is a collection of statements that work together to complete a particular job.
Method in Java/ Method
What are the benefits of using methods in Java?
- They keep code organized and neat.
- They allow code reuse.
- They save time and reduce mistakes.
- They keep your code organized and neat.
It is the starting point of a Java program and is the first method executed by the Java Virtual Machine (JVM).
main() method
JVM stands for
Java Virtual Machine
Methods in Java are _______ _________ of ________ designed to perform specific tasks.
reusable blocks of
code
Methods help break ______ , ______
programs into ______ , easier-to-handle parts.
- large
- complex
- smaller
public static int add (int a, int b) {
int result;
result = a +b;
return result;
}
What do you call the:
1. public
2. int (after the static)
3. (int a, int b)
4. return result;
- Access Modifier
- Return Type
- List of Parameters
- Return Value
public static int add (int a, int b) {
int result;
result = a +b;
return result;
}
What do you call the “public static int add (int a, int b) { “ part ?
Method Header
public static int add (int a, int b) {
int result;
result = a +b;
return result;
}
What do you call the “int result; up to return result; “ part ?
Method Body
What are the components of a Java method?
- Access specifier
- Return type
- Method name
- Parameters
- Method body
- Return statement
It determines who can use the class, method, or field (e.g., public, private).
accessSpecifier
It specifies the type of value the method returns. If no value is returned, the keyword void is used.
returnType
The name of the method, written in camelCase (e.g., calculateSum).
methodName
Input values that the method can accept. A method can have zero or more parameters, each with a data
type and name.
parameter
The code inside curly braces {} that defines what the method does.
method body
If the method has a return type (not void), it must include a ________ _________ followed by the value to be sent back.
return statement
What are relational operators in Java?
1) == (Equal To): Checks if two values are equal.
2) != (Not Equal To): Checks if two values are not equal.
3) > (Greater Than): Checks if the left operand is greater.
4) < (Less Than): Checks if the left operand is lesser.
5) >= (Greater Than or Equal To)
6) <= (Less Than or Equal To)
What are the logical operators in Java?
1) && (AND): Returns true only if both conditions are true.
2) || (OR): Returns true if at least one condition is true.
3) ! (NOT): Reverses the result of a condition.
________ _________ help make decisions in a program using conditions.
Selection statements
What selection statement is used for multiple conditions?
if-else-if ladder
Executes one block of code if the condition is true and another
block if the condition is false.
if-else Statement
A selection statement that executes code if a condition is true
if Statement
A shorthand for if-else that returns a value based on a condition.
Ternary Operator (? :)