9

I just want to output current and I wrote

import java.util.*;

at beginning, and

System.out.println(new Date());

in the main part.

But what I got was something like this:

Date@124bbbf

When I change the import to import java.util.Date; the code works perfectly, why?

====================================

The problem was, OK, my source file was "Date.java", that's the cause.

Well, it is all my fault, I confused everybody around ;P

And thanks to everyone below. It's really NICE OF YOU ;)

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
EthanZ6174
  • 382
  • 2
  • 3
  • 12
  • 1
    You could have other imports - let us see some compilable code that shows this – mmmmmm Oct 30 '09 at 12:14
  • 1
    See also: http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad, http://stackoverflow.com/questions/187453/import-package-vs-import-package-specifictype, http://stackoverflow.com/questions/585268/which-would-make-a-class-file-bigger-import-java-awt-or-a-bunch-or-single-imp, http://stackoverflow.com/questions/1553909/is-there-any-difference-between-class-imports-and-package-imports-in-java – finnw Oct 30 '09 at 12:28
  • 1
    Voting up because sometimes a good counter-example is as helpful as anything. :) – mtruesdell Oct 30 '09 at 12:55
  • thx you all @Mark i named the class "Date" and that's the problem @finnw thx for quoting @mtruesdell pity, i can not voting yet.. a very newbie here i am – EthanZ6174 Oct 30 '09 at 13:05
  • In you first example, do you perchance import anything else? E.g. java.sql.Date? – lindelof Oct 30 '09 at 12:14

5 Answers5

13

You probably have some other "Date" class imported somewhere (or you have a Date class in you package, which does not need to be imported). With "import java.util.*" you are using the "other" Date. In this case it's best to explicitly specify java.util.Date in the code.

Or better, try to avoid naming your classes "Date".

sleske
  • 81,358
  • 34
  • 189
  • 227
  • 2
    @EthanZ6174: See Puzzle 7 in this sample list from Java Puzzlers - http://www.javapuzzlers.com/java-puzzlers-sampler.pdf. That book is a definite recommend for any Java programmer! – Vijay Dev Oct 30 '09 at 12:38
6

The toString() implementation of java.util.Date does not depend on the way the class is imported. It always returns a nice formatted date.

The toString() you see comes from another class.

Specific import have precedence over wildcard imports.

in this case

import other.Date
import java.util.*

new Date();

refers to other.Date and not java.util.Date.

The odd thing is that

import other.*
import java.util.*

Should give you a compiler error stating that the reference to Date is ambiguous because both other.Date and java.util.Date matches.

Ashish Kumar
  • 916
  • 2
  • 15
  • 32
Fedearne
  • 7,049
  • 4
  • 27
  • 31
4
import java.util.*;

imports everything within java.util including the Date class.

import java.util.Date;

just imports the Date class.

Doing either of these could not make any difference.

Chris R
  • 2,464
  • 3
  • 25
  • 31
2

Your program should work exactly the same with either import java.util.*; or import java.util.Date;. There has to be something else you did in between.

cherouvim
  • 31,725
  • 15
  • 104
  • 153
  • thanks for answering the question, and i knew there is no difference between those, unless I named the src "Date.java" ...:( – EthanZ6174 Oct 30 '09 at 12:24
0
but what I got is something like this: Date@124bbbf  
while I change the import to: import java.util.Date;  
the code works perfectly, why? 

What do you mean by "works perfectly"? The output of printing a Date object is the same no matter whether you imported java.util.* or java.util.Date. The output that you get when printing objects is the representation of the object by the toString() method of the corresponding class.

Vijay Dev
  • 26,966
  • 21
  • 76
  • 96