-4
Class X{
    Integer x=new Integer(5);
    Integer y;
    public static void main (String[] args) throws java.lang.Exception
    {
         X i = new X();
         String[] str={"x", "y"};
         System.out.println(Integer.class.isInstance(str[0]));
    }
}

It returns false as str[0] is a instance of Class String. Is there a way it can return true as str[0]=x and variable "x" is instance of Integer Class?

Thanks.

Asish
  • 409
  • 2
  • 4
  • 17
  • 2
    Why don't you use `Integer[] numbers = {x, y};` and convert to String whenever you want? – Maroun Aug 12 '15 at 07:17
  • 1
    Yes, it is. You should try to parse string value to Integer (like `Integer.parseInt(str[i])`) and then it should return true. But keep in mind if `str[i]` is not a string representing a number, an exception will be thrown when parsing. – peterremec Aug 12 '15 at 07:18
  • if it's only member variable (and not local) then you probably could use reflection to make such a check but I really don't see a reasonable use case for doing it – Mateusz Dymczyk Aug 12 '15 at 07:18
  • 1
    Your code is not clear. In String array your are keeping "x" and "y". That can never be integers. – Amit Bhati Aug 12 '15 at 07:21
  • I think OP tries to refer to the *fields* `x` and `y` through string identifiers `"x"` and `"y"`. – aioobe Aug 12 '15 at 07:24
  • I am sorry if the question was not clear. But i solved it using reflection : getField and getType and then comparing it isAssignableFrom(). – Asish Aug 12 '15 at 08:34

3 Answers3

1

When you're doing String[] str={"x", "y"}; you're not saving the variable x in the array, you're saving the String containing only the character "x". It's not because it's an array or anything that it doesn't work, if you want to get x as an Integer, you have to do this.x or i.x. In the String array, it's just two Strings, not the values you created in i that happens to have the same name.

EDIT: If you want to save x and y from i in an array, you have to do:

Integer[] ints= {i.x, y.x};
System.out.println(Integer.class.isInstance(ints[0]);

If you want to get these value as String:

Integer.parseInt(ints[]);
Asoub
  • 2,273
  • 1
  • 20
  • 33
0

Your class is equivalent to the below code:-

public  class X {
        public static void main (String[] args) throws java.lang.Exception
        {
             String[] str={"x", "y"};
             System.out.println(Integer.class.isInstance(Integer.parseInt(str[0])));
        }
    }

You are trying to do comparison between an Integer and a random string(x and y in this case) which cannot be parsed to an integer. You cannot parse string to an integer in this case. See below examples, it might make things clear for you:-

public  class X {
        public static void main (String[] args) throws java.lang.Exception
        {
             String[] str={"5", "7"};
              System.out.println(Integer.class.isInstance(str[0]));
        }
    }

Still returns false.

Change it to

 System.out.println(Integer.class.isInstance(Integer.parseInt(str[0])));

will return true.

Goyal Vicky
  • 1,249
  • 16
  • 16
0

Thanks for the help. I did it in this way.

class A
{
    public Integer x=new Integer(5);
    public Integer y=new Integer(7);
    public static void main (String[] args) throws java.lang.Exception
    {
        A i=new A();
        String[] s = {"allowedFileTypeMap","x","y"};
        Field field = i.getClass().getField(s[1]);
        if(field!=null){
            Object fieldType = field.getType();
            System.out.println(fieldType);
            if(field.getType().isAssignableFrom(Integer.class)){
                System.out.println("Working");
            }
        }       
    }
}
Asish
  • 409
  • 2
  • 4
  • 17