• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check a datatype of a variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am wondering is there a way to check for a variable data type ?



Thanks in advance.


 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has strong, static typing. If you declare a "float m" it *is* an float. No exception.

Do you think you may have different types because of the different literals in line 5 and line 6, i.e. "8" vs. "8f"?
 
Kok Lum Chan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hauke,

I would expect both in line 5 and 6 be float.

In foxpro language, there is a function called VARTYPE() to confirm such claim and wondering does Java has something similar.

Understand that "instanceof" can be used to test the class type but what about primative type ?

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has the distinction between "primitive" types and object types. Objects are of one or several types, namely of their class, all parent classes (one at a time, of course) and all implemented interfaces (and all interfaces implemented by parent classes). You can test with instanceof if a specific type is among these.

Primitive types behave completely different*. They are. Just think of a type as "how to interpret this pattern of bits directly". You can't inspect the type of the "bit pattern", it is defined by the type of the variable. You can't just interpret the pattern as something else, even if you want to. That is why Java is static and strongly typed.

You can, of course, convert from one type to another, e.g. a char as an int (casting), which is sometimes done automatically, sometimes needed to be done specifically.

So, in short, a float variable is of type float. No chance for anything else, so no need (and possibility) to check.

* Autoboxing hides some of the distinctions.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the better question is why would you need to? What situation do you have that requires you to test this sort of thing?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic