Variables

Dr Andy Evans

[Fullscreen]

Code

 

  • The language is broadly divided into 'variables' and 'statements'.
  • Variables are places in which you store stuff you want to use...
    • e.g., words, numbers, data.
    • They are usually represented by names.
  • Statements are what you want to do...
    • e.g., print a word, add two numbers.

Variables continued

  • Let's imagine we need to use an x coordinate a number of times in a program.
  • We could 'hardwire' or 'hardcode' values in:
    System.out.println("x value is 100.0");
    
    
  • Say we use this value of x a thousand times in the program. What if we want to change the x location?
  • What we need is to attach the value to a name, and use the name instead - if we want to change the value, we just change the value the name points at.
  • This is what a variable is for.

Variable types

  • The computer usually needs to know what kind of thing they are before they can be used, so they can be stored efficiently.
  • There are several simple types (primitives), including:

     

    int : an whole number between ~+/- 2,000,000,000
    double : a decimal number between +/- 10+/-38
    boolean : either true or false
    char : a single character

Defining variables

  • To tell the computer what type of variable you are using you must declare it, usually before you use it. The syntax for doing this is:
    	type variableName;
    
    
    For example:
    	
    double x;
    
    
  • This variable declaration tells the computer to make a name-tag in memory in preparation for an double and call it 'x'.
  • Note that variables should start with a lowercase letter and describe the value inside them.

Assigning a value

  • It's then usual to 'assign' a value to the variable. The computer makes an double-shaped space in memory containing the value and attaches the name-tag to it, e.g.,
    	
    x = 100.0;
    
    
  • You can do both at once, e.g.,
    	
    double x = 100.0;
    
    
    Infact, this is quite usual during initialisation (the first time you use a variable). After that, you don't do the label creation bit again.

Using variables

You can then use the variables:
  • e.g., make another variable and use it to store some answer involving the variable:
    	
    double nearX = x + 1.0;
    
    
  • e.g. print it out:
    	
    System.out.println("x = " + x);
    
    
    which prints x = 100.0; note that "+" joins two things for printing if one is text, and the difference between "x = " which is in quotes, so is text, and x, which isn't, and is therefore a variable, the value of which is printed.

Reassignment

  • You could just as easily reassign x:
    	
    x = 200.0;
    
    
    Note that you don't need to create a new label, you just give a new value to the old one.
  • Note that it is also common to do this:
    	
    x = x + 1.0;
    
    
    The right side is assessed first, and the answer (201.0) replaces what is currently in x (200.0).

Operators

Operators for manipulating variables:
+: add
-: subtract
*: multiple
/: divide
%: modulus
++: add one

 

  • Modulus gives the remainder of a division, so 5 % 2 = 1
  • x++ is the same as x = x + 1

Operator precedence

  • Operators have a definite order in which things are done. For example,
    	
    i = 6 + 6 / 3;
    
    
    will give i as 8, not 4.
  • Always best to put equations in parentheses to make it clear what you're after. In this case,
    	
    i = (6 + 6) / 3;
    
    

Casting

  • Casting is the conversion of one type of variable to another.
  • Casting can be done implicitly with some types...
    	
    int m = 3;
    double i = m;
    
    
    gives i as 3.0.
  • Implicit casting is usually possible with primitive types where there is no loss of precision.

Explicit casting

  • Where useful information might possibly be lost, explicit casting is needed.
  • Syntax is...
    	
    (target type) variable;
    
    
    for example,
    	
    int numberOfPokemon = 150;
    byte number;
    
    number = (byte)numberOfPokemon;
    
    

Java's Assumptions

  • Java assumes any number with a decimal point is a double. If you want to make it a float, you have to be explicit:
    	
    float f = (float) 2.5;
    float f = 2.5f;
    
    

Java's Assumptions

  • Equally it assumes numbers without decimals are ints. This causes problems in maths, as rounding to ints will occur if an equation contains them:
    	
    i = 11 / 5;
    
    
    gives i = 2 remainder nothing, even if i is a double.
  • To stop this, always give figures with decimal points, e.g.
    	
    double d = 3.0 / 2.0;   
    
    
    not
    	
    double d = 3 / 2;  
    
    

You need to remember this

Review

  • The take home message is:
    	
    int x; 
    
    
    declares/creates an integer label, called x.
    	
    x = 100;
    
    
    assigns it the value 100.
  • You can do both at once, thus:
    	
    int x = 100;
    
    
    But from then on, you just use the label, thus:
    	
    x = 200;