3 ms·
> Which is the base class for all classes? > java.lang.Object > This is true for custom classes. For primitive types such as int.class, void.class and Object
by dpark 15y ago
> Which is the base class for all classes?
> java.lang.Object
> This is true for custom classes. For primitive types such as int.class, void.class and Object itself have no super class.
This is misleading. Primitives (and void) are not classes at all, which is why they do not have a superclass. Interfaces also don't have superclasses for the same reason. These all do have a representation of the type 'java.lang.Class', and they all support the class literal (X.class) expression, but they are not classes. java.lang.Object is still the base class for all classes.
If I had an interviewee throw this stuff at me, I'd consider it a negative. If you're going to be pedantic, you'd better be correct.
As for this:
> Which one is faster in Java ?
> for(int i = 100000; i > 0; i--) {}
> for(int i = 1; i < 100001; i++) {}
> Answer: Which ever is run second with be fastest. The server JVM can detect and eliminate loops which don't do anything...
Will the compiler itself not detect and eliminate these?
Edit: Apparently no, the compiler won't optimize those away. I can't imagine why. Whether the JIT will optimize away the second loop isn't as clear as he indicates, though. Assuming they are in separate methods, optimizing the first away (after it runs) will have no effect on the second.
- peter_lawrey 15y agoA Class is a proxy for the underlying representation of the type/class. IMHO, int.class is just as much a Class as Integer.class. The compiler does next to no optimisations and the "client" JIT doesn't remove loops which don't do anything. Only the "server" JIT eliminates such loops, after it has compiled the method which can be triggered by loop which loops many times.
- dpark 15y ago> A Class is a proxy for the underlying representation of the type/class. IMHO, int.class is just as much a Class as Integer.class. There are two separate things here: Class and class. Capital-C "Class" is a proxy for a type. Small-c "class" is a kind of type in Java. When someone asks why 'int' does not have a super class, the answer is because 'int' is not a class. It does have a corresponding Class* (which might have been more appropriated named "Type"), though. (There's also the '.class' literal which yields a Class. Again, the naming could have been better.) * The Class returned by int.class actually does have a super class, because Class is a class. e.g.: int.class.getClass().getSuperclass() yields a Class representing Object.