Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

get the variable type

variable type

  • Please log in to reply
6 replies to this topic

#1 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 19 July 2011 - 04:04 AM

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?....
  • 0
GNU/Linux Is the Best.

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 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

Posted 19 July 2011 - 04:21 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 July 2011 - 07:10 AM

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.
  • 0

#4 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 20 July 2011 - 05:47 AM

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

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 20 July 2011 - 07:03 AM

Well, like you did it, using instanceOf.
  • 0

#6 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 20 July 2011 - 07:10 AM

Does not work :)
   int i=5;	
if(i instanceof int){
System.out.println("thats not string");
}

  • 0
GNU/Linux Is the Best.

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 20 July 2011 - 07:19 AM

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
  • 0





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download