Found 9150 Articles for Object Oriented Programming

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

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

6K+ 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

356 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

How can we add padding to a JTextField in Java?

raja
Updated on 03-Jul-2020 11:49:59

3K+ Views

A JTextField is a subclass of JTextComponent class and it is one of the most important components that allow the user to input text value in a single-line format. A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(),  etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.Syntaxpublic void setMargin(Insets m)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextfieldPaddingTest extends JFrame {    private JTextField jtf;    public JTextfieldPaddingTest() {       jtf = new JTextField("Welcome to Tutorials Point"); ... Read More

How to serialize and deserialize an object in Java?

raja
Updated on 01-Dec-2023 10:38:28

2K+ Views

The Serialization is a process of changing the state of an object into a byte stream, an object is said to be serializable if its class or parent classes implement either the Serializable or Externalizable interface and the Deserialization is a process of converting the serialized object back into a copy of an object. During serialization, if we don’t want to write the state of a particular variable in a byte stream using a transient keyword. When the JVM comes up to the transient keyword, it ignores the original state of a variable and stores a default value of that data type ... Read More

How can we stop a thread in Java?

raja
Updated on 01-Dec-2023 09:32:48

25K+ Views

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method. The stop() method is deprecated in Java due to thread-safety issues. Syntax @Deprecated public final void stop() Example import static java.lang.Thread.currentThread; public class ThreadStopTest { public static void main(String args[]) throws InterruptedException { UserThread userThread = new UserThread(); ... Read More

How to set the color to alternate rows of JTable in Java?

raja
Updated on 03-Jul-2020 11:48:46

3K+ 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.We can set the color to alternate rows of JTable by overriding the prepareRenderer() method of JTable class.Syntaxpublic Component prepareRenderer(TableCellRenderer renderer, int row, int column)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class AlternateRowColorTableTest extends JFrame {    public AlternateRowColorTableTest() {       setTitle("AlternateRowColorTable Test");       JTable table = new JTable(new Object[][] {{"115", "Ramesh"}, {"120", "Adithya"}, {"125", "Jai"}, {"130", "Chaitanya"}, ... Read More

How can we implement a Custom HashSet in Java?

raja
Updated on 01-Dec-2023 10:02:50

3K+ Views

A HashSet implements Set interface which does not allow duplicate values. A HashSet is not synchronized and is not thread-safe. When we can add any duplicate element to a HashSet, the add() method returns false and does not allow to add a duplicate element to HashSet. Syntax public class HashSet extends AbstractSet implements Set, Cloneable, Serializable In the below example, we can implement a Custom HashSet. Example import java.util.*; public class CustomHashSetTest extends AbstractSet { private HashMap map = null; private static final Object tempObject = new Object(); public CustomHashSetTest() { map = new HashMap(); ... Read More

Importance of a Locale class in Java?

raja
Updated on 01-Dec-2023 10:43:28

264 Views

A Locale class is used to perform locale operations and supply locale information to the user. A Locale is defined as a set of parameters that represents a geographical location or place where some operation occurs. The important methods of Locale class are getAvailableLocales(), getCountry(), getDefault(), getDisplayLanguage(), getDisplayCountry(), getUnicodeLocaleKeys() etc. The Locale class uses the following constructors − Locale(String L)− Initializes locale from the language code passed as an argument. Locale(String L, String C) − Initializes locale from the language, country code passed as arguments. Locale(String L, String C, String V) − Initializes locale from the language, country, variant passed as arguments. Example import java.text.SimpleDateFormat; import java.util.Locale; public ... Read More

How can we get the name of the Enum constant in Java?

Vivek Verma
Updated on 23-Jun-2025 10:59:28

6K+ Views

In Java, an Enum is a class or special datatype that defines a collection of constants. It was introduced in Java version 1.5. When we need a predefined list of values that do not represent numeric or textual data, we can use an Enum class. Enums are constants, which means their fields are implicitly public, static, and final by default. The names of enum constants are written in uppercase letters. Following is the syntax to create an Enum in Java: Enum enumName{ CONST_VALUE1, CONST_VALUE2, CONST_VALUE3....CONST_VALUEN; } Retrieving Name of the Enum Constant The java.lang.Enum class provides various methods ... Read More

Which collection classes are thread-safe in Java?

raja
Updated on 29-Nov-2023 10:31:28

13K+ Views

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc. Stack The Stack class in Java implements the stack data structure that is based on the principle of LIFO. So, the Stack class can support many operations such as push, pop, peek, search, empty, etc. Example import java.util.*; public class StackTest { public static void main (String[] args) { Stack stack = new Stack(); ... Read More

Advertisements