Posts

Showing posts from June, 2023

Beautiful java Code…

Image
An argument is a value or expression passed as input to a method, and a parameter is a variable defined within the method to hold this input. In Java, a null value represents the absence of a value, and attempting to perform operations on a null value parameter will result in a Null Pointer Exception. Similarly, if a method returns a null value, the caller should handle it appropriately to avoid a NullPointerException. However, it is generally recommended to avoid returning null values whenever possible. Let’s take a look at the example method… public static Double average(final Double... values) {   if (values == null) {     return null;   }   if (values.length == 0) {     return null;   }   double total = 0.0;   for (double value : values) {     total += value;   }   return total / values.length; } Will this fail at compile time? No Will it fail at runtime? Yes An array can be null or empty. In this case, if the array is not of primitive type but a wrapper class, we need to check if

6 Design Patterns in Java that Solve Major Problems!

Image
 PATTERN PROBLEM In a previous post, I showed you how the OOP world looks like.  OOP is a pretty convenient paradigm for identifying requirements while also making them easier to understand. Different OOP concepts like inheritance and polymorphism have a number of use cases in popular applications. However, in some situations, OOP on its own is not enough. To implement a particular feature, you may need to use OOP concepts in a way that makes your code better and more understandable. In this post, you’ll learn 5 design patterns that can make your life better as a Java programmer. Before that, let’s get two things out of the way. Why Design Patterns? Let’s take a class  Boat  that has different sub classes representing the types of boats. A  Boat  object performs two actions, sway and roll. abstract class Boat { void sway () { ... } void roll () { ... } abstract void present () ; } The  sway()  and  roll()  methods are inherited by each sub class as they are the sam