Results 1 to 15 of 15

Thread: integer

  1. #1

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181

    integer

    How can I check if an Integer has never been initialised?
    for an object it works like

    if (object==null) {}

    how do I do this for an integer?
    Sanity is a full time job

    Puh das war harter Stoff!

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I don't think you'll ever see a time where a primitive datatype hasn't been initialized, mainly because the compiler will ***** at you. Generally, I set all numeric primitives to 0 when I declare them.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    no that's wrong. You do not have to initialize one if you declare it out of a function!

    I used this for my solution (which I don't like that's why I ask here) and declared a second empty one so I asked if they are equal and if so I know it hasn't been declared yet...
    Sanity is a full time job

    Puh das war harter Stoff!

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Failure to initialize a local variable will result in an error if the variable has not been initialized and you are in the process of querying it for a result.
    Code:
    // will produce a warning "variable might not have been initialized"
    class T{
        public static void main(String[] args){
           int i;
             System.out.println("The value of i is" + i); 
       }
     }
    If the member is declared static and is being accessed from a static context then the default value of the un-initialized member will be displayed. This is because static variables or "class members" are initialzed when the class is loaded by a hidden internal method. If one was to disassemble the byte codes in a java class file you would see the class initialization code in a method named <clinit>.
    Code:
     class T{
          static int i;
         public static void main(String[] args){
        System.out.println("The value of i is" + i); 
        }
     }
    The following code would run fine even though our integer variable has not been initialized. This is because we have
    already created an instance of the class X.
    Code:
    class T{
       public static void main(String[] args){
          new X().test();
       } 
     }
     class X{
        int i; 
         public void test(){
           System.out.println("The value of i is" + i); 
         }
     }

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    /\/\isanThr0p why don't you just test for the variables default value and if it matches just assume that the variable has yet to have been initialized.

  6. #6

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright. what I am doing now is I have a second no initialized variable and than I check if they are the same. That works.

    I was thinking about setting the unused var to 666666 or something so I know if its that value it's still to be used. But in my program userinput is required so in case the user would input 666666 it would not work correct anymore...

    and if you wanna know, what I am doing is a little task for university. I have to implement a Searchtree and have to program some functions for it.

    thanks guys...
    Sanity is a full time job

    Puh das war harter Stoff!

  7. #7

  8. #8

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    deutscher?
    Sanity is a full time job

    Puh das war harter Stoff!

  9. #9

  10. #10

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well that sounds like an internet translation...

    so what about this?
    Sanity is a full time job

    Puh das war harter Stoff!

  11. #11

  12. #12

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    so do you know some german or did you just use internet translation?
    Sanity is a full time job

    Puh das war harter Stoff!

  13. #13
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I know a little german. But unfortunately i don't get to use it that much since most of the people i interact with at my job are spanish. I hoping in the near future to get to go to Liechtenstein.

  14. #14

    Thread Starter
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    that's cool
    well btw I just found out that a non initialized integer is 0...
    Sanity is a full time job

    Puh das war harter Stoff!

  15. #15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width