Rest of Java 2
Rest of Java 2
java.
inputs?
import java.util.Scanner;
scanner.close();
}
}
Using BufferedReader Class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
try {
// Reading a String
System.out.print("Enter a string: ");
String str = reader.readLine();
System.out.println("You entered: " + str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Using Console Class
The Console class is suitable for reading passwords or other sensitive data as it can
mask input.
import java.io.Console;
if (console != null) {
// Reading a String
String str = console.readLine("Enter a string: ");
System.out.println("You entered: " + str);
} else {
System.out.println("No console available");
}
}
}
4. Define synchronization in java.
ANS-
Synchronization in Java is a mechanism that controls access to shared resources by
multiple threads. It's essential in a multithreaded environment to prevent thread
interference and ensure data consistency.
public class Counter {
private int count = 0;