When I compile the following little program named Gary.java, I get the following error: Incompatible type for declaration. Can't convert java.lang.string to string. Here's my Gary.Java program generating the error: public class Gary { static public void main(String args[]) { String tmp = "boy"; } } Q1) What's causing this compile error? Q2) What class are the primitives in (if any) such as integer? Isn't everything including primitives a subclass of Object?
class testing { static public void main(String args[]) { String s="abs";
} } i compiled your code to be sure but it did not give any error int is not a subclass of Object .but Integer class is. it is a wrapper class which is used to convert the primitive int into an object. similarily all the other primitives have wrapper classes.
Yes sanjay's right.The code is not giving any error.If you want to convert any String value into an integer then you can use the parseInt() of Integer class.But are you sure you got the error for only this part of your program or is there some more code along with this?
Gary, What other files do you have in the same directory as Gary.java? This error looks to me like you have a class there with no package named of all things String.class How's my guess so far?
The reason primitives are called primitives is because they are NOT objects. There is no class associated with them. They were created as a compromise to gain efficiency. Java is ALMOST a pure OO language. The exceptions are: 1. The primitives are not objects 2. Strings can optionally be created referencing literals instead of creating an object using the new operator. 3. Arrays have some non-OO behaviors. Note that C++ is not a pure O language either. Visual Basic is even farther away from pure. There are some pure OO languages out there.
"JavaRanch, where the deer and the Certified play" - David O'Meara