Jump to content

get the variable type

- - - - -

  • Please log in to reply
6 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
hello, how are you?

I want to get a type from the variable - For example, whether it's string or integer.

I make something like this:
String s="5";		

		if(s instanceof java.lang.String){			

			System.out.println("thats string");	

} 

but this method does not work with Integer.

I cant write the code like this - there is syntax Error:

int i=5;

String s="5";		

		if(i instanceof java.lang.Integer){			

			System.out.println("thats string");	

} 


coud you help me? how to check, if the variable is string or int or double and so on?....
GNU/Linux Is the Best.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You could use Integer instead of int for your declaration of i. The problem is that primatives don't have the information you're looking for. Worse, an int is NOT an Integer. An Integer is a class, an int is a primative.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I can't think of any scenario where you got a variable of which you don't know the type, and it turns out to be of type "int " and not "Integer".
Unknown variable of "Integer", definitely possible. "int" .. really can't see how that's possible.

#4
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
and how to check whether the variable is string or integer? for example, in JavaScript, we Could use typeof(), or Number().
GNU/Linux Is the Best.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, like you did it, using instanceOf.

#6
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Does not work :)
   int i=5;	

	   if(i instanceof int){			

	   			System.out.println("thats not string");	

  }

GNU/Linux Is the Best.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, that's cause you said "integer", and not "int"
like wingedpanther said, int is a primitive class.

All classes in Java that do not start with a capital letter are primitive and thus "special".
It's a short list of types: int, short, float, double, byte, boolean, char.

Every other class will, or should have, a capital letter to begin with: String, Integer, ...
Every primitive type has a wrapper class which does start with a capital letter and is written fully : Integer, Float, Double, Byte, Boolean, Character;

Now what's so special about a class vs a primitive type is that a class ALWAYS extends the Object class. Also the classes you create yourselves.
What you're trying to do there with the int is, no offense, stupid. As when you declare something as "int", why check whether it's an int? :)

It is useful as all the normal classes can be upcasted and downcasted, and handled as another class while they are actually another class.


String str = "hi"; 

Integer number = new Integer(8);


Object o1 = str;

Object o2 = number;

So when you're now dealing with o1, and o2. All you know as programmer is that those are from the Object class, they could however really be another class.
That you can check using instanceOf

if(o1 instanceof String){

  //String

} else if (o1 instanceof Integer){

  //Integer

}


Now the thing is, an int, byte, double,.... can't be declared as somehting else

int i = 5;

Object o1 = i;

That code won't compile.

So you can't declare an int as anything else but an int. So when you got an int, you know it. There's no way you declare something differently and it turns out to be an int.
(It can be an Integer tho)

I hope it's somewhat more clear :p
Ask ahead.

Edit: interesting, well explained, upcasting downcasting tut: http://forum.codecal...owncasting.html




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users