Blogger templates

The use of mysterious import java.util.Scanner; in the top of our code

Let’s leave it and consider this for a moment.
What we know about variables?
How we make them?

If we want to make a variable called x with a value of 10, we;d simple do

int x = 10;

We’ve created a new variable of the type int and named it x and assigned the number 10  as its value.
Simple enough.
Now we got a variable x containing a value of 10 (…and of course of type int)
It has 4 main parts in it. To make a variable we need these parts to tell computer exactly what to do (cause as we know, computers aren’t very smart at it)
Let’s look at the parts,
the int at the beginning of this line (or statement in the language of a programmer,  same thing) tells computer exactly what type this variable is going to be.
For example, if we write x = 10; the computer will be very much confused! What to do it!
Shall I take it as an int or as a short. What about long?

So, we see it gets very confusing. That’s why writing int at the beginning is so important.

In the second part, we just name the variable, it can be anything (apart from some rules) from my name to your name to jackfruit123X, I mean anything.

Then we have an assignment operator (the “=” sign) which stores the value 10 in the variable.

Now consider, what if we want to make another double type variable called y that contains 50.0?

double y = 50.0;
Simple enough.

Now, let’s go back the main topic.
What exactly does the import java.util.Scanner; exactly do?
Earlier we found exactly how important it is to tell computer what type the variable can be.
We wrote int, double or short for telling computer what type of variable it’s going to be.

These types are built into the java language.
So we don’t have to worry about where it comes from!
We just write int x = 10; and voila!
We got an integer.

But as I mentioned earlier, computers aren’t smart at it all!
Now we want a Scanner that’ll scan for user input!
But like variable they also need to be created before using!
It makes sense! We can’t use a Scanner if we don’t have one, can we?
To make a Scanner we’ll probably want to create a scanner like this,
Scanner scan = new Scanner(;
But the problem is, Scanner isn’t strictly needed in java to make a program.
More simply put, Scanner needn’t be built into java language core like int and double.

Recall from classes we only have 7 simple data types (byte, short, int, long, float, double and char). These are the blocks for building more advanced data types. Without these data types java doesn’t know any other data types!
That’s why if we write,
 Scanner scan = new Scanner(;
Computer would be again very confused about what this is about because it has not found anything called Scanner in the java.

But luckily the developers of Java made us some more tools for easy programming.
Recall, we used Math.Sin(90) to find out the value of it.
Now, Math.Sin is also not available in java core language but is available because it was made for us by the developers of Java.

All of it’s called Java Class Libraries and JCL for short.
Scanner is yet another type of variable that’s made by the developers of Java and as we can predict available in the JCL.
But like a real library, the JCL is sooo huge that it was organized like papers in boxes and then boxes in shelves.
So to find any specific paper, we need to know the exact box no. Makes sense, right?
Same for the Scanner.

To tell java where to find this one we need to tell the exact shelf no. and box no. Then java can figure out the paper called Scanner and we can finally use it.

So, there goes the mystery behind the line
import java.util.Scanner;

The import is used to import something, and the java is the self, then util is the box, and in it, finally, the paper called Scanner. A dot(.) is used to access the boxes in the java shelf and the second dot is used to access the papers in the util box. (Of course there aren’t any boxes and papers, but the concept is same!)

No comments

Powered by Blogger.