Java Concepts - What everyone knows vs What you should know? :  Part 1

Java Concepts - What everyone knows vs What you should know? : Part 1

Well, Java is a very popular programming language from the past 24 years. Yes, 24 years, can you believe it, as much as old as some of us might be. This article is not just about some Java concepts (available all over the internet) to bore you but it's about some interesting java concepts you should be really aware of, no matter if you are a professional or a student or a person preparing for a java interview.

Why Java is so popular till now?

What everyone knows

Java is object-oriented, strongly typed, platform-independent (WORA - write once run anywhere) and allow concurrent execution of code via threads.

What you should know

Generally, a thing gets popular either when its competitors lose popularity or it gets so many advanced features to attract people. With increasing computing power and demand for churning large volumes of data available, only programming languages which can harness a parallel computing power with its feature-rich toolkit and, of course, associated features can flourish.

There are three forces keeping Java popular worth to be mentioned:

  1. First, Java has always proved without a doubt to be the greatest platform for developing multi-processor applications as it has a feature-rich toolkit for multi-threaded and multi-processor applications
  2. The second force is about capturing behaviour and passing it around like a piece of data.Java represents state and behaviour on a class as variables and methods respectively as we all know.Java 8 introduced lambdas for representing behaviour. Lambdas are free-floating code expressions, thus, supporting functional programming style that can be assigned to a variable just as we initialize a state in Java class or passed around to another method just like we pass data as arguments
  3. The final force is a mechanism of enhancing the languages application libraries without creating a lot of chaos. The heart of any framework library is a simple humble interface. These interfaces cannot be changed once they are made public without breaking the backward compatibility. In Java 8, the interfaces were reborn. They can be modified without having to worry about backward compatibility anymore.
Interface or an Abstract Class? What should I choose?

What everyone knows

Since Java doesn't support multiple inheritance, so you can extend a single class but implement multiple interfaces. Also, in the abstract class, you can define some of the methods whereas all the methods in the interface are abstract. The fields in the interface can only be public, static or final whereas in abstract classes there is no such restriction.

What you should know

With Java 8, you can define default and static methods in an interface.Actually,

The interface is used when you want to define a contract and you don't know anything about implementation. (here it is total abstraction as you don't know anything.)

An abstract class is used when you know something and rely on others for what you don't know. (here it is partial abstraction as some of the things you know and some you don't know.)

Let's take an example of this earth, all living organisms can breathe, eat and move.

So it's a contract, we have no idea about how a particular organism is going to breathe, eat or move. It will totally depend on the classes(human, fish, pigeon, tiger etc) implementing that interface(living organism).

Also, some of the living organisms are mammals. So we know some behaviour of mammals like they breathe via lungs but we are not sure about their eating habits or they move via 2 or 4 limbs. So mammal should be an abstract class in this case.

So human class can be implementing living organism interface and can be extending mammal class.

So the decision is made on the basis of above-mentioned points.

How String class is special?

What everyone knows

The objects of String class are immutable and final in nature thus increase security. i.e you can’t modify them once they are created so we can use them to store sensitive data like username, password etc. Strings are thread-safe. So, we can use them in a multi-threaded code without synchronization.

What you should know

Well, String is the second most commonly used class after Object. Hence, efficiency (in terms of computation and storage) is crucial.

There are two ways to construct a string: implicit construction by assigning a string literal String s1 = "hello";

or explicitly creating a String object via the new operator and constructor

String s2 = new String("hello");

Java has provided a special mechanism for keeping the String literals – in a so-called string common pool. String Pool is a special memory location inside Java memory, more precisely inside PermGen Space. If two string literals have the same contents, they will share the same storage inside the common pool. This approach is adopted to conserve storage for frequently-used strings. On the other hand, String objects created via the new operator and constructor are kept in the heap. Each String object in the heap has its own storage just like any other object. There is no sharing of storage in a heap even if two String objects have the same contents.

String s3 = "hello";

So s1 will be residing in string pool whereas s2 will reside in heap memory. Also, s1 and s3 will be pointing to the same memory address in the string pool.

If you liked the post, here is the link of the next blog of the series Java Concepts: Part 2

No one is perfect so am I. Please feel free to add your views in the comments section.

Arpit Agrawal

Seasoned BackEnd Java Engineer | Research-Oriented Tech Enthusiast | Cloud Specialist

5 年

Awesome article I ever read ..... Thanks Ayushi for sharing this knowledge , I am waiting for your next article .

回复
Akash Bisariya

Lead | Android | Kotlin | Flutter

5 年

very informative...

回复
Parshuram Patil

Consultant | R&D | Full Stack Developer | DevOps

5 年

Must know stuff for everyone especially one who is responsible for optimization and profiling. I believe from java 8 there no PremGen, it's MetaSpace now. And is it really strings are stored in PremGen or MetaSpace for that matter? Any supporting official documentation for this?

要查看或添加评论,请登录

Aayushi Khandelwal的更多文章

社区洞察

其他会员也浏览了