I am writing a program that needs to know the type of constants. I am wondering if there is any way of getting this in java? Like for constant "2" type is int and for "2.0" type is double.

Thanks for your response but what I mean is that I need to use this type in my program not finding out the types myself.

I don't understand. If it's your program you will have entered the literal yourself, so you will know what type it is. The compiler interprets liters at compile time, and impicity converts as necessary. Also there's no way to refer to a literal without some kind of variable. in whch case it's the type of the variable that define what you get, eg
double x = 20;
the literal 20 is int, but x is double and the compiler will have done the conversion and discarded the original literal.