-2

According to this answer, getClass().getName() should give me the solution. I'm not sure why this is not working.

Error:

/tmp/java_i8lW4s/HelloWorld.java:35: error: cannot find symbol
    System.out.print(z.class.getName());
                     ^
  symbol:   class z
  location: class HelloWorld
1 error

Code:

public class HelloWorld
{
  public static void main(String[] args)
  {
    int x, y;
    double z;

    x = 10;
    y = 5;

    z = 10 / 5;

    System.out.print(z.getClass().getName());
  }
}
Community
  • 1
  • 1
dmr07
  • 1,388
  • 2
  • 18
  • 37
  • 4
    `z` is not an object. It is a *primitive data type*. You **cannot** call methods on *primitive data types* – dumbPotato21 Mar 20 '17 at 22:05
  • 1
    You can't call `getClass()` (or any other method) on a primitive variable such as a `double`. Also, your code doesn't quite match the error that you've shown, so you're probably compiling a different version from what you've shown here. – Dawood ibn Kareem Mar 20 '17 at 22:06
  • Why would you want to do that? You know exactly what it is, i.e. a `double`. It cannot be anything else. – Andreas Mar 20 '17 at 22:11
  • 2
    The other answers address the issue, but like @Andreas asked, what is the use case of this? there are other, better ways of checking types of primitives that don't involve changing all of them to instances of classes. Also, you should start using an IDE like eclipse to do your development. It would have highlighted this error and told you what the issue was so you could figure it out without re-compiling. It would have also given you a more descriptive error. – Alex Mar 20 '17 at 22:13

2 Answers2

2

z is not an object it is a primitive type. Try to change it in Double.

For example: short, int, long, float and double are only numeric data primitive type.

Those are the equivalent of Short, Integer, Long, Float and Double.

So you should ask yourself, why have primitive type and object representation of same things. And what's the difference.

Basically they should be the same thing, for example you can add an Integer to an int transparently.

  Integer t = 2 ;
  int i = t + 1; // i now is 3

I also would suggest to have a look at Autoboxing and Autounboxing in java

freedev
  • 25,946
  • 8
  • 108
  • 125
  • Is there a standard way to check for primitive type? I tried `((Object)z).getClass().getName();` but when I did `double z; z = (int) 10/3;`, it still gave me `java.lang.Double` – dmr07 Mar 20 '17 at 22:13
  • 1
    The question is: "may I ask to a primitive type which kind of type is?", if this is your question, you don't need to do this, you know it at compile time. – freedev Mar 20 '17 at 22:16
1

If you define it as

int a = 0;

then it is a primitive

What you want is to be able to call on a Integer class is done by defining it in the Integer wrapper class (same goes for all other primitives that has a wrapper)

Integer a = 0;

See for instance javadoc for Integer, Double

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Mikenno
  • 294
  • 3
  • 9
  • @Daniel Reed, to the question below, a primitive does not store anything in memory but the value, thus you cannot acces what type it is. (need 50 rep.... to comment...) – Mikenno Mar 20 '17 at 22:23