BhauAutomation

Java Topics

In this tutorial, you will explore two important Java concepts: Array in String and Multithreading. These play a vital role in efficient data handling and concurrent programming.

📘 Topic: Core Java / Advanced
Read time: 8 min
📊 Level: Intermediate
📚 Focus: String Array & Concurrency
📊 Array in String

Array in String in Java

In Java, a String can be converted into an Array and vice-versa. This conversion is useful when you want to process, analyze, or modify string data effectively.

How to Convert String to Array?

You can convert a String into an array using two methods:
1. The toCharArray() method converts a String into a character array.
2. The split() method splits a String into an array of substrings based on a given delimiter.

Example

String str = "Bhau Automation"; char[] charArray = str.toCharArray(); String[] words = str.split(" "); System.out.println("Character Array:"); for (char c : charArray) { System.out.print(c + " "); } System.out.println("\nWord Array:"); for (String w : words) { System.out.println(w); }

Advantages

Converting strings into arrays makes it easy to perform character-by-character manipulation, enhances control over string data, and is very helpful during data parsing or tokenization tasks.

Limitations

Conversion may cause additional memory usage for large strings, and continuous back-and-forth conversion can slightly reduce performance in computation-heavy applications.

Additional Example

String sentence = "Java is awesome"; String[] wordsList = sentence.split(" "); System.out.println("First word: " + wordsList[0]); System.out.println("Total words: " + wordsList.length);
▼ MULTITHREADING SECTION ▼
🧵 Multithreading

Multithreading in Java

Multithreading allows Java programs to perform multiple operations simultaneously by dividing a program into small threads. It enhances efficiency and improves CPU utilization.

Multithreading Process

To implement multithreading:
1. Create a thread by extending the Thread class or implementing the Runnable interface.
2. Override the run() method to define thread logic.
3. Start the thread using the start() method.

Example - Extending Thread Class

class MyThread extends Thread { public void run() { System.out.println("Thread is running..."); } } public class Demo { public static void main(String[] args) { MyThread t1 = new MyThread(); t1.start(); } }

Advantages

Multithreading ensures efficient CPU usage by running tasks in parallel. It allows better performance in applications like web servers, games, and real-time systems.

Limitations

Multithreaded programs can be complex to debug and may lead to issues like deadlocks or race conditions if synchronization is not properly handled.

Additional Example - Implementing Runnable

class MyRunnable implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " - Count: " + i); } } } public class ThreadExample { public static void main(String[] args) { Thread t1 = new Thread(new MyRunnable(), "Thread-1"); Thread t2 = new Thread(new MyRunnable(), "Thread-2"); t1.start(); t2.start(); } }