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!!!

Object.getClass().getName();

Obviously that won't work on primitive types. But why do you want to do this to begin with?

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.

Edited 6 Years Ago by rcook: n/a

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?

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 BestJewSinceJC has mentioned you can do this:

Object.getClass().getName();

But he said that will not work with primitives. Actually with a small trick, it will:

public String [B]type[/B](Object obj) {
   return obj.getClass().getName();
}

Now try to call it like this and see what happens:

Integer intgr = new Integer(10);
int i = 5;

System.out.println(intgr + " is: "+[B]type[/B](intgr));
System.out.println(i + " is: "+[B]type[/B](i));
Comments
Autoboxing, but honestly your post could be more clear on the fundamentals the solution uses.
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.

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.

This is the second thread where I have seen 'balmark' post something related to the topic but not addressing the actual situation.

Go fuck yourselves, to save yourselves having to slate anything I post, I'll save ye the effort. I wont post here anymore.

I was hoping to help with the initial problem.. who needs a method to tell you value types if you know about overloading methods or instanceof etc.

Enjoy your little community, I hope it gets smaller and smaller so you can feel bigger and bigger.

/shrug

Bye.

Comments
Your reaction more than confirms my suspicion

who needs a method to tell you value types if you know about overloading methods or instanceof etc.

instanceof will not work on primitive types, but this will:

public void test(Object obj){
  System.out.println(obj.getClass());
}

Which is the same answer like the one that was already given.

This question has already been answered. Start a new discussion instead.