Found 2616 Articles for Java

How can we avoid a deadlock in Java?

raja
Updated on 24-Nov-2023 10:33:17

5K+ Views

In Java, a deadlock is a programming situation where two or more threads are blocked forever. A deadlock condition will occur with at least two threads and two or more resources. How To Avoid Deadlock Avoid Nested Locks − A deadlock mainly happens when we give locks to multiple threads. Avoid giving a lock to multiple threads if we already have given to one. Avoid Unnecessary Locks − We can have a lock only those members which are required. Having a lock unnecessarily can lead to a deadlock. Using Thread.join() − A deadlock condition appears when one thread is waiting other to finish. If ... Read More

Importance of yield() method in Java?

raja
Updated on 24-Nov-2023 10:37:40

11K+ Views

A yield() method is a static method of Thread class and it can stop the currently executing thread and will give a chance to other waiting threads of the same priority. If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution. The advantage of yield() method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads. Syntax public static void yield() Example class MyThread extends Thread { public void run() { ... Read More

Can we define multiple methods in a class with the same name in Java?

raja
Updated on 24-Nov-2023 10:40:52

7K+ Views

Yes, we can define multiple methods in a class with the same name but with different types of parameters. Which method is to get invoked will depend upon the parameters passed. In the below example, we have defined three display methods with the same name but with different parameters. Depending on the parameters, the appropriate method will be called. Example public class MethodWthSameNameTest { public void display() { // method with no parameters System.out.println("display() method with no parameter"); } public void display(String name) { // method with a single parameter ... Read More

How to select different cells of a JTable programmatically in Java?

raja
Updated on 12-Feb-2020 06:17:43

791 Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.In general, a user can select the rows and columns manually in a JTable, we can also select different cells of a JTable programmatically using setRowSelectionInterval() and setColumnSelectionInterval() methods of JTable class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTableCellSelectionTest extends JFrame {    private JTable table;    public JTableCellSelectionTest() {       setTitle("JTableCellSelection Test");       Object[][] data = ... Read More

How can we sort a string without using predefined methods in Java?

raja
Updated on 24-Nov-2023 10:44:38

5K+ Views

A String is an object that represents an immutable sequence of characters and cannot be changed once created. The java.lang.String class can be used to create a string object. In the below program, we can sort the characters of a string without using any predefined methods of String class in Java. Example public class SortStringWithoutPredefinedMethodsTest { public static void main(String[] args) { String str = "jdkoepacmbtr"; System.out.println("Before Sorting:" + str); int j = 0; char temp = 0; ... Read More

What is the use of Object Cloning in Java?

raja
Updated on 02-Jul-2020 05:31:09

2K+ Views

The object cloning is a way to create an exact copy of an object. For this purpose, the clone() method of an object class is used to clone an object. The Cloneable interface must be implemented by a class whose object clone to create. If we do not implement Cloneable interface, clone() method generates CloneNotSupportedException.The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed, so we can use object cloning.Syntaxprotected Object clone() throws CloneNotSupportedExceptionExamplepublic class EmployeeTest implements Cloneable ... Read More

When to call the Thread.run() instead of Thread.start() in Java?

raja
Updated on 24-Nov-2023 10:50:04

2K+ Views

When we call the start() method on a thread it causes the thread to begin execution and run() method of a thread is called by the Java Virtual Machine(JVM). If we call directly the run() method, it will be treated as a normal overridden method of a thread class (or runnable interface) and it will be executed within the context of the current thread, not in a new thread. Example public class CallRunMethodTest extends Thread { @Override public void run() { System.out.println("In the run() method: " + Thread.currentThread().getName()); for(int ... Read More

How to detect the value change of a JSlider in Java?

raja
Updated on 12-Feb-2020 05:32:30

786 Views

A JSlider is a subclass of JComponent class and it is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values. A JSlider has a knob which can slide on a range of values and can be used to select a particular value. and it can generate a ChangeListener interface.We can detect the value changed when the slider is moved horizontally using the Graphics2D class and override paint() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ValueChangeJSliderTest extends JFrame {    private JSlider slider;    public ValueChangeJSliderTest() { ... Read More

How to read the data from a CSV file in Java?

raja
Updated on 01-Jul-2020 12:10:16

21K+ Views

A CSV stands for Comma Separated Values. In a CSV file, each line contains words that are separated with a comma(, ) and it is stored with a .csv extension.We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.CSV FileExampleimport java.io.*; public class CSVReaderTest {    public static final String delimiter = ", ";    public static void read(String csvFile) {   ... Read More

How can we print all the capital letters of a given string in Java?

raja
Updated on 24-Nov-2023 11:17:32

5K+ Views

The Character class is a subclass of Object class and it wraps a value of the primitive type char in an object. An object of type Character class contains a single field whose type is char. We can print all the uppercase letters by iterating the characters of a string in a loop and check individual characters are uppercase letters or not using isUpperCase() method and it is a static method of a Character class. Syntax public static boolean isUpperCase(char ch) Example public class PrintUpperCaseLetterStringTest { public static void main(String[] args) { String str = "Welcome To Tutorials ... Read More

Advertisements