Results 1 to 6 of 6
  1. #1
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default instanceof Opertor problem

    does the instanceof operator only work with type String ??

    this works :
    Java Code:
    String x = "james" ; 
    boolean y = x instanceof String ;
    while this does not work :
    Java Code:
    int x = 15 ; 
    boolean y = x instanceof int ;
    what should i do ??
    thanks ! .

    Tony

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: instanceof Opertor problem

    does the instanceof operator only work with type String ??
    No, it works with objects, not primitives like int.

    what should i do ??
    Please explain what the problem is and what you are trying to do.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Re: instanceof Opertor problem

    I am trying to test the type of a variable then assign the result to a boolean variable , i thought instanceof operator should take care of .

    thanks !

    Tony

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: instanceof Opertor problem

    Well your thinking doesn't make much sense to me. If you already know what type a variable is, then why you have to check it? in your example, you tried to test if an integer (int) is actually int.. If you had a String and you want to test if it's actual value is an integer or boolen that's a different story.

    For example if you take an input from a user like
    Java Code:
    String inputText = input.nextLine()
    Where input is
    Java Code:
    Scanner input = new Scanner(System.in)
    Then, if you want to test if the inputText variable has an integer you have several ways to achieve that.

    1. Integer.parseInt(inputText ) <- inside a try catch statement
    2. Regular Expressions -> if(inputText.matches("\\d")
    3. pass the inputText inside a Scanner Object and call nextInt()

    To test if it's boolean or double it's quite same and you should try it just for the experience
    Last edited by ShadowWalker; 09-03-2016 at 09:20 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: instanceof Opertor problem

    I am trying to test the type of a variable
    There is No need to test the type of a primitive. If in doubt, ask the programmer that wrote the code.
    Last edited by Norm; 09-03-2016 at 10:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: instanceof Opertor problem

    I am trying to test the type of a variable then assign the result to a boolean variable , i thought instanceof operator should take care of
    The hint is in the name of the operator. Instance of. Primitives are not instances.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. instanceof question
    By tngo in forum New To Java
    Replies: 3
    Last Post: 02-18-2013, 12:23 AM
  2. Instanceof
    By KevinNguyen in forum New To Java
    Replies: 12
    Last Post: 11-08-2012, 05:55 PM
  3. instanceof
    By AedonetLIRA in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 12:34 AM
  4. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 10:35 PM
  5. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 07:23 PM

Posting Permissions

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