Oops With Java - PDF Unit - 1
Oops With Java - PDF Unit - 1
sc III Sem)
UNIT - 1
OOPs Concepts and Java Programming:
Introduction to Object-Oriented concepts, procedural and object-oriented
programming paradigm
Java programming:
An Overview of Java, Java Environment, Data types, Variables,
constants, scope and life time of variables, operators, type conversion and
casting, Accepting Input from the Keyboard, Reading Input with
Java.util.Scanner Class, Displaying Output with System.out.printf(), Displaying
Formatted Output with String.format(), Control Statements.
Introduction to Object-Oriented concepts :
--------------------------------------------------------------
As the name suggests, Object-Oriented Programming or OOPs refers to
languages that use objects in programming. Object-oriented programming aims
to implement real-world entities like inheritance, hiding, polymorphism, etc in
programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code can access this
data except that function.
OOPs Concepts:
1. Class
2. Objects
3. Data Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
1. Class :
A class is like a blueprint for an object. A class is a user-defined data
type. It consists of data members and member functions, which can be accessed
and used by creating an instance of that class. It represents the set of properties
or methods that are common to all objects of one type.
Example:
Consider the Class of Cars. There may be many cars with different names
and brands but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class,
and wheels, speed limits, mileage are their properties.
2. Object :
It is a basic unit of Object-Oriented Programming and represents the real-life
entities. An Object is an instance of a Class. When a class is defined, no
memory is allocated but when it is instantiated (i.e. an object is created)
memory is allocated. An object has an identity, state, and behavior. Each object
contains data and code to manipulate the data. Objects can interact without
having to know details of each other’s data or code, it is sufficient to know the
type of message accepted and type of response returned by the objects.
example :
-------------
“Dog” is a real-life Object, which has some characteristics like colour,
Breed, Bark, Sleep, and Eats.
3. Data Abstraction:
Data abstraction is one of the most essential and important features of object-
oriented programming. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background details
or implementation. Consider a real-life example of a man driving a car. The man
only knows that pressing the accelerators will increase the speed of the car or
applying brakes will stop the car, but he does not know about how on pressing
the accelerator the speed is increasing, he does not know about the inner
mechanism of the car or the implementation of the accelerator, brakes, etc in the
car.
4. Encapsulation:
Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates. In
Encapsulation, the variables or data of a class are hidden from any other class
and can be accessed only through any member function of their class in which
they are declared. As in encapsulation, the data in a class is hidden from other
classes, so it is also known as data-hiding.
5. Inheritance:
Inheritance is an important pillar of OOP(Object-Oriented Programming). The
capability of a class to derive properties and characteristics from another class is
called Inheritance. When we write a class, we inherit properties from other
classes. So when we create a class, we do not need to write all the properties
and functions again and again, as these can be inherited from another class that
possesses it. Inheritance allows the user to reuse the code whenever possible
and reduce its redundancy.
6. Polymorphism:
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form. For example, A person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an employee.
Java programming:
An Overview of Java :
Java programming language was originally developed by
Sun Microsystems, which was initiated by James Gosling and released in 1995
as a core component of Sun Microsystems' Java platform.
The latest release of the Java Standard Edition is Java SE 23. With the
advancement of Java and its widespread popularity, multiple configurations
were built to suit various types of platforms. For example: J2EE for Enterprise
Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE, and Java ME,
respectively. Java is guaranteed to be Write Once, Run Anywhere.
Java is −
• Object-Oriented: In Java, everything is an object. Java can be easily
extended since it is based on the object model.
• Platform Independent: Unlike many other programming languages,
including C and C++, when Java is compiled, it is not compiled into a
platform-specific machine but rather into platform-independent byte
code. This byte code is distributed over the web and interpreted by the
virtual machine (JVM) on whichever platform it is being run on.
• Simple: Java is designed to be easy to learn. If you understand the
basic concept of OOP Java, it would be easy to master.
• Secure: With Java's secure feature, it enables the development of virus-
free, tamper-free systems. Authentication techniques are based on public-
key encryption.
Architecture-neutral: The Java compiler generates an architecture-neutral
object file format, which makes the compiled code executable on many
processors with the presence of the Java runtime system.
• Portable: Being architecture-neutral and having no implementation-
dependent aspects of the specification makes Java portable. Compiler in
Java is written in ANSI C with a clean portability boundary, which is a
POSIX subset.
• Robust: Java makes an effort to eliminate error-prone situations by
emphasizing mainly compile-time error checking and runtime checking.
Multithreaded: With Java's multithreaded feature, it is possible to write
programs that can perform many tasks simultaneously. This design
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
History of Java
James Gosling initiated Java language project in June 1991 for use in one of his
many set-top box projects. The language, initially called 'Oak' after an oak tree
that stood outside Gosling's office, also went by the name 'Green' and ended up
later being renamed as Java, from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It
promised Write Once Run Anywhere (WORA), providing no-cost run-times on
popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source
software under the terms of the GNU General Public License (GPL).
On 8 May, 2007, Sun finished the process, making all of Java's core code free
and open-source, aside from a small portion of code to which Sun did not hold
the copyright.
Java Environment
Java Environment is the setup that allows you to write, compile, and run
Java programs. It consists of JDK, JRE, and JVM.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit
(JDK) is a software development environment that is used to develop Java
applications. It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms
released by Oracle Corporation:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
The JDK contains a private Java Virtual Machine (JVM) and a few other
resources, such as an interpreter/loader (java), a compiler (javac), an archiver
(jar), a documentation generator (Javadoc), etc., to complete the development of
a Java Application.
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE.
The Java Runtime Environment is a set of software tools that are used for
developing Java applications. It is used to provide the runtime environment. It is
the implementation of JVM. It physically exists. It contains a set of libraries +
other files that the JVM uses at runtime.
Data Types :
In Java, data types define the type of data that a variable can hold.
They are categorized into two main types:
1. Primitive data types and
2. Non-primitive data types.
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
Syntax:
Datatypename variable name = value ;
boolean demo = true ;
Size : Virtual machine dependent (typically 1 byte, 8 bits)
2. byte Data Type
The byte data type is an 8-bit signed two's complement integer. The byte
data type is useful for saving memory in large arrays.
Syntax:
Datatypename variable name = value ;
byte value = 4;
Size : 1 byte (8 bits)
3. short Data Type
The short data type is a 16-bit signed two's complement integer.
Similar to byte, a short is used when memory savings matter, especially in
large arrays where space is constrained.
Syntax:
Syntax:
Datatypename variable name = value ;
double value = 123.34 ;
Size : 8 bytes (64 bits)
8. char Data Type
The char data type is a single 16-bit Unicode character with the size of 2
bytes (16 bits).
Syntax:
Datatypename variable name = value ;
char name = 'G' ;
Size : 2 bytes (16 bits)
Output :
------------------
char: G
integer: 89
byte: 4
short: 56
float: 4.7333436
double: 4.355453532
long: 12121
boolean: true
Output
Name: Good Morning
Message: Hello, World!
3. Arrays
Array is a non-primitive data type .
An array is a container object that holds a fixed number of value of a
single type.
1. One Dimensional Array
2. Two Dimensional array
Declaring and Initializing Array
1.Declaration:
int[] i; // Preferred
int i[]; // Also valid
2. Initialization:
i= new int[5]; // Creates an array of size 5
( Combined Declaration and Initialization)
3. Combined Declaration and Initialization:
int[] i = new int[5]; // Array with predefined values int i=10;
4. Declaration with values :
int[] x = {10, 20, 30, 40, 50};
5. Accessing Elements :
System.out.println(arr[0]); // Prints first element
arr[1] = 25; // Changes second element to 25
Variables
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.
Types of Java Variables
Now let us discuss different types of variables which are listed as follows:
• Instance Variables (non static variable)
• Class Variable (Static Variables)
• Local Variables
1. Instance Variables :
Instance variables is also known as non-static variables and in this
variable are declared in with in class only. We can’t create out side of the class.
• 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.
• Instance variables can be accessed only by creating objects.
Example :
Class InstanceExample{
Int x = 10;
Int y =30;
Int sum = x + y;
Public static void main ( Stirng [] args )
{
InstanceExample example = new InstanceExample ( );
System.out.println (“Sum of two values is : ” + example.sum);
}
}
}
Output :
Sum of two values is : 40
2. Class Variable :
Class variable is also known as instance variable or static variable. in this
variable are declared in with in class only. We can’t create out side of the class.
• 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.
• 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.
Example :
Class Example{
Static Int x = 50;
Static Int y =30;
Int sum = x - y;
Output :
Subtraction of two values is : 20
3. Local Variable
A variable defined within a block or method is called a local variable. We
can’t create out side of the method.
• 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 :
Class LocalExample{
Public static void main ( Stirng [] args )
{
Int x = 10;
Int y =30;
Int sub = x * y;
System.out.println (“Multiplication of two values is : ” + sub);
}
}
}
Output
Multiplication of two values is : 300
Constants
A constant in Java is a variable declared with the final keyword,
indicating that its value cannot be changed after it is assigned. Constants are
used to store values that should remain unchanged throughout the execution of a
Java program.
Constants are used for storing fixed values like mathematical constants,
configuration settings, or other unchanging data needed in various parts of the
code. Declaring a value as a constant enhances code readability, ensures data
consistency, and helps prevent accidental modifications, contributing to robust
and maintainable Java programs.
Types of Constants :
Numeric Constants
1. Integer Constants
1. Real Constants
Real constants represent numbers with fractional parts. They include a
decimal point and can be written in scientific notation as well.
These quantities are represented by numbers containing fractional parts like
17.548. such numbers are called real ( or floating point ) constants.
Example
public class FloatingPointConstantExample {
public static final double PI = 3.14159;
public static final double scientificNotation = 2.5e3; // 2500.0
public static void main(String[] args) {
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
2. Non-Numeric Constants
Non-numeric constants in Java refer to constants that are not directly
associated with numeric values. These constants are used to represent non-
numeric data, such as textual information, characters, boolean values, and more.
Let's explore some common types of non-numeric constants and their
explanations.
String Constants:
String constants are used to represent sequences of characters or textual
data. They are enclosed within double quotes and can include letters, numbers,
symbols, and spaces. String constants are often used for displaying messages,
labels, prompts, and any other form of textual information.
Example
public class StringConstantExample {
public static final String name = "Welcome to our application!";
public static void main(String[] args) {
System.out.println(name);
}
}
Output:
Welcome to our application!
Character Constants:
Character constants represent single characters and are enclosed within
single quotes. They are commonly used to represent individual characters, such
as letters, digits, and symbols.
Example
public class CharacterConstantExample {
public static final char firstLetter = 'A';
public static void main(String[] args) {
char secondLetter = 'B';
System.out.println("First letter: " + firstLetter);
System.out.println("Second letter: " + secondLetter);
}
}
Output:
First letter: A
Second letter: B
2. Instance Scope:
o Variables declared without static inside a class.
o Each object of the class has its own copy.
o Scope: Accessible within the class using an object.
3. Local Scope:
o Variables declared inside a method, constructor, or block.
o Scope: Limited to the block in which they are declared.
o Not accessible outside the block.
4. Block Scope:
o Variables declared inside loops, if statements, or other blocks.
o Scope: Limited to the block where they are defined.
Lifetime of Variables
The lifetime of a variable refers to how long the variable exists in memory:
1. Static Variables:
o Lifetime: Exists for the entire duration of the program.
o Memory is allocated when the class is loaded and deallocated when
the program ends.
2. Instance Variables:
o Lifetime: Exists as long as the object exists.
o Memory is allocated when the object is created and deallocated
when the object is garbage collected.
3. Local Variables:
o Lifetime: Exists only during the execution of the method or block.
o Memory is allocated when the method/block is invoked and
deallocated when it ends.
4. Block Variables:
o Lifetime: Exists only during the execution of the block.
Operators
What is an Operator?
An operator is a symbol that performs operations on variables and values. Java
provides many types of operators to perform different kinds of operations.
Types of Operators in Java
1. Arithmetic Operators
2. Relational (Comparison) Operators
3. Logical Operators
4. Assignment Operators
5. Unary Operators
6. Bitwise Operators
7. Ternary Operator
8. Shift Operators
Operators Operations
+ Addition
- Subtraction
x Multiplication
/ Division
% Modulus
class ArithExample
{
public static void main(String[] args)
{
// declare variables
int a = 15, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Run Code >>
Output
a + b = 20
a - b = 10
a * b = 75
a/b=3
a%b=0
Assignment Operators are mainly used to assign the values to the variable that is
situated in Java programming. There are various assignment operators in Java,
such as
class Main
{
public static void main(String[] args)
{
// create variables
int a = 5;
int var;
}
}
Run Code >>
Output
var using =: 5
var using +=: 10
var using *=: 5
Java relational operators are assigned to check the relationships between two
particular operators. There are various relational operators in Java, such as
class Main
{
public static void main(String[] args)
{
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
Run Code >>
Output
a is 7 and b is 11
false
true
false
true
false
true
class Main
{
public static void main(String[] args)
{
// && operator
System.out.println((6 > 3) && (8 > 6)); // true
System.out.println((6 > 3) && (8 < 6)); // false
// || operator
System.out.println((6 < 3) || (8 > 6)); // true
System.out.println((6 > 3) || (8 < 6)); // true
System.out.println((6 < 3) || (8 < 6)); // false
// ! operator
System.out.println(!(6 == 3)); // true
System.out.println(!(6 > 3)); // false
}
}
Run Code >>
Output
true
false
true
true
false
true
false
Unary Operators in Java are used in only one operand. There are various types of
Unary Operators in Java, such as
Operators Description
+ Unary Plus
- Unary Minus
++ Increment operator
-- Decrement Operator
We'll see the demonstration of the increment and decrement operator in Java in
our Java Playground.
class Main
{
public static void main(String[] args)
{
// declare variables
int a = 13, b = 13;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
// decrement operator
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
result2 = --b;
}
}
Run Code >>
Output
Value of a: 13
After increment: 14
Value of b: 13
After decrement: 12
Bitwise Operators in Java are used to assist the performance of the operations on
individual bits. There are various types of Bitwise Operators in Java, such as. We
will see the working of the Bitwise Operators in the Java Online Compiler.
Operators Descriptions
~ Bitwise Complement
^ Bitwise exclusive OR
class Main
{
public static void main(String[] args)
{
Output
To compare two values (or variables), comparison operators are used. This is
crucial to programming since it facilitates decision-making and the search for
solutions. A comparison's return value is either true or false. These are referred to
as "Boolean values."
Operators Operations
== Equal to
!= Not equal
Operators Operations
Example
Output
The only conditional operator that accepts three operands is the ternary operator
in Java. Java programmers frequently use it as a one-line alternative to the if-
then-else expression. The ternary operator can be used in place of if-else
statements, and it can even be used to create switch statements with nested ternary
operators. The conditional operator uses less space and aids in writing if-else
statements as quickly as possible even if it adheres to the same algorithm as an
if-else statement
Example
Output
Example :
// Java Program to Illustrate Automatic Type Conversion
class TypeExample{
public static void main(String[] args)
{
int i = 100; // Automatic type conversion
long l = i; // Integer to long type
float f = l; // long to float type
System.out.println("Int value " + i);
System.out.println("Long value " + l);
System.out.println("Float value " + f);
}
}
Output
Example :
// Java program to Illustrate Explicit Type Conversion
public class NarrowingExample{
public static void main(String[] args)
{
// Double datatype
double d = 100.04;
// Print statements
System.out.println("Double value " + d);
System.out.println("Long value " + l);
System.out.println("Int value " + i);
}
}
Output :
Double value 100.04
Long value 100
Int value 100
Type conversion :
In type conversion, a data type is automatically converted into another
data type by a compiler at the compiler time. In type conversion, the destination
data type cannot be smaller than the source data type, that's why it is also called
widening conversion. One more important thing is that it can only be applied to
compatible data types.
%d Integer (decimal)
%f Floating-point number
%s String
%c Character
%n Newline (platform-independent)
Examples
1. Formatting Strings
System.out.printf("Hello, %s!%n", "World");
// Output: Hello, World!
2. Formatting Numbers
System.out.printf("Integer: %d, Float: %.2f%n", 42, 3.14159);
// Output: Integer: 42, Float: 3.14
3. Aligning Text
System.out.printf("Left: %-10s Right: %10s%n", "Java", "Code");
// Output: Left: Java Right: Code
4. Padding Numbers
System.out.printf("Padded: %05d%n", 42);
// Output: Padded: 00042
5. Displaying Percentages
System.out.printf("Success Rate: %.2f%%%n", 99.99);
// Output: Success Rate: 99.99%
Control Statements
Control statements in Java are the instructions that controls or manages
the flow of execution of a program based on specific conditions or loops.
Control Statements are used to:
o Make decisions: Control program flow based on conditions
(e.g., if, switch).
o Loop through blocks of code: Repeat code execution multiple
times (e.g., for, while).
o Jump to a different part of the code: Change the natural flow of
execution (e.g., break, continue).
Types of Control Statements in Java
1. Decision-making statements
2. Looping (Iteration) statements
3. Branching (Jump) statements
Decision-making statements
Decision making statements allow you to make a decision, based upon
the result of a condition.
Types of Decision-Making Statements
• if
• if - else
• if-else-if ladder
1. 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
}
Example :
class IfExample {
public static void main(String[] args) {
int x = 10;
int y = 12;
int sum = x+y;
if(sum > 20) {
System.out.println("sum is greater than 20");
}
}
}
Output
sum is greater than 20
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Example
Output :
x + y is greater than 20
if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple
else-if statements. In other words, we can say that it is the chain of if-else
statements that create a decision tree where the program may enter in the block
Prepared by S. Dinesh, M.C.A Lect.in Computer Science
Unit - I
of code where the condition is true. We can also define an else statement at the
end of the chain.
Syntax :
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
Example
class Example {
public static void main(String args[])
{
int i = 20;
if (i == 10)
nested-if Statement
In nested if-statements, the if statement can contain a if or if-
else statement inside another if or else-if statement.
Syntax :
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Example
class ConditionExample {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i < 15) {
if (i < 15)
if (i < 12)
System.out.println( "i is smaller than 12 too");
}
else {
Output
i is greater than 15
i is smaller than 12 too
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:
witch (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
}
Example :
class SwitchExample {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
Output :
2
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly
while some condition evaluates to true. However, loop statements are used to
execute the set of instructions in a repeated order. The execution of the set of
instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly.
1. for loop
2. while loop
3. do-while loop
Let's understand the loop statements one by one.
For Loop
In Java, for loop is similar to C and C++. It enables us to initialize the
loop variable, check the condition, and increment/decrement in a single line of
code. We use the for loop only when we exactly know the number of times, we
want to execute the block of code.
Syntax :
for(initialization, condition, increment/decrement) {
//block of statements
}
Example
Example :
Public WhileExample {
public static void main(String[] args) {
int i = 1;
System.out.println("Printing the list of first 10 even numbers");
while(i<=5) {
System.out.println(“Well come to the world”);
i ++;
}
}
}
Output
Well come to the world
Well come to the world
Well come to the world
Well come to the world
Well come to the world
do-while loop
The do-while loop checks the condition at the end of the loop after
executing the loop statements. When the number of iteration is not known and
we have to execute the loop at least once, we can use do-while loop.
Syntax :
do
{
//statements
} while (condition);
Example :
Class DoExample {
public static void main(String[] args) {
int i = 0;
System.out.println("Printing the list of first 10 even numbers ");
do {
System.out.println(i);
i = i + 2;
}while(i<=10);
}
}
Output :
Printing the list of first 10 even numbers
0
2
4
6
8
10
Jump Statements
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to the
other part of the program. There are two types of jump statements in Java, i.e.,
break and continue.
Java break statement
As the name suggests, the break statement is used to break the current
flow of the program and transfer the control to the next statement outside a loop
or switch statement. However, it breaks only the inner loop in the case of the
nested loop.
The break statement cannot be used independently in the Java program, i.e., it
can only be written inside the loop or switch statement.
Example :
Output :
0
1
2
3
4
Java continue statement
Unlike break statement, the continue statement doesn't break the loop, whereas,
it skips the specific part of the loop and jumps to the next iteration of the loop
immediately.
Example :
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i equals 5
}
System.out.print(i + " ");
}
}
}
Output :
0
1
2
3
4
5