Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Naming Objects with Strings?

 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic