Variable

From StudyWiki

Jump to: navigation, search


Contents

General Concepts

Variable
A data value that is editable when the program executes.


  • Variables have 3 properties:
    • Memory location to store variable,
    • type of data stored in memory location,
    • and name used to refer to memory location.


Programming Languages

Java

Variable Declaration Syntax
<data type> <variable> ;


<data type>
Any of Java's primitive data types.
<variable>
a valid identifier, or a series of valid identifiers separated by commas. E.g. variable1, variable2, variable3 ;
Variables can be declared and initialised at the same time:
E.g. int variable1 = 10 ;


Naming Conventions
  • Names can only contain letters, digits, underscores, or dollar signs.
  • Names are case sensitive.
  • First letter must be a lower case character.
  • Where names/identifiers have multiple words, the first letter of all words after the first will be upper case (don't use underscores for spaces).
  • array variables use the plural names.


VBA

Variable Declaration Syntax
Dim <variable> As <data type>
<variable>
a variable name following the naming conventions for VBA variables.
<data type>
one of VBAs primitive data types.


Naming Conventions
  • must begin with a letter (a-z or A-Z) or an underscore
  • cannot contain a period (.) or a special character
  • must not contain a space
  • must not exceed 255 characters. You should limit the name of a variable to 30 characters
  • must be unique in the same scope
  • name must be preceded by 3 letter prefix according to type


Prefixes for variable names according to type
Data Type Prefix
Boolean bln
Byte byt
Date/Time dtm
Double dbl
Error err
Integer int
Long lng
Object obj
Single sng
String str
Currency cur
Variant var


Option Explicit

  • Variables don't need to be declared before use, unless Option Explicit is specified at the top of the source code.