3

I've been struggling with my first regex. During the compile, Pattern and Matcher kept getting cannot find symbol errors.

I just changed import java.util.* to import java.util.regex.* and it works like a dream.

I was under the impression that importing java.util.* would also bring in java.util.*.* etc. Is that not the case? I can't find any documentation that addresses this specific question....

dwwilson66
  • 6,806
  • 27
  • 72
  • 117
  • So what are some reasons that regex classes can't be found during compile, but CAN be found once I explicitly import the regex package(s)? – dwwilson66 Aug 06 '12 at 19:29
  • 1
    @dwwilson66: they CAN be found, but Java avoids recursive imports (which bloat binary size) by making you explicitly import packages. Which is certainly not a bad idea. – David Titarenco Aug 06 '12 at 19:31
  • 2
    Lets look at is like boxes. Utilities is a box (java.util), with some stuff in it (classes), and some boxes (subpackages). If you just open (import)the Utilities box, you cant get the stuff inside the boxes in it yet (the classes in the subpackages), you still have to open (import) them. – Alex Coleman Aug 06 '12 at 19:31
  • 3
    `java.util.regex.Pattern` matches `java.util.regex.*` but not `java.util.*`. You could just use Eclipse's Ctrl-Shift-O to organize your imports for you. – davidfmatheson Aug 06 '12 at 19:32

4 Answers4

9

No, package imports only get the direct classes in that package (java.* will not import everything, only ones such as Java.SomeClass, not java.util.SomeClass)

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
5

Importing java.util.* will not import java.util.*.*.

FThompson
  • 28,352
  • 13
  • 60
  • 93
3

Yes, that is how package imports work (and are supposed to work) in Java. For example, doing import javax.swing.*; will import all classes within javax.swing.* but not sub-packages and their classes.

Ergo, javax.swing.* will not import javax.swing.event or javax.swing.event.*

Read the following blog for some friendly newbie advice.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
2

See a link and a quoted excerpt from the link below.

http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

import java.awt.*;
import java.awt.color.*;
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21