Found 2616 Articles for Java

When can a double-type be preferred over float-type in Java?

raja
Updated on 01-Dec-2023 10:53:42

200 Views

Both double-type and float-type can be used to represent floating-point numbers in Java. A double-type is preferred over float-type if the more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points while the precision of float type is only around 6 to 7 decimal digits. The double-type can be used for all the calculations and temp variables while a float-type can be used to maintain an array of numbers. A double-type uses 1 bit for a sign and 11 bits for exponent while float-type only uses 1 bit for a sign and ... Read More

How to check if the string ends with specific substring in Java?

raja
Updated on 01-Dec-2023 11:01:53

966 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. We can use the endsWith() method of String class to check whether a string ends with a specific string or not, it returns a boolean value true or false. Syntax public boolean endsWith(String prefix) Example public class StringEndsWithSubStringTest { public static void main(String[] args) { String str = "WelcomeToTutorialsPoint"; if(str.endsWith("Point")) { System.out.println("Ends with the specified word!"); ... Read More

Can we override a protected method in Java?

raja
Updated on 01-Dec-2023 11:07:06

6K+ Views

Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier. Example class A { protected void protectedMethod() { System.out.println("superclass protected method"); } } class B extends A { protected void protectedMethod() { System.out.println("subclass protected method"); } } public class Test { public static void main(String args[]) { B b = new B(); b.protectedMethod(); } } Output subclass protected method

How can we add multiple sub-panels to the main panel in Java?

raja
Updated on 03-Jul-2020 12:16:44

3K+ Views

A JPanel is a subclass of JComponent class and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, tables, lists, trees,  etc. to a JPanel.We can also add multiple sub-panels to the main panel using the add() method of Container class.Syntaxpublic Component add(Component comp)Exampleimport java.awt.*; import javax.swing.*; public class MultiPanelTest extends JFrame {    private JPanel mainPanel, subPanel1, subPanel2;    public MultiPanelTest() {       setTitle("MultiPanel Test");       mainPanel = new JPanel(); // main panel       mainPanel.setLayout(new GridLayout(3, 1));       mainPanel.add(new JLabel("Main ... Read More

How to check if the string begins with specific substring in Java?

raja
Updated on 01-Dec-2023 14:34:36

1K+ Views

A String class can be used to represent the character strings, all the string literals in a Java program are implemented as an instance of a String class. The Strings are constants and their values cannot be changed (immutable) once created. We can use the startsWith() method of String class to check whether a string begins with a specific string or not, it returns a boolean, true or false. Syntax public boolean startsWith(String prefix) Example public class StringStartsWithSubStringTest { public static void main(String[] args) { String str = "WelcomeToTutorialsPoint"; if(str.startsWith("Welcome")) { ... Read More

How can we set the border to JComboBox items in Java?

raja
Updated on 03-Jul-2020 11:58:06

505 Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can set the border to the items of a JComboBox by rendering a JComboBox which extends the DefaultListCellRenderer class and need to override the getListCellRendererComponent() method.Syntaxpublic Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame {    public JComboBoxTest() {       setTitle("JComboBox Test");       ... Read More

What is the purpose of using a dumpStack() method in Java?

raja
Updated on 01-Dec-2023 14:39:39

520 Views

The dumpStack() method is a static method of Thread class and it can be used to print or display stack tracing of the current thread to System.err. The purpose of the dumpStack() method is basically for debugging and Internally this method is calling the printStackTrace() method of Throwable class. This method does not raise any exceptions. Syntax public static void dumpStack() Example public class ThreadDumpStackTest { public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("ThreadDumpStackTest"); t.setPriority(1); System.out.println("Current Thread: " + ... Read More

How can we rotate a JLabel text in Java?

raja
Updated on 03-Jul-2020 11:56:43

2K+ 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. By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().Syntaxpublic abstract void rotate(double theta, double x, double y)Exampleimport java.awt.*; import java.awt.geom.*; import javax.swing.*; public class RotateJLabelTest extends JFrame {    public RotateJLabelTest() {       setTitle("Rotate JLabel");       JLabel label ... Read More

What is the default value of a local variable in Java?

raja
Updated on 03-Jul-2020 11:53:32

5K+ Views

The local variables can be declared in methods, code blocks, constructors, etc in Java. When the program control enters the methods, code blocks, constructors, etc. then the local variables are created and when the program control leaves the methods, code blocks, constructors, etc. then the local variables are destroyed. The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time, otherwise, the compiler throws an error.Examplepublic class LocalVariableTest {    public void print() {       int num;     ... Read More

How many ways to iterate a LinkedList in Java?

raja
Updated on 03-Jul-2020 11:54:51

263 Views

A LinkedList is a data structure that contains a group of nodes connected in a sequential manner with a pointer. A LinkedList can behave as a dynamic array and it allocates space for each element separately in its own block of memory called a Node. Each node contains two fields, a "data" field to store an element type the list holds and a "next" field which is a pointer used to link one node to the next node.We can iterate the elements of a LinkedList in three ways in Java.Using IteratorWe can iterate the elements of a LinkedList through the Iterator class.Exampleimport java.util.*; ... Read More

Advertisements