Monday 18 July 2011

Java: Variables (Naming)

Going to start updating Mo and Th just so I can have shorter posts each day. If they seem like too little info per week, or the posts are too long, I'll change to a MWF schedule. We'll see. Anyway...


Variables in Java are given names, allowing statements in programs to refer to them easily. An example of this is:

result = 2 + k;

This places the value attained from 2+k (k itself being another variable), and places it inside the result variable, which can, at this point, be used in another statement. There are few constraints in naming a variable, first of which is, a variable must begin with an upper or lower case letter. For simplicity, give each variable a unique name, though this isn't technically a rule.

As with all text in Java programming, variables are case sensitive, so be careful how you name them! A common way to name variables is to be in lower case, unless there is more than 1 word, such as "myResult", in which case, the second and further words have their first letter capitalized.

Another guideline is to make variables concise and simple to understand. Don't name a variable holding the width measurement of some object "a" or "bottle", just name it "width".


That's all for now, we'll see how these shorter updates work out.

No comments:

Post a Comment