Skip to content
Home » Java Programs » Java Program to Search an Element in an Array

Java Program to Search an Element in an Array

Simple java program to search an element in an array. A data structure called an array comprises a collection of elements, values, or variables, each of which is identifiable by an array index or key. Array types, depending on the language, may overlap or be mistaken with other data types that express collections of items, such as lists and strings.

Boolean Definition in Java

The boolean keyword is a primitive data type in Java. It can only store one of two potential values: true or false. With variables and methods, the boolean keyword is utilised. False is the default setting. It’s usually used in conjunction with conditional statements. 

The boolean keyword in Java can be used to construct a boolean variable. In Java, unlike C++, you can’t add a numerical value to a boolean variable; you can only use true or false. When a boolean variable is printed, the strings “true” or “false” are displayed on the console.

Decision Making Statement in Java

Decision Making in Java facilitates the creation of decision-driven statements and the execution of a specific set of code in response to specified situations. If a condition is true, the if statement will execute a block of statements; if the condition is false, the if statement will not. 

The if statement is a decision-making structure that consists of one or more statements following an expression. The if else structure is a decision-making structure in which the if statement is followed by an optional else statement that runs when the expression is false.

Search Element in Array Java For Beginner

In Java, an array is a collection of variables that are referred to by a single variable name and an index number. An array’s items are called elements. An array’s items must all be of the same type. As a result, the array has a type that determines the types of elements it can hold. 

Array Definition in Java

In Java, an array is a collection of variables that are referred to by a single variable name and an index number. An array’s items are called elements. An array’s elements must all be of the same type. For example, an int array can hold int values, while a String array can hold strings. Here we have executed the java program to search an element in an array using array method 

Java Program to Search an Element in an Array

//java program to search an element in an array 
class Main 
{
public static void main(String[] args)
{

int[] num = {10, 20, 30, 40, 50, 13};
int toFind = 13;
boolean found = false;
for (int n : num)
{
if (n == toFind)
{
found = true;
break;
}
}
if(found)
System.out.println(toFind + " is found");
else
System.out.println(toFind + " is not found");
}
}

Output:

13 is found