Found 9150 Articles for Object Oriented Programming

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

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

237 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

172 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

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

945 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

226 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

406 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

Difference between sums of odd and even digits.

Mahesh Parahar
Updated on 16-May-2020 14:49:21

550 Views

Problem StatementWith a given long integer n, write a program to find the difference between sum of the odd digits and even digits to be 0 or not. Index starts from 0.Examplen = 1212112 Sum of odd position elements = 2 + 2 + 1 = 5 Sum of even position elements = 1 + 1 + 1 + 2 = 5 Difference = 5 - 5 = 0 Output = YesExample Live DemoFollowing is the program in Java to find the required output.class JavaTester {    public static int difference(int n){       return (n % ... Read More

Difference between sum of the squares of and square of sum first n natural numbers.

Mahesh Parahar
Updated on 16-May-2020 14:45:24

967 Views

Problem StatementWith a given number n, write a program to find the difference between sum of the squares of and square of sum first n natural numbers.Examplen = 3 Squares of first three numbers = 3x3 + 2x2 + 1x1 = 9 + 4 + 1 = 14 Squares of sum of first three numbers = (3 + 2 + 1)x(3 + 2 + 1) = 6x6 = 36 Difference = 36 - 14 = 22Example Live DemoFollowing is the program in Java to find the required difference.public class JavaTester {    public static int difference(int n){   ... Read More

What are the different "/edit" commands in JShell in Java 9?

raja
Updated on 14-Apr-2020 18:13:27

257 Views

JShell is a command-line tool introduced in Java 9 that evaluates declarations, statements, and expressions without the main() method. JShell can set up a text editor called JShell Edit Pad, which allows us to modify the code very easily, and it can be launched using the "/edit" command.Below are the different "/edit" commands used in Jshell./edit /edit [ID] /edit [Code_Name]/edit: This command can be used without an argument, the "/edit" command displays all the active code in the text editor./edit [ID]: This command displays in the text editor the code corresponding to the ID entered./edit [Code_Name]: This comamnd displays in the ... Read More

Advertisements