top of page
No tags yet.

SEARCH BY TAGS: 

RECENT POSTS: 

FOLLOW ME:

  • Facebook Clean Grey
  • Twitter Clean Grey
  • Instagram Clean Grey

Fundamentals of Java Scanner Class

Hello everyone, after exhausting day I got the itch to get onlne and do some publishing content. it kept me thinking that Instead of watching movies why not try to do write an article every once in a while for a change.

Anyhow, what would I want to discuss today. Well i came across on an interesting question on of the java blog and i would like to answer it here.

The question is like this. Why standard classes are not built in with java like for example Scanner class? Like for instance we have below source code

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(); }

We always start it off with importing the desire packages like for above we start first with Scanner and then Random. The point is why not directly just use Scanner anyway it's part already of java API. 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 disadvantage of incorporating default classes on the java language.

First and foremost, It will introduce a quite large number of reserved keywords into java language. Let's say you want to create a new FAN. What do you call this class? Random? No, you can't do that, because the name is already in use and cannot be used any further. 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. Imagine memorising thousands of reserved keywords and to avoid compilation issues so as not to use it gain. That would be quite an issue. Java is already a complicated programming language, and this will complicate it even more. You can extend the String class if you want to, just load it properly by implicitly specify it in java environment.

Secondly, 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 optimisation 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.

I would suggest to visit http://javatutorialhq.com/java/util/scanner-class-tutorial/ to learn more about java scanner. That's all folks, will write more again of this on the next post.


bottom of page