Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > to use import java.lang.* or import java.lang.Math or none at all?

Reply
Thread Tools

to use import java.lang.* or import java.lang.Math or none at all?

 
 
JPractitioner
Guest
Posts: n/a
 
      02-20-2006
Hi guys,
i knew that by default all java.lang classes will be imported by the
compiler during compilation. but, to make it easier for the computer,
should i specify which class i really will be using? does this action
will boost the performance during compilation and runtime or not a
matter at all?

the answer to this post will definitely affect my programming style in
the future when i'm considering "to import or not to import"...
hmm,,,


thanks in advance.

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      02-20-2006
JPractitioner wrote:

> i knew that by default all java.lang classes will be imported by the
> compiler during compilation. but, to make it easier for the computer,
> should i specify which class i really will be using? does this action
> will boost the performance during compilation and runtime or not a
> matter at all?


Whether and how you import classes has exactly zero effect at runtime. Imports
(with or without wildcards) are only a kind of abbreviation provided by the
compiler to save us the effort of typing in fully-qualified type names every
time.

In theory explicit importing should make compilation faster -- by a very tiny
amount. I've never heard anyone claim that they've even managed to measure a
difference let alone found a case where it made a practical difference.

So the question comes down to how to write your code for maximum clarity. One
school of thought asserts that you should always import each class explicitly
(rather than by a wildcard). There's a fair consensus on that point -- with
which I disagree. For some reason, the advocates of that point of view seem to
exclude classes in the java.lang package. I agree entirely that it would be
counter-productive to be explicit about importing those classes -- I just
extend the logic to other well-known packages where they fail to do so

Still, that /is/ the consensus, so -- unless you prize "best practice" over
"common practice" (and there are arguments to be made for both positions) --
you won't go /too/ far wrong by falling in with the crowd, and your life may be
a little easier.

-- chris


 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-20-2006
JPractitioner wrote:
> i knew that by default all java.lang classes will be imported by the
> compiler during compilation. but, to make it easier for the computer,
> should i specify which class i really will be using?


No.

> does this action
> will boost the performance during compilation and runtime or not a
> matter at all?


No.

> the answer to this post will definitely affect my programming style in
> the future when i'm considering "to import or not to import"...


Are you that easy to convince? I have that lovely bridge on sale ...

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-20-2006
Chris Uppal wrote:
> Still, that /is/ the consensus, so -- unless you prize "best practice" over
> "common practice" (and there are arguments to be made for both positions) --
> you won't go /too/ far wrong by falling in with the crowd, and your life may be
> a little easier.


I only see it as "consensus" among people using certain IDE's who happen
to shout loudest and among some book authors. The later often of the
type where, by judging them by their books, they have never written any
program larger than Hello World.

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
steve_marjoribanks@hotmail.com
Guest
Posts: n/a
 
      02-20-2006
It's up to you really. Different people have different styles. As far
as I know, importing in groups won't slow it down or make it larger but
it does make it less obvious to anyone else who reads your code as to
exactly which classes the methods you've used have come from.

 
Reply With Quote
 
JPractitioner
Guest
Posts: n/a
 
      02-20-2006
Thanks all for the replies..
since there will be no affect on performance during runtime,
i'll be hapier using the wildcards..
but the idea of making it more obvious to other peoples(clarity
purposes) is good
hehe, thomas, actually it depends on how satisfying the answers i'm
getting..
sorry to not mentioning that
by the way, how much is the bridge?

Thanks again all.
Cheers.

 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      02-20-2006
Thomas Weidenfeller wrote:
> Chris Uppal wrote:
>
>> Still, that /is/ the consensus, so -- unless you prize "best practice"
>> over
>> "common practice" (and there are arguments to be made for both
>> positions) -- you won't go /too/ far wrong by falling in with the
>> crowd, and your life may be
>> a little easier.

>
>
> I only see it as "consensus" among people using certain IDE's who happen
> to shout loudest and among some book authors. The later often of the
> type where, by judging them by their books, they have never written any
> program larger than Hello World.


And among people who do not like to risk having their code fail to
compile when third parties add classes to their packages.

Ray

--
This signature intentionally left blank.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-20-2006
Thomas Weidenfeller wrote:

> > Still, that /is/ the consensus, so -- unless you prize "best practice"
> > over "common practice" (and there are arguments to be made for both
> > positions) -- you won't go /too/ far wrong by falling in with the
> > crowd, and your life may be a little easier.

>
> I only see it as "consensus" among people using certain IDE's who happen
> to shout loudest and among some book authors.


<grin>

There is something in what you say

-- chris


 
Reply With Quote
 
tom fredriksen
Guest
Posts: n/a
 
      02-20-2006
Thomas Weidenfeller wrote:
> JPractitioner wrote:
>> does this action
>> will boost the performance during compilation and runtime or not a
>> matter at all?

>
> No.


I know you are saying no. But to expand the question a bit.
Does this affect in any way how fast the jvm starts up? considering the
long list of classes loaded when the jvm start. I would logically think
that reducing the number of classes read in that list would at least
affect the startup time. Especially when using lots of third party
libraries, which might be quite large. Further on the question can be
expanded to adding large jars in the classpath, how does that affect
both startup time and other aspects.

Is the answer still no? I know it has to touch the file/class anyway to
be able determine if its something to use, but does that mean no extra
cost after that?.

/tom
 
Reply With Quote
 
JPractitioner
Guest
Posts: n/a
 
      02-20-2006
yeah, i am using third party jars as well, good one tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid - BorderStyle="None" and BorderWidth="None" doesn't work for Firefox David Freeman ASP .Net 8 02-16-2011 11:03 AM
Re: <var> is None vs. <var> == None Steve Holden Python 9 01-26-2009 09:10 PM
<var> is None vs. <var> == None Gerald Britton Python 7 01-24-2009 09:02 AM
testing for valid reference: obj vs. None!=obs vs. obj is not None alf Python 9 12-09-2006 05:00 AM
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Daniel Crespo Python 5 11-13-2005 12:52 PM



Advertisments