• 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

Find the data type of a variable

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

How do we find the data type of a variable? I mean,i think this is always possible to know the data type of a variable but what if i want to find it through a java code? Please guide me to the correct direction. Thank you.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know Java Reflection?(This may not be a good way but i don't know any other.)
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am talking of,let say a variable i....what is the data type of the variable i? Is it an integer or float or double and so on.....We can do it for an reference variable by using instanceof operator,to see if the reference variable is of a particular type. What for primitives? Any ideas?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using Java Reflection you can check primitive/non primitive data types.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using reflection is almost just as good as using Wrapper.xxxValue() or Wrapper.valueOf(), I mean when would you
be confonted with a scenario where you wouldn't have any idea what an object/primitive is. And if you didn't, the only thing
you could do is a series of if statements to either cast it to the type it is, and/or get the value right?

Interesting question,

Justin

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you want to cast a reference to an unknown type like that is a different question!
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As in C language we have the sizeof() operator which will tell us the bit depth of the data which determines the type of the variable. A question arose in the class that as we use sizeof() operator in C to find the bit depth of a variable in C what is it there for java? So reflection can be used as a solution,but valueOf() operation? Still we have to know the data type to store the value in, isn't it?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole idea of a high-level language is not to know the implementation details like that. So Java has dispensed with the sizeof() keyword. There is no simple way to find the memory size of a variable in Java.
And valueOf() is used to create an object to refer to:will print 123.

And I still cannot understand why anybody would want to put a reference of unknown type into a variable of unknown type. I have probably misunderstood the question; that would make for some very poor quality coding
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just some C geeks asked the question...not to be implemented in some code.....Thank you all for the help. Have a nice time.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudipto shekhar wrote:...not to be implemented in some code.....

Thank goodness for that

You need to be careful about C C++ Java and C#. Despite the similarities in syntax, they are three different languages. C++ is an extension of C, and Java and C# are completely distinct. There are lots of things in those languages which look very similar, but are actually completely different, eg "protected" "static".
Despite the differences, C# and Java set off following the same philosophy, and at least to the beginner are much more similar to each other than Java is to C/C++.




And yes I did mean three: Java = 1 C# = 2, C/C++ = 3.

And you're welcome to the help
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I am not a beginner in Java and also I know "JAVA is not C++ ".
Understanding the basic concept of JAVA with OO language is very important and that is where most of the in-experienced,non-Java programmers fail to understand. I think it so. Thanks a lot Mr. Ritchie and all. You have a nice time.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

I think the problem with confusing C++ and Java is worse for experienced people; most inexperienced people on his website have only written Java, so they haven't got anything to get confused with.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajat, welcome to the Ranch!

Thanks for the code. It looks a little bit strange to me, though. Why does class Datatype extend Exception, and why do you use exceptions like that in your code?

Exceptions should be used only for "exceptional" situations - when something unexpected happens while the application runs. Don't use exceptions for normal control flow, as you are doing in that code. Also, never swallow exceptions (lines 22 and 35) without showing anything to the user - because when something goes wrong, the user will never know that something went wrong.
 
Ranch Hand
Posts: 35
Hibernate jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I think this should help
 
Rajat Swain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Rajat, welcome to the Ranch!

Thanks for the code. It looks a little bit strange to me, though. Why does class Datatype extend Exception, and why do you use exceptions like that in your code?

Exceptions should be used only for "exceptional" situations - when something unexpected happens while the application runs. Don't use exceptions for normal control flow, as you are doing in that code. Also, never swallow exceptions (lines 22 and 35) without showing anything to the user - because when something goes wrong, the user will never know that something went wrong.



Thank you Jesper!

I am a pure beginner in java. Your suggestions will surely help me.

Thanks again!!
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic