minte9
LearnRemember



Import package

To import a specific package into the current file, use import statement.
 
/**
 * If you do not use a package statement ...
 * your type (your class) goes in an unnamed package.
 * 
 * Java compiler automaticaly imports three packages:
 * 
 * (1) the package with no name
 * (2) the java.lang package
 * (3) the current package
 */

package com.minte9.oop.packages;

import java.util.ArrayList;

public class Import {
    public static void main(String[] args) {
        
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);

        System.out.println(list); // [1, 2]
    }
}

Hierarchy

At first, packages appear to be hierarchial, but they are not.
 
/**
 * Importing java.uitl.* imports all of the types ...
 * in the java.util package but it does not import ...
 * java.util.regex package
 */

package com.minte9.oop.packages;

//import java.util.*; // not used
//import java.util.regex.Matcher; // Look Here
import java.util.regex.Pattern;

public class Hierarchy {
    public static void main(String[] args) {
        
        Pattern p = Pattern.compile("Java");
        java.util.regex.Matcher m = p.matcher("Java SE 8");

        System.out.println(m.find()); // true
    }
}



  Last update: 233 days ago