• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Using String as a variable name !

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoted from one of the question from JQPlus:
public class SubstringTest
{
public static void main(String args[])
{
String String = "string isa string";
System.out.println(String.substring(3, 6));
}
}
Using String as a variable name is OK while String is also a class name. I am confused!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't allowed to use any of the reserved words as identifiers. You can use most any other word.
After compilation, the bytecode doesn't remember what you originally named your identifiers - the compiler makes up its own names (so to speak).
By using common class names as identifiers, you're more likely to confuse yourself than the compiler.
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above code works fine . The variable "String" is good as any other varibale.
I slightly modified the above code to below.
public class SubStringTest
{
public static void main(String args[])
{
String String = "string isa string";
String s1 = String;
System.out.println(String.equals(s1)); //true
System.out.println(String.substring(3, 6)); //ing
System.out.println(s1); //string isa string
}
}

Here, its allowing us to do anything , as we do with any other String variable... no obnormal behavior.
But,
I tried the same with int.
///////////////////
int int x=100;
System.out.println(int);
The above code gives me all kinds of compilation errors.
So, this opppeses your explanation. We are not supposed to use the keywords/reserved words as identifiers.
I guess, there must be some other reason for the success of the line.
//String String = "string isa string";
Can you please look into it??
[ June 15, 2002: Message edited by: Murthy Ram ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We are not supposed to use the keywords/reserved words as identifiers.


String is the name of a type, not a keyword.
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String is the name of a type, not a keyword


oops!! sorry for the typo..
What I meant was, we are not allowed to use String, int, float as identifiers.
But, how the above the code works with String , not with int, or float,or char..
thanks
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murthy,
Reason : int is a keyword, String not. Therefore you can use String,Float,Byte as a variable name, but using char,float,byte as a variable name will give an error.
Jamal Hasanov
www.j-think.com
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its pretty clear to me now..
thanks Jamal!!
murthy
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Murphy,
I don't know why Sun allows such a mess.
If we define a class and one of its method is
public String subString(int a, int b)
and we use String as a variable name of its object,when we call String.subString(4,10).
Then we can predict what could happen.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan,
It would be very difficult for the compiler if every class name can't be a identifier. Take a look at the API Specs and you'll see why.
In your scenario, I think Java will generate a compiler error because it cannot determine which String class to use. You should use the fully qualified name for the class you wanna use (ex. java.lang.String).
[ June 16, 2002: Message edited by: Paul Villangca ]
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic