instanceof

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    #1

    instanceof

    Can I have many instanceof code for one 'if' ? for example.....
    Code:
     if(checkMe instanceof AInstruction instanceof LocVariableInst)
    What I am trying to say is, these instanceof are refer to these classes:
    Code:
    public abstract class AInstruction extends Instruction &
    public abstract class LocVariableInst extends Instruction
    please advise me...thankssss
    Last edited by shana07; Apr 5 '07, 10:35 AM. Reason: put classes in tag
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    No you can't because the result of the instanceof operator is a boolean, i.e.
    an object either is (true) or isn't an instance of a certain class/interface.

    e.g. "true instanceof Object" doesn't make sense where "true" is the result
    of another "instanceof " sub-expression.

    Of course you could do this, e.g.:
    Code:
    if ((object instanceof A) || (object instanceof B)) ...
    kind regards,

    Jos

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by JosAH
      No you can't because the result of the instanceof operator is a boolean, i.e.
      an object either is (true) or isn't an instance of a certain class/interface.

      e.g. "true instanceof Object" doesn't make sense where "true" is the result
      of another "instanceof " sub-expression.

      Of course you could do this, e.g.:
      Code:
      if ((object instanceof A) || (object instanceof B)) ...
      kind regards,

      Jos
      I see. Actually my aim here is to process attributes of object - checkMe. Then I wanna the object is intanceof A and instanceof B. Then I should write
      Code:
      if ((object instanceof A) && (object instanceof B))
      isn't?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by shana07
        I see. Actually my aim here is to process attributes of object - checkMe. Then I wanna the object is intanceof A and instanceof B. Then I should write
        Code:
        if ((object instanceof A) && (object instanceof B))
        isn't?
        Yep, if that is what you want, that'd be the correct Java syntax then. If you're
        familiar with design patterns may I suggest a double dispatch (aka visitor)
        pattern instead?

        kind regards,

        Jos

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by JosAH
          Yep, if that is what you want, that'd be the correct Java syntax then. If you're
          familiar with design patterns may I suggest a double dispatch (aka visitor)
          pattern instead?

          kind regards,

          Jos
          ops sorry I afraid I don't know about it. Could you please give one sample code about double dispatch?
          Thank you.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by shana07
            ops sorry I afraid I don't know about it. Could you please give one sample code about double dispatch?
            Thank you.
            Google is your friend: visitor pattern

            kind regards,

            Jos

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Originally posted by JosAH
              Google is your friend: visitor pattern

              kind regards,

              Jos
              guys, I know this expr is wrong - I get illegal start of expr.
              Code:
              if((B instanceof A1) && (B instanceof A2) && if(B instanceof A3))
              Please guide me how make it valid...
              I need to write code something like this:
              Code:
              if((B instanceof A1) && (B instanceof A2) && if(B instanceof A3))            
              {    
                            if (C instanceof A4)
                            {   //do something here...........    }
              }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by shana07
                guys, I know this expr is wrong - I get illegal start of expr.
                Code:
                if((B instanceof A1) && (B instanceof A2) && if(B instanceof A3))
                Please guide me how make it valid...
                Simply remove that second 'if':
                Code:
                if((B instanceof A1) && (B instanceof A2) && (B instanceof A3))
                kind regards,

                Jos

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  Originally posted by JosAH
                  Simply remove that second 'if':
                  Code:
                  if((B instanceof A1) && (B instanceof A2) && (B instanceof A3))
                  kind regards,

                  Jos
                  friend, my mistake..I deleted that but don't you think that the correct syntax is::
                  Code:
                  if(B instanceof A1 && B instanceof A2 && B instanceof A3)
                  - the inner bracket.
                  I need to consult you about this still. Let say I need to get in words something like this (please ignore correct/incorrect syntax)
                  Code:
                  for (B instanceof A1 && B instanceof A2 && B instanceof A3)
                  {
                     if( B instanceof A4)
                     { 
                       //then only proceed here........}
                  }
                  Meaning the first for statement, I wanna make it such as FOR object B is intance of class A1, A2 and A3 (only these 3 instace classes),
                  then only (they might have more classes) B instance of A4 to do this and that......
                  Please teach me the correct sytanx....thank you so much.

                  Comment

                  • BSCode266
                    New Member
                    • Jan 2007
                    • 38

                    #10
                    I think you are now trying to use a for as an if which wont work. Correct syntax for a for should be:

                    Code:
                    for(int i; comparison; math stuff for i;)
                    { body}
                    If you want a for loop and still do the comparison with instanceof try something like this:

                    I also just filled in the for loop because i dont know what you use as a limit for it.
                    Code:
                    if((B instanceof A1) && (B instanceof A2) && (B instanceof A3))
                    { 
                      for(int i; i<10; i++)
                      {
                       if( B instanceof A4)
                       { 
                         body
                       }
                      }
                    }
                    BSCode266

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      Originally posted by BSCode266
                      I think you are now trying to use a for as an if which wont work. Correct syntax for a for should be:

                      Code:
                      for(int i; comparison; math stuff for i;)
                      { body}
                      If you want a for loop and still do the comparison with instanceof try something like this:

                      I also just filled in the for loop because i dont know what you use as a limit for it.
                      Code:
                      if((B instanceof A1) && (B instanceof A2) && (B instanceof A3))
                      { 
                        for(int i; i<10; i++)
                        {
                         if( B instanceof A4)
                         { 
                           body
                         }
                        }
                      }
                      BSCode266
                      Really appreciate your help. Let me give you example what is actually I require:
                      Object B is instance of 100 classes :
                      A1, A2, A3, ............... ...........,A10 0

                      Then I wanna my program to do comparison in these instance classes only:
                      such as:
                      Code:
                      something like start from or for B instanceof A1& A2 & A3
                      then
                      if (A instanceof A4)
                      {//do this...}
                      Sorry if this is confusing you........

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by shana07
                        Really appreciate your help. Let me give you example what is actually I require:
                        Object B is instance of 100 classes :
                        A1, A2, A3, ............... ...........,A10 0

                        Then I wanna my program to do comparison in these instance classes only:
                        such as:
                        Code:
                        something like start from or for B instanceof A1& A2 & A3
                        then
                        if (A instanceof A4)
                        {//do this...}
                        Sorry if this is confusing you........
                        Do you really have 100 classes named A1 to A100?

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by r035198x
                          Do you really have 100 classes named A1 to A100?
                          No matter their names, trying to find an instance of one of them using the
                          instanceof operator is not the way to go. @OP: care to elaborate a bit about
                          what exactly you want to accomplish?

                          kind regards,

                          Jos

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #14
                            Originally posted by JosAH
                            No matter their names, trying to find an instance of one of them using the
                            instanceof operator is not the way to go. @OP: care to elaborate a bit about
                            what exactly you want to accomplish?

                            kind regards,

                            Jos
                            Thank you. Actually I want to store data in an ArrayList. This program or I call a driver is reading /analysing another one program, so-called TestProgram. Then it stores TestProgram data in ArrayLists. I managed to store the required data for the whole big program let's say from parts A - E. But my aim is to start store data from part C to D only. That's why, in my driver program the object must be instanced of to C and also instanced of D.
                            So in words, start store & count data from C and particularly in D need to do something else for example :
                            Code:
                            start from part C
                            {
                            if (instructMe instanceof D) then
                            {
                              store and count ArrayLists size.

                            Comment

                            • shana07
                              Contributor
                              • Jan 2007
                              • 280

                              #15
                              Originally posted by shana07
                              Thank you. Actually I want to store data in an ArrayList. This program or I call a driver is reading /analysing another one program, so-called TestProgram. Then it stores TestProgram data in ArrayLists. I managed to store the required data for the whole big program let's say from parts A - E. But my aim is to start store data from part C to D only. That's why, in my driver program the object must be instanced of to C and also instanced of D.
                              So in words, start store & count data from C and particularly in D need to do something else for example :
                              Code:
                              start from part C
                              {
                              if (instructMe instanceof D) then
                              {
                                store and count ArrayLists size.
                              I have forgotten to mention that this instance is for bytecode level.
                              What I need is to chose only instructMe is instance of ClassC in bytecode level - meaning, only for ClassC.

                              Comment

                              Working...