Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

howto initialise multiple integer variables to zero

Thread view: 
terry433iid@yahoo.com - 06 Mar 2006 09:51 GMT
say I have the following integer variables :-
       int is_client_system = 0;
       int client_total = 0;
       int is_server_system = 0;
       int server_total = 0;
       int is_other_system = 0;
       int other_total = 0;
       int total_hardware = 0;

Is there a neater way to initialise these to zero as this does not seem
very efficient coding ??
Thomas Weidenfeller - 06 Mar 2006 11:19 GMT
> say I have the following integer variables :-
>         int is_client_system = 0;
[quoted text clipped - 7 lines]
> Is there a neater way to initialise these to zero as this does not seem
> very efficient coding ??

If you have to worry about the efficiency of such a few innocent
statements, then something is very wrong in your application.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Brad Pitt - 06 Mar 2006 16:02 GMT
You have to declare them anyway so adding a "=0;" at the end shouldn't
be a big deal. You can use textpad and run a macro or any other macro
tool to do it.

If you are lazy then put them in a array and loop through it and
initialize them to zero. but why create unnecessary datastructures?

check out www.textpad.com and the macros...its free
Rajah - 06 Mar 2006 16:54 GMT
Terry,
You might make these members in another object. Java will implicitly
initialize booleans to false; bytes, shorts, and ints to 0; and floats
and doubles to 0.0.

In the following example, you have to instantiate a new InitVals object
(that is, InitVals iv = new Initvals()) and then refer to the members
by the object (for example, iv.client_total).

Don't worry too much about the efficiency of initialization. Someone
has to do, either you or Java at runtime. You might consider your
initialization to be akin to documentation, helping a future reader to
know what you're doing.

public class InitVals {
   int is_client_system;
   int client_total;
   int is_server_system;
   int server_total;
   int is_other_system;
   int other_total;
   int total_hardware;

    public static void main(String[] args) {
        InitVals iv = new InitVals();
        System.out.println("is_client_system: " + iv.is_client_system);
        iv.client_total++;
        System.out.println("incremented client_total: " + iv.client_total);
    }
}
Green - 06 Mar 2006 17:24 GMT
If they are member variables then Java will initialize them
automatically with 0.
Tony Burrows - 10 Mar 2006 15:32 GMT
On Mon, 06 Mar 2006 01:51:44 -0800, terry433iid wrote:

> say I have the following integer variables :-
>         int is_client_system = 0;
[quoted text clipped - 7 lines]
> Is there a neater way to initialise these to zero as this does not seem
> very efficient coding ??
There doesn't seem to be a lot of point, just to same a little effort.
You could use something like
is_client_system = (client_total = 0);
to set two, using the value returned from the inner assignment for the
outer one, but it all becomes more obscure.

Tony
Mishagam - 15 Mar 2006 17:26 GMT
> On Mon, 06 Mar 2006 01:51:44 -0800, terry433iid wrote:
>
[quoted text clipped - 17 lines]
>
> Tony
You don't need brackets here,
is_client_system = client_total = 0;
will work OK (if you already defined variables) (I tested on NetBeans).
I don't think anything can be shorter than this - one additional
character for variable. On the other side, if you have to define
variables, original example looks best for me.
I don't think any example from other languages that require defining
variables and don't set them to 0 authomatically is shorter.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.