Posts

Showing posts from August, 2020

Reading User Input in Python With Code Sample

In this article, I will explain how to read user input in Python. Python 2 and Python 3 differ slightly in the ways that they support reading user input. Though the main focus of this article would be to demonstrate how to read user input in Python 3, I will quickly touch upon the Python 2 approach as well and explain how it differs from Python 3 Reading User Input in Python 3 Python 3 provides a method input() . You can use this to read input that a user enters. This method treats the value entered by a used as a String. The following code demonstrates this: name = input("Enter your name:")print("Hello ",name) The input() method accepts as parameter a String that it displays to the user It then assigns the value that the user enters to the name variable The input() method converts the value entered by a user to String . So, if you want to read an integer type, you need to use typecasting. Python provides an in-built method int() that converts a value to an Intege

IntFunction in Java 8 With Code Sample

In this blog post, I will be explaining how the Java 8 functional interface  IntFunction  works. To know more about functional interfaces, you can refer to this  blog post. Edit What is IntFunction IntFunction is an in-built functional interface in the java.util.Function package. It accepts an argument of int data type, operates on it and produces a result of any data type. It is a specialization of the Function interface. It has an apply method. It applies the logic in this method on the int argument passed in and produces a result of the specified data type. IntFunction Code Sample The following code demonstrates this   interface: IntFunction<Double> doubleRetriever = input -> Double.valueOf(input);System.out.println("Double value " + doubleRetriever.apply(5)); This code implements this interface via a lambda expression that returns a result of Double type. It simply returns a Double value corresponding to the int input passed in. This code prints the f

What is the difference between a module and a package

Java 9 has introduced the concept of modules in Java. You can take a look at this article which explains more about modules. Here, I will be explaining the difference between a module and a package in Java. Edit Package Module A package cannot be deployed by itself A module can be deployed by itself A package groups together related classes A module groups together related packages Packages are present in Java right from the beginning Modules were added by Java 9 Packages were added to keep related classes together and to allow developers to have a class with the same name in a different packages Modules were added for security reasons and to reduce the size of the JDK Classes defined within a package are accessible via reflection even if they are private Classes defined within a module are not accessible outside the module via reflection Packages do not require a package descriptor Modules require a module descriptor which is a file called module-info.java