Home static Java Programming [Archive] - Is there some body really good in java to tell me === how can i declare var
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Is there some body really good in java to tell me === how can i declare var
| This topic has 2 replies on 1 page. |
| | Posts:19 Registered: 8/17/03 | Is there some body really good in java to tell me === how can i declare var Jul 4, 2004 5:35 AM |
| | hi ,
First of all I am sorry for the harsh language that I used but I am really in urgent need of the problem given below and I thought may be by this way I can get responses sooner. Thanks in advance. ===================================
how can i declare variables dynamically? for example , in php i can do
$var_num = 1; ${'var'.$var_num} = 5;
so i get
$var1 = 5
how can i do that in java? ============================ | | Posts:316 Registered: 4/7/04 | Re: Is there some body really good in java to tell me === how can i declare Jul 4, 2004 6:14 AM (reply 1 of 2) |
| | This can't be done in Java but the functionality can be simulated. You can use the Map interface to acomplish the same thing: import java.util.*; class DynamicVarTest { public static void main(String args[]) { Map<String, Integer> dynamicVariables = new HashMap<String, Integer>(); int var_num = 1; dynamicVariables.put("var" + var_num, 5); System.out.println("var1 = " + dynamicVariables.get("var1")); // Output: // var1 = 5 }}
| | Posts:447 Registered: 3/8/01 | Re: Is there some body really good in java to tell me === how can i declare var Jul 4, 2004 6:16 AM (reply 2 of 2) |
| | You can't do that. PHP is a scripting language, meaning there's another program that reads each PHP statement and evaluates it whenever you ask for a PHP page. Java, on the other hand, compiles to something (almost like) assembly code, which consists of really simple instructions, and variables in Java are simply a way to refer to particular pieces of memory; the bytecode will basically refer directly to those address. The same thing applies in C++ and other programming languages.
However, there are alternatives for having similar to what you want. Here are some possibilities: A) If you want a certain number of variables with the same name, use an array. You can make an array using SomeClass[] myArray = new SomeClass[numberOfElements]. Then, access the i'th element (where i is from 0 to myArray.length-1) using myArray. B) If you really want to use arbitrary strings for your names, create a HashMap (similar to the associative arrays in PHP). Use HashMap hm = new HashMap(). Then add values using hm.put("yourName", yourValue). And get objects back out using SomeClass value = (SomeClass)(hm.get("yourName").
See the Java tutorials on arrays (http://java.sun.com/docs/books/tutorial/java/data/arrays.html) and Maps (http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html). | |
|
This topic has 2 replies on 1 page. |
|
|