tags:

views:

515

answers:

6

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 ;)

+1  A: 

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
thanks for answering the question, and i knew there is no difference between those, unless I named the src "Date.java" ...:(
EthanZ6174
+2  A: 
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
+3  A: 

In you first example, do you perchance import anything else? E.g. java.sql.Date?

lindelof
I thought that too, but that doesn't have an empty constructor.
Pool
nope, but i named my src "Date.java", sigh... what a careless guy i am..
EthanZ6174
The importing java.util.Date explicitly should give an compiler error
Fedearne
+5  A: 

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
you were right, I named my src "Date.java"
EthanZ6174
@EthanZ6174: Good for you! Never ever do that again...
Vijay Dev
@Vijay Dev absolutely ... what a shame to me
EthanZ6174
@EthanZ6174: You don't need to be ashamed. We all learn as we go.
Vijay Dev
@Vijay Dev thx, nice of you ;)
EthanZ6174
@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
@Vijay Dev awesome~~~
EthanZ6174
A: 
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
nope, i name the class "Date.java" and that is the problem..
EthanZ6174
+1  A: 

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

The toString() you se 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.

Fedearne