Data Types and Operators
Non-Primitive Data Types
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Strings in Java

Strings in Java are objects that represent sequences of characters. Java provides the String class to create and manipulate strings.

Key Features of Strings:

  • Immutability: Once a string is created, it cannot be changed. Any modification creates a new string.
  • String Pool: Java optimizes the creation of strings by storing them in a pool, making reuse possible without creating a new object each time.

Creating Strings:

1. Using String Literals:

				
					String str1 = "Hello, World!";

				
			

2. Using the new Keyword:

				
					String str2 = new String("Hello, World!");

				
			

Common String Operations:

1. Concatenation:

				
					String str3 = str1 + " How are you?";
String str4 = str1.concat(" How are you?");

				
			

2. Finding Length:

				
					int length = str1.length(); // Returns 13

				
			

3. Character at a Specific Index:

				
					char ch = str1.charAt(0); // Returns 'H'

				
			

4. Substring:

				
					String subStr = str1.substring(7, 12); // Returns "World"

				
			

5. String Comparison:

				
					boolean isEqual = str1.equals(str2); // Returns true if strings are equal

				
			

6. Converting to Upper or Lower Case:

				
					String upper = str1.toUpperCase(); // Returns "HELLO, WORLD!"
String lower = str1.toLowerCase(); // Returns "hello, world!"

				
			

7. Splitting a String:

				
					String[] words = str1.split(", "); // Returns ["Hello", "World!"]

				
			

8. Replacing Characters or Substrings:

				
					String replacedStr = str1.replace("World", "Java"); // Returns "Hello, Java!"

				
			

Arrays in Java

Arrays in Java are data structures that store multiple elements of the same type. They are fixed in size and can store either primitive types or objects.

Key Features of Arrays:

  • Fixed Size: Once created, the size of the array cannot be changed.
  • Indexing: Array elements are indexed, starting from 0.
  • Homogeneous: All elements in an array must be of the same type.

Creating Arrays:

1. Single-Dimensional Array:

				
					int[] numbers = new int[5]; // Creates an array of size 5
numbers[0] = 10;
numbers[1] = 20;
// Or initialize with values
int[] numbers = {10, 20, 30, 40, 50};

				
			

2. Multi-Dimensional Array:

				
					int[][] matrix = new int[3][3]; // Creates a 3x3 matrix
matrix[0][0] = 1;
matrix[1][1] = 2;
// Or initialize with values
int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

				
			

Common Array Operations:

1. Accessing Elements:

				
					int firstElement = numbers[0]; // Returns 10
int secondElement = matrix[1][1]; // Returns 5

				
			

2. Finding Array Length:

				
					int length = numbers.length; // Returns 5

				
			

3. Iterating Over an Array:

				
					for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

// Enhanced for loop
for (int num : numbers) {
    System.out.println(num);
}

				
			

4. Copying an Array:

				
					int[] copiedArray = Arrays.copyOf(numbers, numbers.length);

				
			

5. Sorting an Array:

				
					Arrays.sort(numbers); // Sorts the array in ascending order

				
			

6. Searching an Array:

				
					int index = Arrays.binarySearch(numbers, 30); // Returns the index of the element if found

				
			

Example Code Using Strings and Arrays:

				
					public class Example {
    public static void main(String[] args) {
        // String Example
        String greeting = "Hello, Java!";
        System.out.println("Original String: " + greeting);
        System.out.println("Uppercase: " + greeting.toUpperCase());
        System.out.println("Substring: " + greeting.substring(7));

        // Array Example
        int[] numbers = {10, 20, 30, 40, 50};
        System.out.println("Original Array:");
        for (int number : numbers) {
            System.out.print(number + " ");
        }

        // Sorting the array
        Arrays.sort(numbers);
        System.out.println("\nSorted Array:");
        for (int number : numbers) {
            System.out.print(number + " ");
        }
    }
}

				
			

This program demonstrates the use of strings and arrays, performing basic operations like concatenation, substring extraction, and array sorting.

Scroll to Top