Found 2616 Articles for Java

How can we convert a hexadecimal value to a byte in Java?

raja
Updated on 22-Nov-2023 12:19:46

131 Views

A Byte class is a subclass of Number class and it can wrap a value of primitive type byte in an object. An object of type Byte contains a single field whose type is a byte. The important methods of Byte class are byteValue(), compare(), compareTo(), decode(), parseByte(), valueOf() and etc. We can convert a hexadecimal value to a byte using the method decode().byteValue() of Byte class. Syntax public final class Byte extends Number implements Comparable Example public class ConvertHexaDecimalToByte { public static void main(String args[]) { byte b = Byte.decode("0x0a").byteValue(); // convert hexadecimal value to byte. ... Read More

Can Enum implements an interface in Java?

raja
Updated on 22-Nov-2023 12:35:44

10K+ Views

Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. An Enum is a special datatype which is added in Java 1.5 version. The Enums are constants, by default they are static and final so the names of an enum type fields are in uppercase letters. Example interface EnumInterface { int calculate(int first, int second); } enum EnumClassOperator implements EnumInterface { // An Enum implements an interface ADD { @Override ... Read More

How can we implement a moving text using a JLabel in Java?

raja
Updated on 10-Feb-2020 13:34:34

1K+ Views

A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. We can also implement a moving text in a JLabel by using a Timer class, it can set a timer with speed(in milliseconds) and this as an argument.Exampleimport java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; public class MovingTextLabel extends JFrame implements ActionListener {    private JLabel label;    public MovingTextLabel() {       setTitle("MovingTextLabel");       label= new JLabel(" ... Read More

What can cause the "cannot find symbol" error in Java?

raja
Updated on 10-Feb-2020 13:35:22

12K+ Views

The “cannot find symbol” error occurs mainly when we try to reference a variable that is not declared in the program which we are compiling, it means that the compiler doesn’t know the variable we are referring to.Some possible causes for “Cannot find symbol” to occur areUsing a variable that is not declared or outside the code.Using wrong cases (“tutorials” and “Tutorials" are different) or making spelling mistakes.The packaged class has not been referenced correctly using an import declaration.Using improper identifier values like letters, numbers, underscore and dollar sign. The hello-class is different from helloclass.Examplepublic class CannotFindSymbolTest {    public ... Read More

When can we call wait() and wait(long) methods of a Thread in Java?

raja
Updated on 22-Nov-2023 12:46:34

490 Views

Whenever the wait() method is called on an object, it causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object whereas wait(long timeout) causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time has elapsed. wait() In the below program, When wait() is called on an object, the thread enters from running to waiting state. It waits for some other thread to call notify() or notifyAll() so that it can enter runnable state, a deadlock will be formed. Example class MyRunnable implements ... Read More

What is the purpose of Process class in Java?

raja
Updated on 10-Feb-2020 12:49:33

1K+ Views

The java.lang.Process is a subclass of Object class and it can describe the processes that are started by the exec() method of Runtime class. A Process object controls the process and gets information about it. The Process class is an abstract class, therefore, it cannot be instantiated. The important method s of the Process class are destroy(), exitValue(), getErrorStream(), waitFor(), getInputStream() and getOutputStream().Syntaxpublic abstract class Process extends ObjectExampleimport java.util.concurrent.*; public class ProcessTest {    public static void main(String[] args) throws Exception {       Runtime runtime = Runtime.getRuntime();       System.out.println("Launching of Notepad Application");       Process process = runtime.exec("Notepad.exe"); // Launch ... Read More

What is the use of StrictMath class in Java?

raja
Updated on 22-Nov-2023 09:39:02

82 Views

The java.lang.StrictMath is a final class and it is a subclass of Object class. The StrictMath class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. We need not create an instance for StrictMath class because all the methods in StrictMath class are static methods. The important methods of StrictMath class are abs(), acos(), asin(), atan(), ceil(), floor(), log(), max(), min(), pow(), random(), round() and etc. Syntax public final class StrictMath extends Object Example public class StrictMathTest { public static void main(String args[]) { System.out.println("Absolute Value: " + StrictMath.abs(-100.50)); ... Read More

What is the importance of Runtime class in Java?

raja
Updated on 22-Nov-2023 09:53:24

992 Views

The java.lang.Runtime class is a subclass of Object class, can provide access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program. The Runtime class does not have any public constructors, so a program cannot create its own instances of the class. A program must call the getRuntime() method to get a reference to the current Runtime object. The important methods of Runtime class are addShutdownHook(), exec(), exit(), freeMemory(), gc(), halt() and load(). Syntax public class Runtime extends Object Example public class RuntimeTest { ... Read More

How to set the shortcut key to a JCheckBox in Java?

raja
Updated on 10-Feb-2020 12:26:50

187 Views

A JCheckBox is a subclass of JToggleButton and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically. A JCheckBox can generate an ActionListener or ItemListener whenever the checkbox is changed. We can set the shortcut keys to a JCheckBox by using the setMnemonic() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JCheckBoxShortCutKeyTest extends JFrame {    private JCheckBox checkBox;    public JCheckBoxShortCutKeyTest() {       setTitle("JCheckBoxShortCutKey Test");       checkBox = new JCheckBox("Check or Press ALT-C");       checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray));       checkBox.setMnemonic('C');     ... Read More

How can we set the background/foreground color for individual column of a JTable in Java?

raja
Updated on 10-Feb-2020 12:52:14

3K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces. We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.table.*; public class JTableColumnColorTest extends JFrame {    private JTable table;    private TableColumn tColumn;    public JTableColumnColorTest() {       setTitle("JTableColumnColor Test");       table = ... Read More

Advertisements