• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic