Browsed by
Tag: java 8 features

Why main() in java is declared as public static void main? What if the main method is declared as private?

Why main() in java is declared as public static void main? What if the main method is declared as private?

Public – main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static – When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void – Java is platform independent language therefore if it will…

Read More Read More

How can a GUI component handle its own events

How can a GUI component handle its own events

A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener. In Java, GUI components can handle their own events by implementing event listener interfaces. The most common way to handle events for GUI components is by implementing listener interfaces such as ActionListener, MouseListener, KeyListener, etc. Here is a brief explanation: Implementing Event Listener Interface: To handle events on a GUI component, you need to make your class implement the…

Read More Read More

What is difference between String, StringBuffer and StringBuilder? When to use them?

What is difference between String, StringBuffer and StringBuilder? When to use them?

The main difference between the three most commonly used String classes as follows. StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable. StringBuffer class implementation is synchronized while StringBuilder class is not synchronized. Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder. Criteria to choose among String, StringBuffer and StringBuilder If the Object value will not change in a scenario use String Class because a String object is immutable. If the Object value…

Read More Read More

How many ways can an argument be passed to a subroutine and explain them?

How many ways can an argument be passed to a subroutine and explain them?

An argument can be passed in two ways. They are passing by value and passing by reference. Passing by value: This method copies the value of an argument into the formal parameter of the subroutine. Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.

What is the Collection interface

What is the Collection interface

The Collection interface provides support for the implementation of a mathematical bag – an unordered collection of objects that may contain duplicates. In Java, the Collection interface is a part of the Java Collections Framework and is present in the java.util package. It is at the top of the hierarchy of collection interfaces. The Collection interface represents a group of objects, known as elements. It provides a standard set of methods to manipulate collections, such as adding, removing, and querying elements. Some…

Read More Read More

What is the difference between exception and error?-

What is the difference between exception and error?-

?– The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing. The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.

Under what conditions is an object’s finalize() method invoked by the garbage collector

Under what conditions is an object’s finalize() method invoked by the garbage collector

The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable. In Java, the finalize() method is a method of the Object class, and it is called by the garbage collector before an object is reclaimed (garbage collected). However, there are no guarantees about when the finalize() method will be called or whether it will be called at all. The Java documentation explicitly states that it is typically not recommended to rely on finalize() for resource…

Read More Read More

What is the class and interface in java to create thread and which is the most advantageous method?

What is the class and interface in java to create thread and which is the most advantageous method?

Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here. In Core Java, the primary classes and interfaces used to create threads are: Thread class: The Thread class itself is used to create and manage threads. You can extend the Thread class and override its run() method to define the code that will be executed by the thread. Example:…

Read More Read More

What an I/O Filter

What an I/O Filter

An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another. In the context of Core Java, an I/O filter typically refers to an object that is used to perform filtering on data as it is read from or written to a stream. Input/Output (I/O) streams in Java are used for reading from or writing to various sources, such as…

Read More Read More

Are there any global variables in Java, which can be accessed by other part of your program?-

Are there any global variables in Java, which can be accessed by other part of your program?-

No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here. In Java, the concept of global variables, as traditionally understood in some other programming languages like C, doesn’t directly apply. In Java, variables are typically declared within a class, method, or block, and their scope is limited to that particular context. However, if you want to create variables that can be accessed globally across different parts…

Read More Read More