• 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

Incompatible Types

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am pretty new to this wonderland of Java.Well,my questions is as follows:
Assume that i have defined a String variable:
String myString = "abcd";
Now my goal is to create an arraylist with the same name as that of the value assigned to myString.That is the goal is to achieve:
ArrayList abcd = new ArrayList();
However,when i perform the following i get an error:
ArrayList :myString = new ArrayList();
ERROR is -incompatible types
found : java.util.ArrayList
required: java.lang.String

Can you help me to resolve this issue?
Thanx,
Ritu
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You must be doing something else with 'abcd' to get this error. My test class doesn't give me any error like that.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyoti Pathak:
Hi,

However,when i perform the following i get an error:
ArrayList :myString = new ArrayList();
ERROR is -incompatible types
found : java.util.ArrayList
required: java.lang.String

Can you help me to resolve this issue?


Jyoti:
Is it safe to assume that you are coming to Java from Perl? What you are asking to do is something that is very much along the Perl vein.
In Java, variables are strongly typed. As far as I know, there is nothing in Java that is directly analogous to this feature of Perl. You cannot use the value of one variable as the variable name of another. What you can do is something like the following:

Note that there is nothing like:

(It's been quite a while since I've done Perl, and I never did it heavily to begin with; the actual syntax may be slightly different)
Hope that this helps.
 
Jyoti Pathak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel,
Thanks a lot for your help..i really appreciate it..well, even i guess i tried to mess perl and java..
Regards,
Jyoti.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bomn sai,
Your post was moved to a new topic.
Zombie resurrection; unrelated question
 
reply
    Bookmark Topic Watch Topic
  • New Topic