Title: Basics Variables
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Basics Variables

Variable names ... what are the rules for creating variable names

- contain letters, numbers, $ and _
- Identifiers can't start with a number
- Identifiers can't be one of the keywords such as (int, long, double, float, boolean, byte, char, if, while, for, etc.)
- Java is case sensitive. The following are all different variables( rate, Rate, RATE, etc.)
- Although the compiler doesn't care about names you pick, programmers usually use naming conventions for readability. So for example, variables that are made up of 2 words might be currentCar, or current_car, nextToken, or next_token
- You don't have to follow these rules, but most Java programs tend to follow the naming conventions below:
  1. Variables start with lower case. Remember that java is case sensitive so myName and MyName are 2 different variable names. Variable names that are 2 words tend be spelled out and look something like:


		int numberExpression,  lastName, firstName, personName;
		// OR
		int number_expression, last_name, first_name, person_name;
		

  2. Classes tend to start with a single upper case letter: String, Scanner, System, etc.
  3. Constants tend to use all upper case letters


	  static final int BRANCH_COUNT = 10;
	  static final double PI=3.141628;
	  static final int FEET_PER_MILE = 5280;
	  static final double CENTIMETERS_PER_INCH = 2.54;

Variable Declarations:


int 99year; // invalid
int year99; // valid
int hello#; // invalid
int $hello$; // valid
int MAX_VALUE; // valid


// Declarations with initializations:

int year99 = 99;

int num_of_states = 50;



// Another way to accomplish the same goal as above:

int year99 ; // Unitialized variable ... however Java will stuff variable with 0

year99 = 99;



int num_of_states;

num_of_states = 50;

Last Modified: July 17, 2013 2:19 AM