IMPORTING
PACKAGES/CLASS IN JAVA
Imagine
that you decide to make a pizza and you need different kinds of vegetables and
cheese to make one. Now won’t it be better option if you have two baskets, one
containing vegetables and one containing different types of cheese? Definitely
it would be a better option over one basket containing both the things mixed.
But however, your both the baskets will be inside your kitchen.
So Java
packages are nothing but the kitchen in this case, and you can use any one or
more things from it for your pizza. The only difference being here is that the vegetables
and cheese are actually the different classes, which bundle up to go inside a
basket or a Package.
Importing a package
We can
import a class in Java in the following way:
import java.util.*;
We can also
use any one class from the java.util package according to our requirement in
the code. For example, to use Arrays.sort (sorts the integers in the array in
ascending order) method in java we have to import java.util.Arrays class
Ex:
import java.util.Arrays;
public class Coding_Winds {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ar[]= {2,4,3,1,5};
Arrays.sort(ar);
for(int i=0;i<5;i++) {
System.out.println(ar[i]);
}
}
}
Output :Hope you are clear on this topic do read our more articles on JAVA LANGUAGE.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
This article is SUBMITTED By : Pranjal Rai
Do subscribe to our daily blog update by clicking here.
Thank You!