Characters and Booleans

A variable of type char is used to store a single character. A character can be a letter or a number or special character. A char uses 2 bytes of computer memory. The value of a char is always surrounded by single quotes. Here is an example with our “MyVariables” program.

public class MyVariables {
  public static void main(String[] args) {
    char myCoolVariable;
    myCoolVariable = 'A';
    System.out.println(myCoolVariable);
  }
}

Another interesting thing about a char variable is that you can assign an ASCII value to one using the corresponding integer values. Here is an example.

public class MyVariables {
  public static void main(String[] args) {
    char myCoolVariable;
    myCoolVariable = 65;
    System.out.println(myCoolVariable);
  }
}

The output of that code is “A”.

A variable of type boolean uses only one bit of computer memory and stores the values of either true or false.

public class MyVariables {
  public static void main(String[] args) {
    boolean myCoolVariable;
    myCoolVariable = true;
    System.out.println(myCoolVariable);
  }
}

Variables are very handy for storing values that you can use or modify. But sometimes you may want a variable whose value remains unchangeable. To do this add the keyword final in front of your variable type. Then once a value is given to your variable, that value is constant and can never change. Any attempt to do so, will cause the compiler to generate an error.

public class MyVariables {
  public static void main(String[] args) {
    final boolean myCoolVariable;
    myCoolVariable = true;
    System.out.println(myCoolVariable);
  }
}

In my examples, I have declared a variable and given it a name. Then on the next line I assigned my variable a value. It is considered good coding practice to assign a value to your variables soon after declaring them, because forgetting to initialize a variable with a value will produce an error. For ease, you can even assign a value to a variable on the same line as declaring it. Like so:

boolean myCoolVariable = true;

Let’s talk a moment about variable names.

If you thought you could name your variables anything you want, you are mostly right. But there are a few rules to consider.

First Rule: You can’t name your variables any word that is already reserved by the Java programming language. Words like byte, short, int, long, float, double, char, and boolean already have meaning in Java code. Therefore you can’t have an int named int. But because Java is case sensitive you can have an int named Int.

Next Rule: Variable names can only contain letters, numbers, an underscore (_), or a dollar sign ($). They may not contain other symbols or characters. And a variable name may not begin with a number.

Rule Number 3: I shouldn’t have to say this, but you can’t have more than one variable with the same name; even if they are of different types.

Also keep variable names short and meaningful. This is not a firm rule. It’s just some style advice. A variable named “myCoolVariable” is not a good name. Its kind of long, awkward to type, and says nothing of what the variable stands for, other than it being cool.

And while silly names like “byte me” and “double o7″ may produce giggles, they do nothing to enhance code readability.

Next Topic: Arithmetic and Assignment Operators

Comments

Popular Posts