• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why This compile error?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be u can arrange the identifiers in this order.
public static void main(String a[])
{
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
a wee bit from the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic