Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incompatible Types

 
Jyoti Pathak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bomn sai,
Your post was moved to a new topic.
Zombie resurrection; unrelated question
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic