Monday, 22 August 2016

Why we always do import for java classes?

Hi Guys, It has been a while since I made this site without publishing any content at all. It's actually one of my weaknesses, I always try something new and never finished it.

Anyhow, let me get back again on why I have started this blog site. Well basically this is because I am so bored and wanted to try something new. And then one day, after coming from tiring office work, I have thought how about try to start a blog and let's see how it goes.

So let me start first with the discussion on providing on why standard classes are not built in with java like for example Scanner class. We always do like

import java.util.Scanner;
import java.util.Random;

and then we follow it up with

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();
}


Why not just go straight to use Scanner without importing. Well to answer that question, It makes the java language more complex without adding benefits. As I can see it, the only advantage to do so is by removing the import directive at the start of the code. It will deviate to the standard/META on how we instantiate a class. So I believe that's the reason why Java is built like this.

Let me enumerate what are the numerous disadvantage.

First reason, It will introduce a large number of reserved keywords into the language. Let's say you want to create a new BELL. What do you call the class? Random? No, you can't do that, because the name is already in use. So everyone has to be aware of all the different class names that are built in. It must be considered that Java Language has numerous classes. And to consider for all of them to be a reserved keywords, is quite troublesome as the class name are not even unique depending on the package you have loaded to your environment. You can extend the String class if you want to, just load it properly by implicitly specify it in java environment.

Second, you create implementation complexity. When you state that Scanner is actually a part of the language, it has to implemented as a new grammar rule. Lexing and parsing and optimization now need to take this name into account, for no benefit, because like I said, these classes don't actually have any unique grammar behind them. You do not want exploding complexity to implement and maintain in your compilers.

Third, you create language inflexibility. You can't create a lean version and implementation of the Java platform, because to implement the language, you have to implement all those classes. They are built into the language itself, after all.

That's all folks will write more again of this on the next post. Stay tune

~Ryan