• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Type of Primitive Variable.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic