File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find the data type of a variable

 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • 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: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Java Reflection you can check primitive/non primitive data types.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rajat Swain
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rupesh Mhatre
Ranch Hand
Posts: 35
Eclipse IDE Hibernate jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I think this should help
 
Rajat Swain
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic