Found 9321 Articles for Object Oriented Programming

Calling a method using null in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:27:06

530 Views

When a method is invoked on a null reference, it throws NullPointerException but in case of the static method, we can make it possible using cast expression. See the example below −ExampleLive Demopublic class Tester {    public static void display(){       System.out.println("display");    }    private void print() {       System.out.println("print");    }    public static void main(String[] args) {       //Scenario 1:       //Calling a method on null reference       //causes NullPointerException       try {          Tester test = null;     ... Read More

Callback using Interfaces in Java

Samual Sam
Updated on 18-Jun-2020 12:29:08

3K+ Views

In the case of Event-driven programming, we pass a reference to a function which will get called when an event occurs. This mechanism is termed as a callback. Java does not support function pointers. So we can not implement the same direction. But using interfaces we can achieve the same very easily.In the example below, we've made a callback when a button is clicked. See the steps −Create an interface ClickEventHandler with a single method handleClick().Create a ClickHandler class which implements this interface ClickEventHandler.Create a Button class which will call ClickHandler when it's click method is called.Test the application.ExampleLive Demo//Step ... Read More

Callable and Future in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:31:29

3K+ Views

java.util.concurrent.The callable object can return the computed result done by a thread in contrast to a runnable interface which can only run the thread. The Callable object returns a Future object which provides methods to monitor the progress of a task being executed by a thread. The future object can be used to check the status of a Callable and then retrieve the result from the Callable once the thread is done. It also provides timeout functionality.Syntax//submit the callable using ThreadExecutor //and get the result as a Future object Future result10 = executor.submit(new FactorialService(10));   //get the result using ... Read More

Blank final in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:40:21

604 Views

In Java, a final variable can a be assigned only once. It can be assigned during declaration or at a later stage. A final variable if not assigned any value is treated as a blank final variable. Following are the rules governing initialization of a blank final variable.A blank instance level final variable cannot be left uninitialized.The blank Instance level final variable must be initialized in each constructor.The blank Instance level final variable cannot be initialized in class methods.A blank static final variable cannot be left uninitialized.The static final variable must be initialized in a static block.A static final variable cannot ... Read More

Bitwise right shift operator in Java

Samual Sam
Updated on 29-Jul-2021 15:53:33

14K+ Views

Java supports two types of right shift operators. The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.Signed right shift operatorThe signed right shift operator '>>' uses the sign bit to fill the trailing positions. For example, if the number is positive then 0 will be used to fill the trailing positions and if the number is negative then 1 will be used to fill the trailing positions.Assume if a = 60 and b = -60; ... Read More

Automatic resource management in Java

Samual Sam
Updated on 18-Jun-2020 12:13:37

618 Views

automatic resource management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.ResourceA resource is an object which is required to be closed once our program finishes. For example, a file is read, database connection and so on.UsageTo use the try-with-resources statement, you simply need to declare the required resources within the parenthesis, and the created resource will be closed automatically at the end of the block. Following is the syntax of the try-with-resources statement.Syntaxtry(FileReader fr = new FileReader("file path")) {        // use the resource ... Read More

Association, Composition and Aggregation in Java

Samual Sam
Updated on 18-Jun-2020 10:41:15

7K+ Views

AssociationAssociation refers to the relationship between multiple objects. It refers to how objects are related to each other and how they are using each other's functionality. Composition and aggregation are two types of association.CompositionThe composition is the strong type of association. An association is said to composition if an Object owns another object and another object cannot exist without the owner object. Consider the case of Human having a heart. Here Human object contains the heart and heart cannot exist without Human.AggregationAggregation is a weak association. An association is said to be aggregation if both Objects can exist independently. For ... Read More

Array To Stream in Java

Samual Sam
Updated on 18-Jun-2020 10:53:23

384 Views

With Java 8, Arrays class has a stream() methods to generate a Stream using the passed array as its source.DescriptionThe java.util.Arrays.stream() method returns a sequential Stream with the specified array as its source. −Arrays.stream(array)DeclarationFollowing is the declaration for java.util.Arrays.stream() methodpublic static Stream stream(T[] array)Type ParameterT − This is the type of the array elements.Parameterarray − This is the source array to be used.Return ValueThis method returns a stream for the array.ExampleThe following example shows the usage of java.util.Arrays.stream() method.Live Demoimport java.util.Arrays; public class Tester {    public static void main(String args[]) {       int data[] = { 1, ... Read More

Array Declarations in Java

karthikeya Boyini
Updated on 18-Jun-2020 09:24:40

723 Views

Here is the syntax for declaring an array variable −SyntaxdataType[] arrayRefVar;   // preferred way. or dataType arrayRefVar[];  // works but not preferred way.Note − The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.ExampleThe following code snippets are examples of this syntax −double[] myList;   // preferred way. or double myList[];   // works but not preferred way.Creating ArraysYou can create an array by using the new operator with the following syntax −SyntaxarrayRefVar = new dataType[arraySize];The above statement does two things −It creates an array ... Read More

Array Copy in Java

Samual Sam
Updated on 13-Sep-2023 15:09:03

27K+ Views

Array in Java can be copied to another array using the following ways.Using variable assignment. This method has side effects as changes to the element of an array reflects on both the places. To prevent this side effect following are the better ways to copy the array elements.Create a new array of the same length and copy each element.Use the clone method of the array. Clone methods create a new array of the same size.Use System.arraycopy() method.  The arraycopy() can be used to copy a subset of an array.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public ... Read More

Advertisements