Skip to main content

Java Exception Handling MCQ Test

  Loading…

Implementing Arrays and String Functions in Java

 

1️⃣ Working with Arrays in Java

An array in Java is a collection of elements of the same type, stored in contiguous memory locations.

🔹 Declaring and Initializing an Array:

public class ArrayExample {
public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; // Array declaration and initialization System.out.println("First element: " + numbers[0]); // Accessing array elements // Using a loop to print all elements System.out.println("Array Elements:"); for (int num : numbers) { System.out.println(num); } } }

Output:

First element: 10
Array Elements: 10 20 30 40 50

🔹 Taking Array Input from User and Finding Sum:

import java.util.Scanner;
public class ArrayInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number of elements: "); int n = sc.nextInt(); // Taking input for array size int[] arr = new int[n]; // Declaring array int sum = 0; // Taking input in array System.out.println("Enter " + n + " numbers:"); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); sum += arr[i]; // Calculating sum } System.out.println("Sum of elements: " + sum); sc.close(); } }

Example Output:

Enter number of elements: 3
Enter 3 numbers: 5 10 15 Sum of elements: 30

2️⃣ String Functions in Java

A string in Java is a sequence of characters, handled using the String class.

🔹 Basic String Operations:

public class StringExample {
public static void main(String[] args) { String str = "Hello, Java!"; System.out.println("Original String: " + str); System.out.println("Length: " + str.length()); System.out.println("Character at index 1: " + str.charAt(1)); System.out.println("Substring (0 to 5): " + str.substring(0, 5)); System.out.println("Uppercase: " + str.toUpperCase()); System.out.println("Lowercase: " + str.toLowerCase()); System.out.println("Replaced 'Java' with 'World': " + str.replace("Java", "World")); } }

Output:

Original String: Hello, Java!
Length: 12 Character at index 1: e Substring (0 to 5): Hello Uppercase: HELLO, JAVA! Lowercase: hello, java! Replaced 'Java' with 'World': Hello, World!

🔹 Concatenating and Comparing Strings:

public class StringOperations {
public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; // Concatenation String s3 = s1 + " " + s2; System.out.println("Concatenated String: " + s3); // Comparing strings String str1 = "Java"; String str2 = "java"; System.out.println("Are str1 and str2 equal? " + str1.equals(str2)); System.out.println("Ignoring case, are str1 and str2 equal? " + str1.equalsIgnoreCase(str2)); } }

Output:

Concatenated String: Hello World
Are str1 and str2 equal? false Ignoring case, are str1 and str2 equal? true

🔹 Checking if a String is Palindrome:

import java.util.Scanner;
public class PalindromeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); String reversed = new StringBuilder(str).reverse().toString(); if (str.equalsIgnoreCase(reversed)) { System.out.println("The string is a palindrome."); } else { System.out.println("The string is not a palindrome."); } sc.close(); } }

Example Output:

Enter a string: madam
The string is a palindrome.

Conclusion

Arrays help store multiple values in a single variable.
Strings provide many useful functions like concatenation, substring, replacement, and comparison.
✅ Java provides built-in methods to handle both arrays and strings efficiently.

🚀 Next Steps:

  • Learn multidimensional arrays.
  • Work with StringBuilder for faster string manipulations.
  • Explore sorting and searching techniques on arrays.

Comments

Popular posts from this blog

The Collection Interface.

  The Collection Interface. 

OracleJDK vs OpenJDK

Oracle JDK (Java Development Kit): Oracle JDK is the official reference implementation of the Java Platform, Standard Edition (Java SE). It included the JRE along with development tools. OpenJDK: An open-source alternative to Oracle JDK, OpenJDK is a community-driven project. It provides a free and open-source implementation of the Java Platform, and many other JDKs, including Oracle JDK, are derived from OpenJDK. Below is a simple table highlighting some key points of comparison between Oracle JDK and OpenJDK: Feature Oracle JDK OpenJDK Vendor Oracle Corporation OpenJDK Community Licensing Commercial (Paid) with Oracle Binary Code License Agreement Open Source (GNU General Public License, version 2, with the Classpath Exception) Support Commercial support available with Oracle Support subscription Community support, may have commercial support options from other vendors Updates and Patches Regular updates with security patches provided by Oracle Updates and patches contributed by the ...