Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between import java.util.*; and import java.util.Date; ?

Tags:

java

import

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

like image 910
EthanZ6174 Avatar asked Oct 30 '09 12:10

EthanZ6174


People also ask

What is Java Util date in Java?

The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).

What is the difference between import Java util * and import Java io *?

so import java. util. *; imports all of those classes. java.io contains things like FileReader, InputStream, OutputStream etc.

When should I use import Java Util?

Java util package contains collection framework, collection classes, classes related to date and time, event model, internationalization, and miscellaneous utility classes. On importing this package, you can access all these classes and methods.

What is difference between Java and Java Util?

reflect Provides classes and interfaces for obtaining reflective information about classes and objects. java. util Provides the collections framework, formatted printing and scanning, array manipulation utilities, event model, date and time facilities, internationalization, and miscellaneous utility classes.


1 Answers

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.

like image 113
Fedearne Avatar answered Oct 17 '22 02:10

Fedearne