aspose file tools*
The moose likes Beginning Java and the fly likes Type of Primitive Variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type of Primitive Variable." Watch "Type of Primitive Variable." New topic
Author

Type of Primitive Variable.

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Folks,

Is there any method to get the type of the variable (primitive)? Something like this:

If i is an "int" type variable , is there a method to print the "type" of the variable "int":




Thanks.

Suresh.
Juva Yuva
Greenhorn

Joined: Dec 18, 2008
Posts: 17
Originally posted by Suresh Rajadurai:
Hi Folks,

Is there any method to get the type of the variable (primitive)? Something like this:


I m afraid , we cannot call any methods on the primitive type ( as it does not have behaviours) and hence the answer should be NO.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

You can do it by a combination of autoboxing and reflection, but why do you think you need this and how do you plan to use it?


luck, db
There are no new questions, but there may be new answers.
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Even using autoboxing features, i believe we would have to write custom methods to check the data type (of the object), the parsing methods should be useful here.


When the compiler's not happy, ain't nobody happy.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

variable types are really only of interest to the programmer. if you are at a spot where you can write code using this variable, why don't you just look in the source code to find out what it is?

A user shouldn't care if i store the value of 8 in an int, a float, a short or even a string - it should be transparent to them.

So, the broader question is "Why do you want to do this?" If it's an academic exercise, that's one thing. But if you think you have some compelling reason to do this in your production code, I'd suggest you think again.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Originally posted by Darryl Burke:
You can do it by a combination of autoboxing and reflection, but why do you think you need this and how do you plan to use it?

Autoboxing would turn the int into an Integer, and would lead to Integer.class, not int.class

Given an Integer object, it is impossible to determine whether it was resolved using autoboxing or calling Integer.valueOf manually. You could filter out new Integer calls by using reflection to check for reference equality with the cache inside Integer, but that only works for values between -128 and 127.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi,

First of all I thank all of you for the valuable answers that you all given. I really appreciate it. thanks a lot.

The reason I needed this infor is, to know the type of "result" we get from the following 2 methods:
1. Math.floor(10.1)
2. Math.round(10.1)

The (1) gives 10.0
The (2) gives 10

Why (2) does not give "10.0" ?
Therefore I assume the (1) gives float and (2) gives integer.

For this purpose I wanted to know the "primitive type" of result the 2 methods produce.

There is a question in one of the mock exams:

Which of the following will result in an output of 10 (not 10.0) ?

(1)System.out.println(Math.round(10.1));
(2)System.out.println(Math.floor(10.1));
(3)System.out.println(Math.abs(10.1));
(4)System.out.println(Math.min(10.1));


I got confused in this question, that is why I posted this question.


Regards

Suresh.
subhajit paul
Greenhorn

Joined: Dec 17, 2008
Posts: 17
suresh,
The methods you are calling are well-documented methods of java.lang.Math class. So the return type of the methods can be found out reading the documentation. Do you really need to find out the return types at runtime?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

Go to the java api. Look for the "Math" class, and you'll find this page. It clearly states that the floor method returns a double, and the round method returns a long.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Originally posted by Rob Prime:

Autoboxing would turn the int into an Integer, and would lead to Integer.class, not int.class


Hence the use of reflection. All the primitive wrapper classes have a final static field TYPE.


Given an Integer object, it is impossible to determine whether it was resolved using autoboxing or calling Integer.valueOf manually. You could filter out new Integer calls by using reflection to check for reference equality with the cache inside Integer, but that only works for values between -128 and 127.

Agreed, but the OP specifically mentioned that the variable would be a primitive.

As far as the detailed requirement posted much later is concerned, this wouldn't be a solution at all. That's why I had asked
but why do you think you need this and how do you plan to use it?

[ December 30, 2008: Message edited by: Darryl Burke ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type of Primitive Variable.
 
Similar Threads
primitives, literals
error while sorting using comaparator
dereferenced mean
using toString()
how can i determine the type of a variable?