Variables and Literals

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

Often when we write programs, we need to store a piece of data, such as a number or a word. Variables allow us to store such data. This section will introduce variables as well as describe the proper way to name them. For information about variable types see Common Primitive Variables.

[See page 39 in the Gaddis text ]

Declaring Variables

First we must create the variable, which is also referred to as declaring a variable. Such declarations look like this:

Example 1: Declaring variables:

 int count;
double pass;
char name;
boolean status; 

Once we have the variable we want to put a value into it. This can either be done when the variable is declared, or later on in the program.

Example 2: Assignments:

 int x = 4;    //this is an assignment statement
int count;

count = 7;  //this is also an assignment statement 


Note: Each statement must end with a semicolon.

Naming Guidelines

In the Java programming language, a name can consist of a sequence of one or more characters. The name must begin with a letter or underscore ( _ ) and must be composed of letters, numbers, and underscores. Spaces are not allowed in variable names.

Generally we want our variables to describe the data it is holding. The names should be short and concise.

The basic rule of thumb is to use the "camel case" format, where the first letter is capitalized for each word used (after the first full word in the variable). An example of this type of format is 'camelCaseFormat'.

Following this format and choosing meaningful variable names will make your code much easier to read.

Example 3: A few ILLEGAL variable names

 13A    @google    int    (v) 

Example 4: A few LEGAL BUT POOR variable names

 A    theNumberThatTellsUsTheTotal    helloworld 

Example 5: A few GOOD variable names

 total   taxRate    helloWorld 

Upper case and lower case letters are considered to be different; so hellowWorld, helloworld, and HeLloWoRlD are unique names.

There are some names which are reserved for special uses in Java, and cannot be used by the programmer, such as class, public, static, if, else, while and catch.

Reserved Words

The following can not be used for variable names:

 break       byte        case        catch         char        class
const       continue    default     do            double      else
extends     false       final       finally       float       for
goto        if          implements  import        instanceof  int
interface   long        native      new           null        package
private     protected   public      return        short       static
strictfp    super       switch      synchronized  this        throw
throws      transient   true        try           void        volatile
while 

All of these words have a special meaning in Java, so they can't also be used for variable names. These are called reserved words.

Literals

A name for a constant value is called a literal. Literals can be used to assign values to variables.

Example 6: Examples of literals

 1    10    -1    a    C   True    False    Words 

There are also special literals using the escape character ( \ ):

  • \t - a tab
  • \n - a newline
  • \' - a single quote
  • \\ - a backslash
  • \" - a double quotation
Previous Page: Input using JOptionPane Next Page: Common Primitive Variables