instanceofoperator?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saumyaranjan
    New Member
    • Feb 2008
    • 5

    instanceofoperator?

    Hi 2 every1,
    Wht is the purpose of insanceof operator???plz gv a code????
  • hollywood115
    New Member
    • Feb 2008
    • 16

    #2
    Originally posted by saumyaranjan
    Hi 2 every1,
    Wht is the purpose of insanceof operator???plz gv a code????
    Hey Saumyaranjan.. its quite simple actually.

    say you have a method:

    public void getObj(Object obj)
    {
    ....
    }

    instanceOf is useful if u have to do different things in that method depending on whether you get an integer input, a string input, or others. so u could say

    Code:
    public void getObj(Object obj)
    {
       if (obj instanceof String)
       {
             System.out.println("You have passed in a String");
       }
       else if (obj instanceof int)
       {
             System.out.println("You have passed in an integer");
       }
    }
    its also useful if u just need to check what object the variable is in any other case as well.
    hope that helped :)

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Originally posted by hollywood115
      Hey Saumyaranjan.. its quite simple actually.

      say you have a method:

      public void getObj(Object obj)
      {
      ....
      }

      instanceOf is useful if u have to do different things in that method depending on whether you get an integer input, a string input, or others. so u could say

      Code:
      public void getObj(Object obj)
      {
         if (obj instanceof String)
         {
               System.out.println("You have passed in a String");
         }
         else if (obj instanceof int)
         {
               System.out.println("You have passed in an integer");
         }
      }
      its also useful if u just need to check what object the variable is in any other case as well.
      hope that helped :)
      Since int is a native type and not an Object, this probably won't work. But you could use:

      [CODE=java]else if (obj instanceof Integer)[/CODE]

      to check if the incoming object was an Integer object.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        @OP: you asked the following completely unrelated questions the last couple of days:

        - Frame work?
        - web mathod???
        - instanceofopera tor?
        - Marker Interface??????
        - Super n This???????
        - Overriding?
        - What is a 'Doa'?
        - Shoping card application question
        - Uploading of video files through Servlets
        - aceess of private members?

        Why did you ask all those questions? Most of them were answered but you
        didn't reply to the kind souls who answered you anymore. Please stop this.

        kind regards,

        Jos (mod)

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by JosAH
          @OP: you asked the following completely unrelated questions the last couple of days:

          - Frame work?
          - web mathod???
          - instanceofopera tor?
          - Marker Interface??????
          - Super n This???????
          - Overriding?
          - What is a 'Doa'?
          - Shoping card application question
          - Uploading of video files through Servlets
          - aceess of private members?

          Why did you ask all those questions? Most of them were answered but you
          didn't reply to the kind souls who answered you anymore. Please stop this.

          kind regards,

          Jos (mod)
          I've deleted most of them. They fall under the crappy post rule. Please stop posting crappy posts. It will only serve to get your account banned.

          Comment

          Working...