aspose file tools*
The moose likes Beginning Java and the fly likes Naming Objects with Strings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Naming Objects with Strings?" Watch "Naming Objects with Strings?" New topic
Author

Naming Objects with Strings?

jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Hello!
In order to make my programs run with less code I would like to know if I can name objects with a String..I did it when I was into Visual Basic...something like this:
String testName = "testName"
int testName = 14
Is this possible at all???
Thanks,
Jason
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281

String testName = "testName"
int testName = 14

Hi Jason,
I have not done a lot of basic, but I think that variables had more or less hidden extensions to denote their different types. I think Strings had a $ extension and so on.
The two variables you quote above are actually stored in two different places and would take up more room.
In java the name you see is the name the compiler sees. Two identical names with different types cannot exist within the same scope (range where you can use them in your class, method or block). The compiler will tell you what is allowable, though you need to be careful with names common to class variables and method variables.
I do remember a Fortran compiler from a long long time age that would allow you to put new values into literals, e.g., you could set 2 = 4 and from then on 2 + 2 = 8!!!
Ed


The nice thing about Standards is that there are so many to choose from!
jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
My example was terrible...after putting a bit more thought into it, this example would be better:
int someInt = 100;
for (int a = 0; a < someInt; a++)
{
String testString + someInt = "YUP";
}
So you would get 100 Strings such as testString1, testString2...testString100
Hope this makes it a bit more clear!
Thanks!
Jason
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281

int someInt = 100;
for (int a = 0; a < someInt; a++)
{
String testString + someInt = "YUP";
}

How about this:

I haven't compiled this but hopefully you would get 100 Strings such as
testString [0] set to YUP0,
testString [1] set to YUP1,
...
testString [99] set to YUP99.
Welcome to the world of arrays!!! This makes it much easier to perform operations on all elements of testString!
Alex Collins
Greenhorn

Joined: Mar 29, 2004
Posts: 6
There are two ways to create a String object,
remember, String is an object and not a primitive data type.
1.)
String stringName = new String();
stringName = "Hello";
**OR**
2.)
String stringName = "Hello";
To do a loop like you posted earlier you would do:
StringBuffer stringName = new StringBuffer("Hello");
for (int i = 0;i < 100;i++){
stringName.append(i);
System.out.println(stringName);
}
Or simply use the string array example posted earlier (both do the same)
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by jason candelora:
My example was terrible...after putting a bit more thought into it, this example would be better:
int someInt = 100;
for (int a = 0; a < someInt; a++)
{
String testString + someInt = "YUP";
}
So you would get 100 Strings such as testString1, testString2...testString100
Hope this makes it a bit more clear!
Thanks!
Jason

Just to be sure you get a clear answer. No.
But as another has posted you can certainly use an array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Naming Objects with Strings?