This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Find the data type of a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find the data type of a variable" Watch "Find the data type of a variable" New topic
Author

Find the data type of a variable

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

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.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Do you know Java Reflection?(This may not be a good way but i don't know any other.)
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

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

Joined: Aug 16, 2007
Posts: 1374
Using Java Reflection you can check primitive/non primitive data types.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Whether you want to cast a reference to an unknown type like that is a different question!
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

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

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: Apr 02, 2008
Posts: 818

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

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: Apr 02, 2008
Posts: 818

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

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: May 05, 2011
Posts: 2
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rupesh Mhatre
Ranch Hand

Joined: Apr 29, 2011
Posts: 35




I think this should help
Rajat Swain
Greenhorn

Joined: May 05, 2011
Posts: 2
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!!
 
 
subject: Find the data type of a variable
 
Similar Threads
query returning null
Display text from MySQL Text Field
Increment a character...
Expert Level quesiton from J@Whiz
Need Programming Help With the "enum" Type of Variable