Posts

Showing posts from April, 2021

Python While Loop with code samples

Much like a Java while loop, you can use a Python while loop to repeatedly execute a block of code as long as a condition is true. In this article, we will learn more about the Python while loop. Python While loop Syntax A while loop in Python has the following syntax: while <condition>: loop body The while keyword is followed by a condition. If the condition evaluates to true, the body of the loop is executed. Once the body of the loop is completed, the condition is checked again and this is repeated until the condition evaluates to false at which point the loop is exited. While loop Sample Code The following code demonstrates how you can use the while loop: num=10counter=1total=0while counter<=num: total += counter counter+=1print("sum of first 10 numbers is",total) This code adds the numbers from 1 to 10 and prints the sum. It uses a while loop to iterate from 1 to 10. The while loop checks the condition counter <= num and if so adds the current value of count

Parallel Streams in Java with code samples

In my earlier article, I had covered the Stream API in depth. In this article, we will be taking a look at parallel streams. Simply speaking, a parallel stream is a stream that operates on its elements in parallel. How to obtain a parallel stream Just like the stream method, all the Collection interfaces have a parallelStream method. This returns a parallel stream on the underlying Collection. Consider the following code: List<String> food = Arrays.asList("Pizza","Sandwich","Burger","Rice");Stream<String> foodStr = food.parallelStream(); This code first creates a String List food with some values. It then invokes the parallelStream method on food. This returns a String Stream which is a parallel stream. In addition, the Stream interface also has a method parallel . If the stream on which this method is invoked is a sequential stream, it returns a parallel stream corresponding to it. If the stream on which it is invoked in a paral

Java Inheritance Interview Questions With Examples

In this article, I will be listing some Java inheritance interview questions What are the benefits of inheritance in Java? Answer: Inheritance has the following benefits: It permits code reuse. So, if there is some common code that can be used across a set of subclasses it can be specified in the base class and reused by all the subclasses. It facilitates polymorphism via method overriding. Method overriding allows a subclass to specify a method with the same type signature as a method in the superclass.  To know more about method overriding refer to this blog post. It makes the code extendable. So, if there is already some code, developers can use the code by creating a subclass and they need not write it again What are the different types of inheritance? Answer: Java supports the following types of inheritance: Single Inheritance – In this case, a class has only one subclass Multi-level Inheritance – In this case, a subclass in turn has a subclass Hierarchical Inheritance – In this

IntBinaryOperator

In this blog post, I will be explaining how the Java 8 functional interface  IntBinaryOperator works. To know more about functional interfaces, you can refer to this  blog post. What is IntBinaryOperator IntBinaryOperator is an in-built functional interface in the java.util.Function package. It accepts two int arguments, operates on it and produces a result of type int . It is a specialization of the BinaryOperator interface. It has a applyAsInt method. It applies the logic in this method on the int arguments passed in and produces an int result. IntBinaryOperator Code Sample The following code demonstrates this   interface: IntBinaryOperator intBinaryOp = (num1,num2) -> num1+num2;int input1=14,input2=12;int result = intBinaryOp.applyAsInt(input1, input2);System.out.println("result:"+result); Line 1 declares a IntBinaryOperator instance intBinaryOp and implements it via a lambda expression that adds the input arguments and returns the result. Line 3 invokes the applyA