Variables

Variables – Unit 1: Introduction To Java (Computer Science 30S)

If you don’t feel like reading this web page, here are two alternatives:

  • Read this presentation of the same content (slides 63 – 111)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. Java Variables
  2. Declaring Variables
  3. Java Data Types
  4. Java Naming Rules
  5. Using Java Variables
  6. Converting Between Data Types
  7. Java’s String Data Type
  8. Converting Strings
  9. Math Library Code

Java Variables

  • Remember that a variable is a memory location which stores a value
  • To use a variable in a program it must be defined by giving its type and identifier
  • The type of the variable indicates what kind of value it will store
  • In Java, some data types are built into the language, and some are defined by programmers
  • The name of the variable, or identifier, is how the value will be referred to in the program
  • Variables are used for two purposes in Java, to reference objects, and to store values in memory
  • The term variable implies that the value inside it may vary (change)
  • declaration statement must be used to tell the compiler the name of the variable and it’s data type
  • A variable whose data type is a class, makes a reference of that class type (know as an instance – more later)
  • Java defines a number of basic variable types
  • We also call these primitive, or built-in data types
  • Java has eight built in types: byte, short, intlongfloatdoublechar, and boolean

Declaring Variables

  • Declaring a variable in Java is a specific process to bring the variable into “existence” in your program, you must:
  1. Give the data type
  2. Then the variable name
  3. Then you should initialize your variable to a starting value (although this is actually optional)
  • For example…

Capture(14)

  • This declares a variable of type integer named number with an initial value of 0
  • We will now examine this more closely

Java Data Types

  1. Byte – byte
  • The byte type is a basically a simple number (a very small integer), ranging from:

Capture(1)(5)

  • Remember, no decimal places
  • 8 bits used to store the variables
  • For example…

Capture2

  1. Short – short
  • The short type is a basically a simple number (shorter integer), ranging from:

Capture(2)(4)

  • Remember, no decimal places
  • 16 bits used to store the variables
  • For example…

Capture2(1)

  1. Integer – int
  • The integer type is a basically a simple number, ranging from:

Capture(3)(4)

  • Remember, no decimal places
  • 32 bits used to store the variables
  • For example…

Capture2(2)

  1. Long – long
  • Basically a longer integer, ranging from:

Capture(4)(3)

  • Same properties as an integer
  • 64 bits used to store the variables
  • For example…

Capture2(3)

  1. Float – float
  • Floating point are a (limited precision) representation of real numbers on the computer
  • A float requires 32 bits
  • For example…

Capture(5)(3)

  1. Double – double
  • Double precision are also a representation of real numbers
  • This is the largest decimal representation of ‘real’ numbers
  • For example…

Capture2(4)

  1. Character – char
  • The char type hold one character, not a string of characters
  • The char type can hold any of the ASCII characters, and must be enclosed in single quotes 
  • For example…

Capture3

  1. Boolean – boolean
  • The Boolean values can only hold true and false values
  • Java uses the keyword boolean unlike Java’s parent language C++ (and C#), where Boolean variables are declared using the keyword bool
  • For example…

Capture4


Java Naming Rules

  • Java is a strongly typed language, meaning that we have to watch our syntax
  • For example…

Capture(6)(3)

  • A strongly typed language makes for:
  • More compiler errors
  • Fewer bugs (programmers must think)
  • Faster code when finished
  • Variables identifier names have to follow naming rules similar to other programming languages
  • Naming Variables (cont’d)
  • You can use letters (A-z, a-z), numbers (0-9), dollar signs ($), and Underscore ( _ )
  • You cannot begin a variable name with a number
  • Case matters (e.g. MYNAME is different from myName)
  • Dollar signs and underscore are not recommended to begin variable names
  • Remember that in good style you should use meaningful but reasonable variable names, start in lower case and capitalize only the first letter of each new word
  • For example, these are legal…

Capture(7)(2)

  • These are illegal…

Capture2(5)


Using Java Variables

  • Operators are used in the same way as other langauges +, -, *, / ) with precedence order
  • Integer division automatically truncates the decimal point

Capture3(1)

  • Constants can also be used in Java (and should be used)
  • They define a variable value that doesn’t change
  • However, in Java, the term final is used instead of const (which is used in Java’s parent language C++)
  • For example…

Capture4(1)

  • Increment & Decrement Operators
  • Java has two somewhat special operators
  • The increment ++ and decrement — operators, for example…

Capture5

  • Short Hand Operators: as well, the basic math operations can use another short-hand notation to assign values to variables in a quicker (less-typing) way, for example…

Capture6

  • These in action…

Capture7


Converting Between Data Types

  • In Java arithmetic operation should always be done on variables of the same type
  • For example adding two integers will return a integer result, multiplying two doubles will return a double result, etc.
  • When performing arithmetic on two different types the result is indeterminate
  • For example…

Capture(15)

  • To perform math on different variables types, conversion to one set type must be done
  • Data types can be converted in two ways, which is known a type casting (or just casting)
  • Method 2 works with string (seen later), but method 1 works with numerical data
  • Method 1:

Capture2(6)

  • Other examples…

Capture3(2)


Java’s String Data Type

  • The String data type is built into the Java language but it is different than other built-in data types in that it is not atomic or primitive, but a more advanced data type
  • Strings can be used to hold strings of data instead of a single character (like char)
  • Strings come with a variety of built-in functions to operate on them (seen later), but can generally be used like other data types
  • Declaration of string variables uses a Capital letter in the front of the word String because it references a class
  • Concatenation can be done on strings but is done with the + operator, and it does not do addition, it concatenates (joins together) the strings
  • For example…

Capture4(2)


Converting Strings

  • Converting string to numerical variable types can be done using the second method known as parsing
  • This is seen below…

Capture5(1)

  • Another example…

Capture6(1)

  • Strings can also be used in other ways, like converting them to charscomparing them (see if they are equal), getting their length,  and other things
  • Some of these built-in string methods are demonstrated next

Capture7(1)


Math Library Code

  • In Java basic arithmetic operations can be done with the standard operators (+,-,/,*), but more advanced math operations can be done as well using a built-in math code library
  • This is the Math library and it is built right into the Java language
  • It is demonstrated below:

Capture8

  • More examples…

Capture9


The NEXT thing to work on is the “Variables” example page