Java Programming/Identifiers, literals and expressions

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Navigation Left Arrow.svg Statements Java Programming
Identifiers, literals and expressions
Types Navigation Right Arrow.svg


You have already looked at some simple assignment statements. It would be assumed henceforth that you know what variables, identifiers, literals and expressions are. Yet, we will explore more about these very components of the assignment statement.

Contents


[edit] Identifiers

Identifiers are the names we give to our variables. You can name your variable anything like aVariable, someVariable, age, someonesImportantData, etcetera. But notice something, none of the names we described here has a space within it. Hence, it is pretty obvious that spaces aren't allowed in variable names. To be be honest, there are a lot of other things that are not allowed in variable names. The only things you are allowed however are:

  • Characters A to Z and their lower-case counterparts a to z.
  • Numbers 0 to 9. However, numbers should not come at the beginning of a variable's name.
  • And finally, special characters that include only $ (dollar sign) and _ (underscore).

Which of the ones below are proper variable identifiers:

  • f_name
  • lastname
  • someones name
  • $SomeoneElsesName
  • 7days
  • TheAnswerIs42

I can tell you that 3 and 5 are not the right way to do things around here, the rest are proper identifiers. Well, not quite. They might be correct but they are not what you should be naming your variables for a few reasons as listed below:

  • The name of the variable should reflect the value within them.
  • The identifier should be named following the naming guidelines or conventions for doing so. We will explain that in a bit.
  • The identifier shouldn't be a nonsense name like fname, you should always name it properly: firstName is the best way of naming a variable.

[edit] Naming conventions for identifiers

When naming identifiers, you need to use the the following guidelines which ensure that your variables are named accurately. As we discussed earlier, we should always name our variables in a way that tells us what they hold. Consider this example:

Listing 1.1: Variables, variables, variables.

int a = 24;
int b = 365;
int c = a * b;

Do you know what this program does? Well, it multiplies two values. That much you guessed right. But, do you know what those values are? Exactly, you don't. Now consider this code:

Listing 1.2: Correction, correction, correction.

int age = 24;
int daysInYear = 365;
int ageInDays = age * daysInYear;

Now you can tell what's happening, can't you? However, before we continue, notice the case of the variables.

NOTE:
Case is how the letters are arranged in a word. If a word contains CAPITAL LETTERS, it is in UPPER CASE. If a word has small letters, it is in lower case.
Both cases in a word renders it as mIxEd CaSe.

The variables we studied so far had a mixed case. When there are two or more words making up the names of a variable, you need to use a special case called the camel-case. Just like the humps of a camel, your words need to stand out. Using this technique, the words first and name could be written as either firstName or FirstName.

The first intance, firstName is what we use as the names of variables. Remember though, firstName is not the same as FirstName because Java is case-sensitive. Case-sensitive basically implies that the case in which you wrote one word is the case you have to call that word in when using them later on. Anything other than that is not the same as you intended. You'll know more as you progress. You can hopefully tell now why the variables you were asked to identify weren't proper.

[edit] Literals (values)

Now that we know how variables should be named, let us look at the values of those variables. Simple values like numbers are called literals. This section shows you what literals are and how to use them. Consider the following code:

Listing 1.3: Literals, literally!

int age = 24;
long bankBalance = 20000005L;

By now, we've only seen how numbers work in assignment statements. Let's look at data types other than numbers. Characters are basically letters of the English alphabet. When writing a single character, we use single quotes to encapsulate them. Take a look at the code below:

Listing 1.4: Initializing character variables.

char c = 'a';

Why, you ask? Well, the explanation is simple. If written without quotes, the system would think it's a variable identifier. That's the very distinction you have to make when differentiating between variables and their literal values. Character data types are a bit weird. First, they can only hold a single character. What if you had to store a complete name within them, say John, would you write something like:

Listing 1.5: Different character variable assignments.

char firstChar = 'J';
char secondChar = 'o';
char thirdChar = 'h';
char fourthChar = 'n';

Now, that's pathetic. Thankfully, there's a data type that handles large number of characters, it's called a String. A string can be initialized as follows:

Listing 1.6: Initializing a String variable.

String name = "John";

Notice, the use of double quotation marks instead of single quotation marks. That's the only thing you need to worry about. Pretty simple, ain't it?