FatCow Posted August 23, 2014 Posted August 23, 2014 Is it possible for Java to have two different variables named "number" and "Number?"
Endy0816 Posted August 23, 2014 Posted August 23, 2014 Yes. Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive. http://mathbits.com/MathBits/Java/DataBasics/Namingrules.htm 1
Curiatron Posted August 23, 2014 Posted August 23, 2014 You will be able to determine the answer by writing a small test program that compares the two variables. Psuedo-ish code: Main { int Number = 10 int number = 5 //if Java recognizes N and n as different variables then you will get no error, otherwise you may get an error here. //review output. if Number and number output is the same as what you initially declared then Java recognizes upper and lower case names as different vars. printf("Number: ", Number, " number ", number ) } A general tip - it's been helpful to me when learning a new language to answer these type of questions using similar small test programs. 2
pzkpfw Posted August 24, 2014 Posted August 24, 2014 (edited) ... but note it's a very bad idea, unless part of a clearly understood naming convention. For example, you might have a property named "Number" (e.g. name all properties with leading capital), and a method in the same class might have a locally scoped (meaning "visible" only inside that method) variable named "number". (This could still be a bad idea, depending on context). But you definitely wouldn't want a method that has both "Number" and "number" as locally scoped variables, or a class with both of those as properties - too easily mixed up. (Not a criticism of the preceeding example by Curiatron, which was only for illustration.) Edited August 24, 2014 by pzkpfw 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now