Found 2617 Articles for Java

How can we create an instance of VarHandle in Java 9?

raja
Updated on 16-Apr-2020 14:11:28

141 Views

In general, a Variable Handle is simply typed reference to a variable. It will be an array element, an instance or static field of the class. VarHandle class can provide write and read access to variables under specific conditions. These are immutable and have no visible condition. In addition, they can't be sub-classified, and each VarHandle has a generic type T, which is the type of each variable represented by this VarHandle. The objective of VarHandle is to define a standard for calling equivalents of java.util.concurrent.atomic and sun.misc.Unsafe operations on fields and array elements.In the below example, we can use ... Read More

How to implement Flow.Publisher interface in Java 9?

raja
Updated on 16-Apr-2020 10:36:29

637 Views

A Publisher interface is a provider of an unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s). In response to call Publisher.subscribe(Subscriber), the possible invocation sequences for methods on the Subscriber. It means that the onSubscribe() method, followed by the unbounded number of onNext() methods (as requested by Subscriber) followed by an onError() method, if there is a failure or an onComplete() method when no more elements available as long as Subscription is not canceled.Syntaxpublic interface Publisher {    public void subscribe(Subscriber

Difference between Java and Kotlin in Android with Examples

Mahesh Parahar
Updated on 16-Apr-2020 05:57:32

205 Views

Kotlin was introduced in Android development considering multiple enhancements in Kotlin w.r.t Java. For example:Less no. of Lines and easier development with the same functionality.Java: TextView displayText = (TextView) findViewById(R.id.textView); displayText.setText("Hello World"); Kotlin: textView.setText("Hello World")Compile-time handling of infamous null pointer exception.var value: String = "abc" // compilation error value = nullData class instead of POJO.data class User(val name: String, val age: Int)The following are some of the important differences between Java and Kotlin.Sr. No.KeyJavaKotlin1ExceptionsJava uses checked exceptions for exception handling.Kotlin has no checked exception. It throws compile-time errors.2Null HandlingJava has not enforced null check thus null pointer exception ... Read More

How can we implement methods of Stream API in Java 9?

raja
Updated on 15-Apr-2020 17:55:40

169 Views

Stream API provides lots of built-in functionality to help in performing operations on a collection using a stream pipeline. The API is declarative programming that makes the code precise and less error-prone. In Java 9, few useful methods have added to Stream API.Stream.iterate(): This method can be been used as stream version replacement for traditional for-loops.Stream.takeWhile(): This method can be used in a while loop that takes value while the condition is met.Stream.dropWhile(): This method can be used in a while loop that drops value while the condition is met.In the below example, we can implement the static methods: iterate(), takeWhile(), and dropWhile() methods of Stream ... Read More

What are the different startup scripts in JShell in Java 9?

raja
Updated on 15-Apr-2020 13:50:12

102 Views

JShell is an interactive Java Shell tool that executes code from the JShell and instantly displays an output. JShell is the REPL (Read-Evaluate-Print-Loop) tool that can run from the command-line prompt.In JShell, there is an option to load a script on startup that includes some special predefined options. These can be specified using the "--startup" flag passing in either a filename or one of DEFAULT, JAVASE, and PRINTING. We can use "/list -start" comamnd to see all startup snippets to be evaluated.DEFAULT: It loads the default behavior. This acts the same as if this is not specified at all.JAVASE: It imports all ... Read More

What are the advantages and disadvantages of the Module System in Java 9?

raja
Updated on 15-Apr-2020 09:54:52

883 Views

A major change in Java 9 version is Module System, and it provides modular JVM that runs on devices with less available memory. The JVM runs with only those modules and API required by an application.module Module-Name { requires moduleName; exports packageName; }Below are some of the advantages and disadvantages of the Module System.Advantages of Module:The main change in Java 9 is that it is now a module system with a modular JDK, modular source code, and modular run-time images.Internal APIs are hidden in a module.A module system creates more opportunities for the development ... Read More

Difference between Traits and Abstract Classes in Scala.

Mahesh Parahar
Updated on 16-May-2020 11:33:34

1K+ Views

TraitsTraits are similar to interfaces in Java and are created using trait keyword.Abstract ClassAbstract Class is similar to abstract classes in Java and are created using abstract keyword.Example Live DemoFollowing is the program in Scala to show the usage of Traits and Abstract Classes.trait SampleTrait {    // Abstract method    def test    // Non-Abstract method    def tutorials() {       println("Traits tutorials")    } } abstract class SampleAbstractClass {    // Abstract method    def test    // Non-abstract meythod    def tutorials() {       println("Abstract Class tutorial")    } } ... Read More

Difference between the largest and the smallest primes in an array in Java

Mahesh Parahar
Updated on 16-May-2020 11:29:51

760 Views

Problem StatementWith a given array of integers where all elements are less than 1000000. Find the difference between the largest and the smallest primes in an array.ExampleArray: [ 1, 2, 3, 4, 5 ] Largest Prime Number = 5 Smallest Prime Number = 2 Difference = 5 - 3 = 2.SolutionUse Sieve of Eratosthenes approach, which is an efficient way to find out all prime numbers smaller than a given number. Then we will figure out the largest and smallest prime number to get the required difference.Example Live DemoFollowing is the program in Java to find the required output.public ... Read More

Difference between sums of odd position and even position nodes of a Binary Tree in Java

Mahesh Parahar
Updated on 16-May-2020 14:54:17

153 Views

Problem StatementWith a given binary tree, write a program to find the difference between sum of nodes at odd position and even position. Assume root at level 0, odd position, left/right child of root at level 2, left child at odd position and right child at even position and so on.Example    5    / \   2   6 / \ \ 1 4 8 / / \ 3 7 9 Sum of nodes at odd positions = ... Read More

Difference between sums of odd level and even level nodes of a Binary Tree in Java

Mahesh Parahar
Updated on 16-May-2020 14:50:36

329 Views

Problem StatementWith a given binary tree, write a program to find the difference between sum of nodes at odd level and even level. Assume root at level 1, left/right child of root at level 2 and so on.Example        5       /   \       2     6      /  \     \     1     4    8    /    /  \   3    7    9 Sum of nodes at odd level = 5 + 1 + 4 + 8 = 18 Sum of ... Read More

Advertisements