Skip to main content

Posts

Showing posts with the label Why in Java

Why String is Immutable and Final in Java

While coding in any programming language we always require some predefined types which we can use to write the code and every programming language provides these types in its way e.g. Java provides primitive types ( int, long, char float etc) and reference types (custom types like Object, String, Thread ). For string manipulation, Java provides a class java.lang.String which gives us a way to create string objects and provides different behaviors to operate on those objects e.g. replace(), length() String name = "Naresh"; System.out.print(name.length()); System.out.print(name.isEmpty()); Whenever we talk about String class in Java we say it is i mmutable in nature and all string literals are stored in String Constant Pool (SCP) . Prior to Java 7 String Constant Pool belongs to Permanent Generation area of heap which means Garbage Collector will not touch it in normal scenarios. But from Java 7 onwards string constant pool is not part of Perm Gen but live with o...

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...

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 me...

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...

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 t...

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"); } @...

Everything About ClassNotFoundException Vs NoClassDefFoundError

We know Java is an Object Oriented Programming Language and almost everything is an object in Java and in order to create an object we need a class. While executing our program whenever JVM find a class, First JVM will try to load that class into memory if it has not done it already. For Example, If JVM is executing below the line of code, before creating the object of Employee class JVM will load this class into memory using a ClassLoader. Employee emp = new Employee(); In above example, JVM will load the Employee class because it is present in the execution path and JVM want to create an object of this class. But we can also ask JVM to just load a class through its string name using Class.forName() or ClassLoader.findSystemClass() or ClassLoader.loadClass() methods. For Example below the line of code will only load the Employee class into memory and do nothing else. Class.forName("Employee"); Both ClassNotFoundException  and  NoClassDefFoundError occur when a particular c...

Why an outer Java class can’t be static

In a previous blog , I talked about why we can not define an outer class using private or protected keywords. If you have not read it, please go ahead and give it a look. I this article I will talk what is the use of the static keyword, why an outer Java class can’t be static, why it is not allowed in Java to define a static outer class. In order to understand that first, we need to understand what is the static keyword used for, what purpose it solves and how does it works. What does static keyword do Every Java programmer knows that if we need to define some behavior (method) or state (field) which will be common to all objects we define it as static. Because static content (behavior or state) does not belong to any particular instance or object, it will common to all objects and all objects are free to change any static field and every change will be visible to every object. We do not need to create any object of the class to access a static field or method, we can directly...

Why an outer Java class can’t be private or protected

As soon as we try to use private or protected keyword while declaring an outer class compiler gives a compilation error saying “Illegal modifier for the class your_class_name; only public, abstract & final are permitted”. Here in this article, we are going to study why we are not allowed to use these keywords while declaring outer classes. But before understating the reason behind this we need to understand Java access specifiers and their use cases. There is total 4 access specifier in Java mentioned below in the order of their accessibility. private:  anything (field, class, method, interface etc.) defined using private keyword is only accessible inside the entity (class or package or interface) in which it is defined.  default:  only accessible inside the same package and it is also known as package-private (No modifiers needed). protected:  only accessible inside the same package plus outside the package within child classes through inheritance only....

Why Java is Purely Object Oriented Language Or Why Not

Some years back when I have started learning Java I get to know that Java follows Object Oriented Programming paradigm and everything in Java is an object either it is a String (which was a char array in C) or an array itself. But later on I found on Internet people saying that Java is actually not purely Object Oriented because everything in Java is not an object, for example: All primitive types ( char, boolean, byte, short, int, long, float, double ) are not objects because we are not able to do any object like operation (using . and calling methods) on them. I have also found some people some saying that all static content (variables and methods) does not belong to any object so they are non object things. Due to my little bit knowledge and less experience I easily accepted these reasons and started to believe that Java is not a purely Object Oriented Programming Language. But later on I found that for every object JVM creates two objects The object itself. And one Class level ob...