testing data type

hi everybody!!!!!!!
can anyone tell me how do we test the data type of variables in java, like if a variable is a string, the system prints out that this variable is string or not.
more especially how can we test this on primitive data type?

Never mind I found it. I usually come up with solution after I make posts that ask for more information.
Anyway, you need a way to find the type of a variable. As [I]BestJewSinceJC[/I] has mentioned you can do this:
[CODE]
Object.getClass().getName();
[/CODE]
But he said that will not work … Read More

I know of no such operator in Java -- there is one called "typeof" in other languages, but nothing equivalent that I know of for Java.

I think that's probably because Java is a strongly-typed language, and there is no need for a runtime operation to tell you the type of something. Once you declare a variable to be a primitive type, that's' it, it can't ever be anything else.

There is "instanceof" for objects, but your question specifically said primitives.

hi everybody!!!!!!!
can anyone tell me how do we test the data type of variables in java, like if a variable is a string, the system prints out that this variable is string or not.
more especially how can we test this on primitive data type?

waiting for your replies!!!

Can you post the exact description of your problem? What are trying to accomplish and what have you been asked to do?

public void test(Object obj){
if(obj instanceof String){
System.out.println("Obj is a String, I can cast it to a string and use its methods eg.");
System.out.println("The Strings length: "+ ((String)obj).length());
}
}

you can also just overload methods, so the correct one gets called eg.

public void test(String obj){
System.out.println("obj is a string");
}
public void test(Integer obj){
System.out.println("obj is an Integer");
}
public void test(Object obj){
System.out.println("obj is not a string or integer, it is a "+obj.getClass());
}
public void runtest(){
Integer in = new Integer(4);
String str = "Hi";
Double d = new Double(55);
test(in);
test(str);
test(d);
}

public void test(Object obj){
if(obj instanceof String){
System.out.println("Obj is a String, I can cast it to a string and use its methods eg.");
System.out.println("The Strings length: "+ ((String)obj).length());
}
}

you can also just overload methods, so the correct one gets called eg.

public void test(String obj){
System.out.println("obj is a string");
}
public void test(Integer obj){
System.out.println("obj is an Integer");
}
public void test(Object obj){
System.out.println("obj is not a string or integer, it is a "+obj.getClass());
}
public void runtest(){
Integer in = new Integer(4);
String str = "Hi";
Double d = new Double(55);
test(in);
test(str);
test(d);
}

That is not what avinash_545 has asked. He wanted to do that with primitive types (int, float). Not Integer, Double. And even though your code "accidentally" does that, it has already been answered.