1

What's the difference between a package and an import? Please give an example.

Why can't we just use import java.util.*;?
Doesn't it give access to all the others automatically?

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
public static void main( String args[] ){

  // String to be scanned to find the pattern.
  String line = "This order was placed for QT3000! OK?";
  String pattern = "(.*)(\\d+)(.*)";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  if (m.find( )) {
     System.out.println("Found value: " + m.group(0) );
     System.out.println("Found value: " + m.group(1) );
     System.out.println("Found value: " + m.group(2) );
  } else {
     System.out.println("NO MATCH");
  }
 }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
hridayns
  • 697
  • 8
  • 16
  • "*Whats the difference between a package and an import?*" `import` imports class/classes from specified `package`. "*Please give an example.*" you already posted one. "*Why cant we just use `import java.util.*;`*" you can, nothing stops you. "*Doesn't it give access to all the others automatically?*" others *what*? – Pshemo May 10 '14 at 11:13
  • You can use `import java.util.regex.*;` to import both classes. – Sergey Kalinichenko May 10 '14 at 11:14

4 Answers4

2

sentence "import java.util." imports only the classes under the util package. You can use "import java.util.regex." to import Matcher and Pattern classes . A package is a namespace for your classes, it's used to group related classes togheter and for access privilege reasons.

  • Ok im still not fully clear on package. But i think im starting to understand about import. So, util is actually the name of a package. and regex is a class? – hridayns May 10 '14 at 14:05
  • Maybe its the NetBeans interface thats confusing me. – hridayns May 10 '14 at 14:09
1

Package is used to put all one Module related into one specified Folder for a better understanding ,whereas import is used to import the specific class that we need to run our application/class(Like we use Java.Util...etc).

Akhil Garg
  • 19
  • 2
  • Can you post some code to show me the use of multiple packages? Can you even use multiple packages? – hridayns May 10 '14 at 14:07
0

package name is user defined its what you give,

Ex: package test;

import package is used to get already predefined packages in java to be used in your current package

Ex: if you want to use "util" package in your test package,

package test;

java.util.Scanner;

Here util is java predefined package and Scanner is the class present in util package.

-1

Package is used to put all our related classes into one specified Folder for a better understanding ,whereas import is used to import(get) the needed or the dependent classes to run our application/class.

Khyathi36
  • 21
  • 1
  • 1
  • 2
  • Is there some reason I should prefer either: 1) package 2) import – hridayns May 10 '14 at 14:10
  • It depends on the requirement ,for example if you have a requirement which needs some information related to Date then to get that Date class and their methods we need to import Java.util package. Package is a set of classes which needs to be imported – Khyathi36 May 12 '14 at 04:26