Skip to main content

Posts

Why String is Immutable and Final in Java

Recent posts

Why String is Stored in String Constant Pool

In a previous article Why String is Immutable and Final in Java , I have discussed why String is immutable in nature and advantages and disadvantages String's immutability gives us. I have also discussed that, all String literals are cached into a special memory area called String Constant Pool and how String's immutability made String constant pool possible. But the question arises why do Java required a separate constant pool to store Strings, What's the reason, Why strings are not stored in the normal heap memory like other objects do and in this article, I will try to answer these questions. String Interning Well, we know String is the most popular type present in Java and almost all Java programs use it. In fact, I have not seen a single Java program which is written without using String. In general, a normal Java business application deals with thousands of string objects, lots of them have same value associated and lots of them are mid operations string means they a

What is Variable Shadowing and Hiding in Java

Java allows us to declare a variable whenever we need it, We can categorize all our variables into 3 categories which have different-different scopes Instance Variables - Defined inside a class and have object level scope. Class Variables - Defined inside a class with static keyword, have class level scope common to all objects of the same class Local Variables - Defined inside a method or in any conditional block, have the block-level scope and only accessible in the block where it defined. What is Variable Shadowing Variable shadowing happens when we define a variable in a closure scope with a variable name and we have already defined a variable in outer scope with the same name. In other words, when a local variable has the same name as one of the instance variable, the local variable shadows the instance variable inside the method block. In the following example, there is an instance variable named x and inside method printLocalVariable(), we are shadowing it by the local vari

Why We Should Follow Method Overriding Rules

In my previous articles  Why Should We Follow Method Overloading Rules , I discussed about method overloading and rules we need to follow to overload a method. I have also discussed why we need to follow these rules and why some method overloading rules are necessary and others are optional. In a similar manner in this article, we will see what rules we need to follow to override a method and why we should follow these rules. Method Overriding and its Rules As discussed in  Everything About Method Overloading Vs Method Overriding , every child class inherits all the inheritable behaviour from its parent class but the child class can also define its own new behaviours or override some of the inherited behaviour. Overriding means redefining a behaviour (method) again in the child class which was already defined by its parent class but to do so overriding method in the child class must follow certain rules and guidelines. With respect to the method it overrides, the overriding method must

Why Should We Follow Method Overloading Rules

In my previous articles, Everything About Method Overloading Vs Method Overriding and  How Does JVM Handle Method Overloading and Overriding Internally , I have discussed what is method overloading and overriding, how both are different than each other, How JVM handles them internally and what rules we should follow in order to implement these concepts. In order to overload or override a method we need to follow certain rules, some of them are mandatory while others are optional and to become a good programmer we should always try to understand the reason behind these rules. I am going to write two articles where I will try to look into the method overloading and overriding rules and try to figure out why we need to follow them. In this article, we will see what rules we should follow to overload a method and we will also try to know why we should follow these rules. Method Overloading In general, method overloading means reusing same method name to define more than one method but all

Everything About Method Overloading Vs Method Overriding

In a previous article  Everything About ClassNotFoundException Vs NoClassDefFoundError , I have explained ClassNotFoundException and NoClassDefFoundError in details and also discussed their differences and how to avoid them. If you have not read it, please go ahead and give it a look. Similar to that here in this article, we will look into one more core concept of Java which is method overloading and overriding. As soon as we start learning Java we get introduced to them and their contracts which are pretty simple to understand but sometimes programmers get confused between them or they do not know either it is a correct overload/override because of the different rules. Here we will discuss What is method overloading and overriding, What contract one must follow to correctly overload or override a method, What are the different rules of method overloading and overriding and what are the differences between them. Method Overloading Method overloading means providing two separate methods

How Does JVM Handle Method Overloading and Overriding Internally

In my previous article Everything About Method Overloading Vs Method Overriding , I have discussed method overloading and overriding, their rules and differences. In this article, we will see How Does JVM Handle Method Overloading And Overriding Internally, how JVM identifies which method should get called. Let’s take the example of parent class  Mammal and a child  Human classes from our previous blog to understand it more clearly. public class OverridingInternalExample { private static class Mammal { public void speak() { System.out.println("ohlllalalalalalaoaoaoa"); } } private static class Human extends Mammal { @Override public void speak() { System.out.println("Hello"); } // Valid overload of speak public void speak(String language) { if (language.equals("Hindi")) System.out.println("Namaste"); else System.out.println("Hello"); } @