• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why This compile error?

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
sanjays samadder
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
manoranjini rao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
thiru kiran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be u can arrange the identifiers in this order.
public static void main(String a[])
{
}
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic