The Coder's Handbook   

Variables

USING VARIABLES

What is a Variable?


You can store information in a program using a variable.  A variable has a name and a type.  Consider the following examples:


int testScore;

double percentOfVoters;

char isAlive;


The first word refers to the type of the variable, which tells you what it stores.  For example, int means that this variable holds an integer.  The name of the variable allows you to reference it.


We always name variables using a system called camelCase.  When using camelCase, you start out a variable’s name with a lowercase value and each subsequent word is capitalized.

When you make a variable, think of it as a box that stores values of a specific type.

Declaring Initializing and Assigning


In order to use a variable, you first need to declare it.  To do so, you simply state the type of the variable and then the name you’d like to use.


int testScore;


Often you will want to start a variable out with a set value.  This is called initializing the variable.


int testScore = 5;


After you’ve created a variable, you won’t need to keep referencing its type.  You can simply refer to it by its name.  You can assign a new value to a variable at any time by using the assignment operator, the equal sign.  


testScore = 7;         // this changes the value of test score to 7


Note that when you assign a variable a new value, the old one is lost.  When you use the assignment operator, the left hand side always takes on the value of the right hand side.   You can even take the value of one variable and assign it to another.


int scoreOne = 2;

int scoreTwo = 5;

scoreTwo = scoreOne;    // scoreTwo now has a value of 2



Good Style

public class Alpha

{

     public static void main(String[] args)     

     {

         System.out.println("Hello World!");

     }

}

Also Good Style

public class Beta  {

     public static void main(String[] args) {

          System.out.println("Hello World!");

     }

}

MEMORY AND TYPES

Memory


A variable is stored in your computer’s memory.  When we talk about memory in programming, we’re usually talking about random access memory, or RAM.  This is your computer’s “short-term memory.”  Don’t confuse this with your computer’s hard drive or a network drive, which can store a lot more data but is slow to access.


A bit is the smallest unit of data you can store; it is a 0 or a 1.


A byte is a collection of 8 bits.

Without this guy, you wouldn't be able to make variables.  Thank you, curly horned bro.

Primitive Types


There are two main categories of variables:  primitive types and objects.   We’re going to wait on discussing exactly what objects are.  For now, think of them as more complicated, customizable variables with special features.   There are exactly eight primitive types.


Name Description Size

byte Stores very small integers 8 bit

short Stores small integers 16 bit 

int Stores normal integers 32 bit 

long Stores big integers 64 bit 

float Stores less precise decimal values 32 bit

double Stores more precise decimal values 64 bit

boolean Stores a true or false value 1 bit*

char Stores a single character 16 bit


In this course, you’ll be using the following types most commonly:


double tacoPrice = 1.95;

int numTacos = 3;

char firstLetter = ‘t’;

boolean isHungry = true;


*A boolean actually takes up one byte of memory in Java, despite only representing 1 bit of memory.  This is because memory is addressed in 1-byte chunks. 

For now we start with the very basics - using primitive types.  Unga bunga.


Later on, we'll learn about objects and step into a glorious future. 


ASCII


ASCII stands for the American Standard for Information Interchange.   This is a list of 256 characters, each of which correspond to a numerical value.  You can find the full table here.  


Consider that a capital “A” is 65, and a capital “B” is 66.  This pattern continues through the alphabet. You can sometimes use patterns on the ASCII table to modify the values of letters, for example:


char myLetter = ‘A’;                 // Notice that chars use single quotes

myLetter = (char) (myLetter + 2);    // myLetter is now ‘C’


The last line in the example converts the ASCII back to a char.  We’ll talk more about this in the next section.

TYPE CONVERSION

This section uses some examples with math and assignment.  Don't stress about the syntax for now, we'll cover it in the next section of the handbook.

What is Casting?


You can convert one data type to another data type.  This process is called casting. 


When you cast from one type to another, sometimes you can lose data.  For example, when you convert from a double to an integer, it always “rounds down.”  This is called truncation - it cuts off all data after the decimal point.  


Keep in mind that when we cast, we're not changing the original variable itself.  It simply modifies the value that is being used on a given line of code.

Explicit Casting


The first way to cast a variable is through an explicit cast.  This means you're specifically saying, "I want you to convert this thing to a different type."  


int a = 2;

double b = 3.5;

int c = a + (int) b;


Walking through the assignment statement, we see that c is assigned the value 5.


2 + (int) (3.5)

2 + 3 3 is truncated to an integer

5

Sometimes you need to make a square peg fit in a round hole, but data is lost in the process.

Implicit Casting


The second way this happens an explicit cast and happens when we use a mixture of types.  This is called mixed-mode arithmetic.  A smaller data type will be converted to the larger data type temporarily for the math to work. 


int a = 9;

double b = 2.0;

double d = b / a;        


Walking through the assignment statement, we see that d is assigned the value 4.5.


9 / 2.0

9.0 / 2.0 9 is widened to a double

4.5

RESOURCES