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

Passing and Returning Objects in Java Methods

Passing and Returning Objects in Java Methods In Java, objects can be passed as parameters to methods and returned from methods just like other primitive data types. This allows for flexibility and the manipulation of object state within methods. Let's explore how passing and returning objects work in Java. Passing Objects as Parameters When you pass an object as a parameter to a method, you are essentially passing a reference to that object. This means that changes made to the object inside the method will affect the original object outside the method.  Example: class Car {     String model;     Car(String model) {         this.model = model;     } } public class CarProcessor {     // Method to modify the Car object     static void modifyCar(Car car, String newModel) {         car.model = newModel;     }     public static void main(String[] args) {       ...

Why is String[] args necessary in main() method in Java?

  Why is String[] args necessary in main() method in Java? In Java, the main method serves as the entry point for the program. The correct syntax for the main method is: public static void main (String[] args) { System.out.println( "Hello, Java!" ); } 🔹 Breaking it down: public → Accessible from anywhere. static → No need to create an object of the class to run it. void → No return value. main → Special method recognized by the JVM as the starting point. String[] args → Stores command-line arguments (optional but required by JVM). Why Can't We Skip String[] args ? JVM looks for main(String[] args) When you run a Java program, the JVM searches for the main method with exactly this signature : public static void main (String[] args) If you change or remove String[] args , the JVM cannot find the correct main() method and throws a runtime error (NoSuchMethodError). Java Specification Requires It The Java Language Specification (JLS) defines that main...