Understanding the difference between JDK, JRE, and JVM plays a vital role
in understanding how Java works and how each component contributes to the
development and execution of Java applications.
JDK: JDK stands for Java Development Kit. It is a set of development tools
and libraries used to create Java programs. It works together with the JVM
and JRE to run and build Java applications..
JRE: JRE stands for Java Runtime Environment, and it provides an
environment to run Java programs on the system. The environment
includes Standard Libraries and JVM.
JVM: JVM stands for Java Virtual Machine. It's responsible for executing
the Java program.
JDK vs JRE vs JVM
Aspect JDK JRE JVM
Used to develop Used to run Java Executes Java
Purpose Java applications applications bytecode
JVM is OS-
Platform- Platform- specific, but
dependent (OS dependent (OS bytecode is
Platform specific) specific) platform-
Dependency independent
JRE +
ClassLoader, JIT
Development tools JVM + Libraries
Compiler, Garbage
(javac, debugger, (e.g., rt.jar)
Collector
Includes etc.)
Writing and Running a Java Convert bytecode
compiling Java application on a into native
Use Case code system machine code
Note: The JVM is platform-independent in the sense that the bytecode can
run on any machine with a JVM, but the actual JVM implementation is
platform-dependent. Different operating systems (e.g., Windows, Linux,
macOS) require different JVM implementations that interact with the specific
OS and hardware
JDK (Java Development Kit)
The JDK is a software development kit that provides tools to develop and run
Java applications. It includes two main components:
Development Tools (to provide an environment to develop your java
programs)
JRE (to execute your java program)
Note:
JDK is only for development (it is not needed for running Java programs)
JDK is platform-dependent (different version for windows, Linux, macOS)
Working of JDK
The JDK enables the development and execution of Java programs. Consider
the following process:
Java Source File (e.g., Example.java): You write the Java program in a
source file.
Compilation: The source file is compiled by the Java Compiler (part of
JDK) into bytecode, which is stored in a .class file (e.g., Example.class).
Execution: The bytecode is executed by the JVM (Java Virtual Machine),
which interprets the bytecode and runs the Java program.
Working of JDK
Note: From above, media operation computing during the compile time can
be interpreted.
The following actions occur at runtime as listed below:
Class Loader
Byte Code Verifier
Interpreter
o Execute the Byte Code
o Make appropriate calls to the underlying hardware
JRE ((Java Runtime Environment)
The JRE is an installation package that provides an environment to only
run(not develop) the Java program (or application) onto your machine. JRE
is only used by those who only want to run Java programs that are end-users
of your system.
Note:
JRE is only for end-users (not for developers).
JRE is platform-dependent (different versions for different OS)
Working of JRE
When you run a Java program, the following steps occur:
Class Loader: The JRE’s class loader loads the .class file containing the
bytecode into memory.
Bytecode Verifier: JRE includes a bytecode verifier to ensure security
before execution
Interpreter: JVM uses an interpreter + JIT compiler to execute bytecode
for optimal performance
Execution: The program executes, making calls to the underlying
hardware and system resources as needed.
JVM (Java Virtual Machine)
The JVM is a very important part of both JDK and JRE because it is contained
or inbuilt in both. Whatever Java program you run using JRE or JDK goes into
JVM and JVM is responsible for executing the java program line by line, hence
it is also known as an interpreter.
Note:
JVM is platform -dependent (different JVMs for window, linux, macOS).
Bytecode (.class files) is platform-independent (same file runs in any JVM).
While JVM includes an interpreter, modern implementations primarily use
JIT compilation for faster execution
Working of JVM
It is mainly responsible for three activities.
Loading
Linking
Initialization
Java Identifiers
An identifier in Java is the name given to Variables, Classes, Methods, Packages,
Interfaces, etc. These are the unique names used to identify programming elements.
Every Java Variable must be identified with a unique name.
Example:
public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}
In the above Java code, we have 5 identifiers as follows:
Test: Class Name
main: Method Name
String: Predefined Class Name
args: Variable Name
a: Variable Name
Rules For Naming Java Identifiers
There are certain rules for defining a valid Java identifier. These rules must
be followed, otherwise, we get a compile-time error. These rules are also valid
for other languages like C and C++.
The only allowed characters for identifiers are all alphanumeric
characters([A-Z],[a-z],[0-9]), '$'(dollar sign) and '_' (underscore). For
example, "geek@" is not a valid Java identifier as it contains a '@', a special
character.
Identifiers should not start with digits([0-9]). For example, "123geeks" is
not a valid Java identifier.
Java identifiers are case-sensitive.
There is no limit on the length of the identifier, but it is advisable to use an
optimum length of 4 - 15 letters only.
Reserved Words can't be used as an identifier. For example, "int while =
20;" is an invalid statement as a while is a reserved word.
Note: Java has 53 reserved words (including 50 keywords and 3 literals), that
are not allowed to be used as identifiers.
Examples of Valid Identifiers
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123
Examples of Invalid Identifiers
My Variable // contains a space
123geeks // Begins with a digit
a+c // plus sign is not an alphanumeric character
variable-2 // hyphen is not an alphanumeric character
sum_&_difference // ampersand is not an alphanumeric character
Reserved Words in Java
Any programming language reserves some words to represent functionalities
defined by that language. These words are called reserved words. They can
be briefly categorized into two parts:
Keywords (50): Keywords define functionalities.
literals (3): Literals define value.
Identifiers are stored by symbol tables and used during the lexical,
syntax, and semantic analysis phase of compilation.
List of Java Reserved Words
Transient
Abstract continue for protected
Assert Default Goto public Try
Boolean Do If Static throws
break double implements strictfp Package
byte else import super Private
case enum Interface Short switch
Catch Extends instanceof return void
Char Final Int synchronized volatile
class finally long throw Date
const float Native This while
Note: The keywords const and goto are reserved, even though they are not
currently used in Java. In place of const, the final keyword is used. Some
keywords like strictfp are included in later versions of Java.
JAVA KEYWORDS
In Java, keywords are the reserved words that have some predefined
meanings and are used by the Java compiler for some internal process or
represent some predefined actions. These words cannot be used as identifiers
such as variable names, method names, class names, or object names.
Java Keywords List
Java contains a list of keywords or reserved words which are also
highlighted with different colors be it an IDE or editor in order to segregate
the differences between flexible words and reserved words. As of Java
21, there are 53 keywords defined in Java. They are listed below in the
table with the primary action associated with them.
Keywords Usage
Specifies that a class or method will be
abstract
implemented later, in a subclass
Assert describes a predicate placed in a Java
assert program to indicate that the developer thinks that
the predicate is always true at that place.
A data type that can hold True and False values
boolean
only
break A control statement for breaking out of loops.
byte A data type that can hold 8-bit data values
case Used in switch statements to mark blocks of text
catch Catches exceptions generated by try statements
A data type that can hold unsigned 16-bit
char
Unicode characters
class Declares a new class
const Reserved but not used
continue Sends control back outside a loop
Specifies the default block of code in a switch
default
statement
Keywords Usage
do Starts a do-while loop
A data type that can hold 64-bit floating-point
double
numbers
else Indicates alternative branches in an if statement
A Java keyword is used to declare an
enum enumerated type. Enumerations extend the base
class.
Indicates that a class is derived from another
extends
class or interface
Indicates that a variable holds a constant value
final
or that a method will not be overridden
Indicates a block of code in a try-catch structure
finally
that will always be executed
A data type that holds a 32-bit floating-point
float
number
for Used to start a for loop
goto Reserved but not used
Tests a true/false expression and branches
if
accordingly
implements Specifies that a class implements an interface
import References other classes
Indicates whether an object is an instance of a
instanceof
specific class or implements an interface
Keywords Usage
int A data type that can hold a 32-bit signed integer
interface Declares an interface
long A data type that holds a 64-bit integer
Specifies that a method is implemented with
native
native (platform-specific) code
new Creates new objects
This indicates that a reference does not refer to
null
anything
package Declares a Java package
An access specifier indicating that a method or
private variable may be accessed only in the class it's
declared in
An access specifier indicating that a method or
variable may only be accessed in the class it's
protected declared in (or a subclass of the class it's
declared in or other classes in the same
package)
An access specifier used for classes, interfaces,
methods, and variables indicating that an item is
public
accessible throughout the application (or where
the class that defines it is accessible)
Sends control and possibly a return value back
return
from a called method
short A data type that can hold a 16-bit integer
Keywords Usage
Indicates that a variable or method is a class
static method (rather than being limited to one
particular object)
A Java keyword is used to restrict the precision
strictfp and rounding of floating-point calculations to
ensure portability.
Refers to a class's base class (used in a method
super
or class constructor)
A statement that executes code based on a test
switch
value
Specifies critical sections or methods in
synchronized
multithreaded code
Refers to the current object in a method or
this
constructor
throw Creates an exception
Indicates what exceptions may be thrown by a
throws
method
Specifies that a variable is not part of an object's
transient
persistent state
Starts a block of code that will be tested for
try
exceptions
Specifies that a method does not have a return
void
value
This indicates that a variable may change
volatile
asynchronously
Keywords Usage
while Starts a while loop
The sealed keyword is used to declare a class
sealed as "sealed," meaning it restricts which classes
can extend it.
The permits keyword is used within a sealed
permits class declaration to specify the subclasses that
are permitted to extend it.
Java Data Types
Java is statically typed and also a strongly typed language because each type
of data, such as integer, character, hexadecimal, packed decimal etc. is
predefined as part of the programming language, and all constants or variables
defined for a given program must be declared with the specific data types.
Data types in Java are of different sizes and values that can be stored in a
variable that is made as per convenience and circumstances to handle different
scenarios or data requirements.
Why Data Types Matter in Java?
Data types matter in Java because of the following reasons, which are listed
below:
Memory Efficiency: Choosing the right type (byte vs int) saves memory.
Performance: Proper types reduce runtime errors.
Code Clarity: Explicit typing makes code more readable.
Java Data Type Categories
Java has two categories in which data types are segregated
1. Primitive Data Type: These are the basic building blocks that store simple
values directly in memory. Examples of primitive data types are
boolean
char
byte
short
int
long
float
double
Note: The Boolean with uppercase B is a wrapper class for the primitive
boolean type.
2. Non-Primitive Data Types (Object Types): These are reference types that
store memory addresses of objects. Examples of Non-primitive data types are
String
Array
Class
Interface
Object
The below diagram demonstrates different types of primitive and non-
primitive data types in Java.
Java Variables
In Java, variables are containers that store data in memory. Understanding
variables plays a very important role as it defines how data is stored,
accessed, and manipulated.
Key Components of Variables in Java:
A variable in Java has three components, which are listed below:
Data Type: Defines the kind of data stored (e.g., int, String, float).
Variable Name: A unique identifier following Java naming rules.
Value: The actual data assigned to the variable.
Note: There are three types of variables in Java: Local, Instance and Static.
variable Declaration
While declaring a variable, we need to take care of two things that are:
1. data type: In Java, a data type define the type of data that a variable can
hold.
2. variable name: Must follow Java naming conventions (e.g., camelCase).
In this way, a name can only be given to a memory location. It can be
assigned values in two ways:
Variable Initialization
Assigning value by taking input
Variable Initialization
class Geeks{
public static void main(String[] args) {
// Declaring and initializing variables
// Initializing float variable
float si = 5.5f;
// Initializing integer variables
int t = 10;
int s = 20;
// Initializing character variable
char var = 'h';
// Displaying the values of the variables
System.out.println("Simple Interest: " + si);
System.out.println("Speed: " + s);
System.out.println("Time: " + t);
System.out.println("Character: " + var);
}
}
Output
Simple Interest: 5.5
Speed: 20
Time: 10
Character: h
Types of Java Variables
Now let us discuss different types of variables which are listed as follows:
Local Variables
Instance Variables
Static Variables
Type of Variable
Let us discuss the traits of every type of variable listed here in detail.
1. Local Variables
A variable defined within a block or method or constructor is called a local
variable.
The Local variable is created at the time of declaration and destroyed
when the function completed its execution.
The scope of local variables exists only within the block in which they are
declared.
We first need to initialize a local variable before using it within its scope.
Example: This example show how a local variable is declared and used
inside the main method and it can not be used outside of it.
// Java Program to show the use of local variables
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
Output
Local Variable: 10
Example: This example demonstrates that local variables are only accessible
within the block in which they are declared
// Java Program to show the use of
// Local Variables
import java.io.*;
public class Geeks {
public static void main(String[] args)
{
// x is a local variable
int x = 10;
// message is also a local
// variable
String message = "Hello, world!";
System.out.println("x = " + x);
System.out.println("message = " + message);
if (x > 5) {
// result is a
// local variable
String result = "x is greater than 5";
System.out.println(result);
}
// Uncommenting the line below will result in a
// compile-time error System.out.println(result);
for (int i = 0; i < 3; i++) {
String loopMessage
= "Iteration "
+ i; // loopMessage is a local variable
System.out.println(loopMessage);
}
// Uncommenting the line below will result in a
// compile-time error
// System.out.println(loopMessage);
}
}
Output
x = 10
message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
2. Instance Variables
Instance variables are known as non-static variables and are declared in a
class outside of any method, constructor, or block.
Instance variables are created when an object of the class is created and
destroyed when the object is destroyed.
Unlike local variables, we may use access specifiers for instance variables.
If we do not specify any access specifier, then the default access specifier
will be used.
Initialization of an instance variable is not mandatory. Its default value is
dependent on the data type of variable. For String it
is null, for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it
is null, etc.
Scope of instance variables are throughout the class except the static
contexts.
Instance variables can be accessed only by creating objects.
We initialize instance variables using constructors while creating an object.
We can also use instance blocks to initialize the instance variables.
Example: This example demonstrates the use of instance variables, which
are declared within a class and initialized via a constructor, with default
// Java Program to show the use of
// Instance Variables
import java.io.*;
class Geeks {
// Declared Instance Variable
public String geek;
public int i;
public Integer I;
public Geeks()
{
// Default Constructor
// initializing Instance Variable
this.geek = "Sweta Dash";
}
// Main Method
public static void main(String[] args)
{
// Object Creation
Geeks name = new Geeks();
// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "+ name.i);
// toString() called internally
System.out.println("Default value for Integer is: "+ name.I);
}
}
Output
Geek name is: Sweta Dash
Default value for int is 0
Default value for Integer is: null
3. Static Variables
Static variables are also known as class variables.
These variables are declared similarly to instance variables. The difference
is that static variables are declared using the static keyword within a class
outside of any method, constructor, or block.
Unlike instance variables, we can only have one copy of a static variable per
class, irrespective of how many objects we create.
Static variables are created at the start of program execution and destroyed
automatically when execution ends.
Initialization of a static variable is not mandatory. Its default value is
dependent on the data type of variable. For String it is null, for float it is 0.0f,
for int it is 0, for Wrapper classes like Integer it is null, etc.
If we access a static variable like an instance variable (through an object),
the compiler will show a warning message, which won't halt the program.
The compiler will replace the object name with the class name automatically.
If we access a static variable without the class name, the compiler will
automatically append the class name. But for accessing the static variable
of a different class, we must mention the class name as 2 different classes
might have a static variable with the same name.
Static variables cannot be declared locally inside an instance method.
Static blocks can be used to initialize static variables.
Example: This example demonstrates the use of static variables, which belong
to the class and can be accessed without creating an object of the class.
// Java Program to show the use of
// Static variables
import java.io.*;
class Geeks {
// Declared static variable
public static String geek = "Sweta Dash";
public static void main(String[] args)
{
// geek variable can be accessed without object
// creation Displaying O/P Geeks.geek --> using the
// static variable
System.out.println("Geek Name is: " + Geeks.geek);
// static int c = 0;
// above line, when uncommented,
// will throw an error as static variables cannot be
// declared locally.
}
}
Output
Geek Name is: Sweta Dash
Instance Variables vs Static Variables
Now let us discuss the differences between the Instance variables and the
Static variables:
Each object will have its own copy of an instance variable, whereas we can
only have one copy of a static variable per class, irrespective of how many
objects we create. Thus, static variables are good
for memory management.
Changes made in an instance variable using one object will not be
reflected in other objects as each object has its own copy of the instance
variable. In the case of a static variable, changes will be reflected in other
objects as static variables are common to all objects of a class.
We can access instance variables through object references, and static
variables can be accessed directly using the class name.
Instance variables are created when an object is created with the use of
the keyword 'new' and destroyed when the object is destroyed. Static
variables are created when the program starts and destroyed when the
program stops.
Syntax: Static and instance variables
class Geeks
{
static int a; // Static variable
int b; // Instance variable
}
Java Operators
Java operators are special symbols that perform operations on variables
or values. These operators are essential in programming as they allow you to
manipulate data efficiently. They can be classified into different categories
based on their functionality. We will explore different types of operators in Java,
including arithmetic, unary, relational, logical, and more.
Types of Operators in Java
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
Decision Making in Java (if, if-else, switch, break, continue, jump)
Decision-making statements in Java execute a block of code based on a
condition. Decision-making in programming is similar to decision-making
in real life. In programming, we also face situations where we want a certain
block of code to be executed when some condition is fulfilled.
A programming language uses control statements to control the flow of
execution of a program based on certain conditions. These are used to cause
the flow of execution to advance and branch based on changes to the state of
a program. Java provides several control statements to manage program flow,
including:
Conditional Statements: if, if-else, nested-if, if-else-if
Switch-Case: For multiple fixed-value checks
Jump Statements: break, continue, return
Types of Decision-Making Statements
if
if-else
nested-if
if-else-if
switch-case
jump - break, continue, return
The table below demonstrates various control flow statements in programming,
their use cases, and examples of their syntax.
Statement Use Case Example
if Single condition check if (age >= 18)
if-else Two-way decision if (x > y) {...} else {...}
nested-if Multi-level conditions if (x > 10) { if (y > 5) {...} }
if-else-if Multiple conditions if (marks >= 90) {...} else if (marks >= 80) {...}
switch-case Exact value matching switch (day) { case 1: ... }
break Exit loop/switch break;
continue Skip iteration continue;
return Exit method return result;
1. Java if Statement
The if statement is the most simple decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or
not i.e. if a certain condition is true then a block of statements is executed
otherwise not.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. if statement
accepts boolean values - if the value is true then it will execute the block of
statements under it. If we don't use curly braces( {} ), only the next line after
the if is considered as part of the if block For example,
if (condition) // Assume condition is true
statement1; // Belongs to the if block
statement2; // Does NOT belong to the if block
Here's what happens:
If the condition is True statement1 executes.
statement2 runs no matter what because it's not a part of the if block
if Statement Execution Flow
Example: The below Java program demonstrates without curly braces, only
the first line after the if statement is part of the if block and the rest code will
be execute independently.
// Java program to illustrate
// if statement without curly block
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
// part of if block(immediate one statement
// after if condition)
System.out.println("Inside If block");
// always executes as it is outside of if block
System.out.println("10 is less than 15");
// This statement will be executed
// as if considers one statement by default again
// below statement is outside of if block
System.out.println("I am Not in if");
}
}
Output
Inside If block
10 is less than 15
I am Not in if
2. Java if-else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won't. But what if we want to do
something else if the condition is false? Here, comes the "else" statement. We
can use the else statement with the if statement to execute a block of code
when the condition is false.
Syntax:
if(condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
if-else Statement Execution flow
Example: The below Java program demonstrates the use of if-else statement
to execute different blocks of code based on the condition.
// Java program to demonstrate
// the working of if-else statement
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output
i is smaller than 15
3. Java nested-if Statement
A nested if is an if statement that is the target of another if or else. Nested if
statements mean an if statement inside an if statement. Yes, java allows us to
nest if statements within if statements. i.e, we can place an if statement inside
another if statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
nested-if Statement Execution Flow
.
Example: The below Java program demonstrates the use of nested if
statements to check multiple conditions.
// Java program to demonstrate the
// working of nested-if statement
import java.util.*;
class SAMPLE
{
public static void main(String args[])
{
int i = 10;
if (i == 10 || i < 15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
}
else {
System.out.println("i is greater than 15");
}
}
}
Output
i is smaller than 15
i is smaller than 12 too
4. Java if-else-if ladder
Here, a user can decide among multiple options.The if statements are executed
from the top down. As soon as one of the conditions controlling the if is true,
the statement associated with that 'if' is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else statement will be
executed. There can be as many as 'else if' blocks associated with one 'if' block
but only one 'else' block is allowed with one 'if' block.
Syntax:
if (condition1)
{
// code to be executed if condition1 is true
} else if (condition2)
{
// code to be executed if condition2 is true
}
else
{
// code to be executed if all conditions are false
}
Example: This example demonstrates an if-else-if ladder to check multiple
conditions and execute the corresponding block of code based on the value of
I.
// Java program to demonstrate the
// working of if-else-if ladder
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output
i is 20
5. Java Switch Case
The switch statement is a multiway branch statement. It provides an easy way
to dispatch execution to different parts of code based on the value of the
expression.
Syntax:
switch (expression)
{
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// more cases...
default:
// code to be executed if no cases match
}
// Java program to demonstrates the
// working of switch statements
import java.io.*;
class SAMPLE
{
public static void main(String[] args)
{
int num = 20;
switch (num) {
case 5:
System.out.println("It is 5");
break;
case 10:
System.out.println("It is 10");
break;
case 15:
System.out.println("It is 15");
break;
case 20:
System.out.println("It is 20");
break;
default:
System.out.println("Not present");
}
}
}
Output
It is 20
The expression can be of type byte, short, int char, or an enumeration.
Beginning with JDK7, the expression can also be of type String.
Duplicate case values are not allowed.
The default statement is optional.
The break statement is used inside the switch to terminate a statement
sequence.
The break statements are necessary without the break keyword,
statements in switch blocks fall through.
If the break keyword is omitted, execution will continue to the next case.
6. jump Statements
Java supports three jump statements: break, continue and return. These
three statements transfer control to another part of the program.
Break: In Java, a break is majorly used for:
o Terminate a sequence in a switch statement (discussed above).
o To exit a loop.
o Used as a "civilized" form of goto.
Continue: Sometimes it is useful to force an early iteration of a loop. That
is, you might want to continue running the loop but stop processing the
remainder of the code in its body for this particular iteration. This is, in effect,
a goto just past the body of the loop, to the loop's end. The continue
statement performs such an action.
Example: The below Java Program demonstrates how the continue statement
skip the current iteration when a condition is true.
// Java program to demonstrates the use of
// continue in an if statement
import java.util.*;
class Geeks {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is even
// skip and continue
if (i % 2 == 0)
continue;
// If number is odd, print it
System.out.print(i + " ");
}
}
}
Output
1 3 5 7 9
Return Statement
The return statement is used to explicitly return from a method. That is, it
causes program control to transfer back to the caller of the method.
Example: The below Java program demonstrates how the return statements
stop a method and skips the rest of the code.
// Java program to demonstrate the use of return
import java.util.*;
public class Geeks {
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if (t)
return;
// Compiler will bypass every statement
// after return
System.out.println("This won't execute.");
}
}