Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Array and Strings in Java

The document provides an overview of arrays and strings in Java, highlighting key characteristics such as fixed size and immutability. It includes examples of array declaration, initialization, and operations, as well as string manipulation methods and comparisons. The conclusion emphasizes the importance of strings in Java programming for various text-related tasks.

Uploaded by

kvkumaravel28
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array and Strings in Java

The document provides an overview of arrays and strings in Java, highlighting key characteristics such as fixed size and immutability. It includes examples of array declaration, initialization, and operations, as well as string manipulation methods and comparisons. The conclusion emphasizes the importance of strings in Java programming for various text-related tasks.

Uploaded by

kvkumaravel28
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Array in Java

• An array is a data structure that stores multiple values of the same


type. In Java, arrays are objects that hold a fixed number of values of
a single type. Arrays are used when you have a large number of
similar items.
• Key Points About Arrays in Java:
1.Fixed Size: Once an array is created, its size cannot be changed.
2.Same Data Type: All elements in an array must be of the same type
(e.g., integers, strings).
3.Indexing: Array elements are accessed via indexes, starting from 0.
• Array Declaration and Initialization in Java
• int[] numbers; // Declaring an array of integers

• Creating an Array:
• numbers = new int[5]; // Creating an array of size 5

• Initialization of an Array:
• int[] numbers = {1, 2, 3, 4, 5}; // Initializing an array directly
• Accessing Array Elements:
• int firstNumber = numbers[0]; // Access the first element (index 0)
System.out.println(firstNumber); // Output: 1
Example Programs
• Simple Array Declaration and Access

public class ArrayExample {


public static void main(String[] args)
{
int[] numbers = {10, 20, 30, 40, 50}; // Declare and initialize array System.out.println("First
element: " + numbers[0]); // Access element at index 0
System.out.println("Last element: " + numbers[4]); // Access element at index 4
}

Output
First element: 10
Last element: 50
Iterating Over an Array Using a Loop
public class ArrayIteration {
public static void main(String[] args) {
int[] numbers = {5, 10, 15, 20, 25}; // Declare and initialize array // Using a for loop to
iterate over the array
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}

Output
Element at index 0: 5
Element at index 1: 10
Element at index 2: 15
Element at index 3: 20
String in Java
• String is a sequence of characters.
• Strings are immutable, meaning once a string is created, it cannot be
changed.
• Strings are objects, but can be represented using string literals.
• Example
• String name = "Java";
Declaring and Initializing Strings

o A string can be declared and initialized like this


• String str = "Hello, World!";

• Alternatively, you can use the new keyword to create a string


String str = new String("Hello, World!");
String Operations

• Length of the string


• int length = str.length(); // Returns length of string

• Concatenation (Combining Strings):


• String greeting = "Hello, " + "World!";

• Accessing Characters (by index)


• char ch = str.charAt(0); // Access character at index 0
Example Code for these operations:
String str = "Hello, Java!";
System.out.println("Length: " + str.length());
System.out.println("First character: " + str.charAt(0));
System.out.println("Concatenation: " + str + " is awesome!");
replace(old, new): Replaces characters in a string.
java

String Methods
• toLowerCase(): Converts string to lowercase
String lowerStr = str.toLowerCase();
toUpperCase(): Converts string to uppercase
String upperStr = str.toUpperCase();
substring(start, end): Extracts a part of the string.
String subStr = str.substring(0, 5); // "Hello"
replace(old, new): Replaces characters in a string
String replacedStr = str.replace("Java", "World");
String Comparison
• equals(): Checks if two strings are equal.

boolean result = str.equals("Java");


equalsIgnoreCase(): Checks if two strings are equal, ignoring case.

boolean result = str.equalsIgnoreCase("java");


compareTo(): Compares two strings lexicographically
int result = str.compareTo("Java");
String Immutability
• In Java, Strings are immutable. Once a string is created, it cannot be changed.

• Example
String str = "Hello";
str = str + " World"; // Creates a new string object, old string remains unchanged.

• This is important for performance and security reasons.


Conclusion

• Strings are fundamental in Java programming.


• They are immutable and come with a wide range of useful methods.
• Common operations include concatenation, comparison, and substring
extraction.
• Strings are widely used for text manipulation, input/output, and
communication.

You might also like