Download presentation
Presentation is loading. Please wait.
Published byMarlene Hart Modified over 9 years ago
1
Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?
2
Copyright 1998-2010 Curt Hill Why Most programming languages have variables Variables store values They are used in computations Their values may determine how the program executes
3
Copyright 1998-2010 Curt Hill What is it? A variable represents one or more memory locations This is a collection of bits that may be interpreted in a variety of ways A value may only be stored in a computer if it can be digitized The name comes from variables in algebra
4
Copyright 1998-2010 Curt Hill Variable characteristics Each variable has three important characteristics: –Name –Type –Value –Other less important characteristics Knowing how to set each of these is important to the first part of this course
5
Copyright 1998-2010 Curt Hill Name A character string that identifies a variable or other item Java names start with a letter Followed by letters, digits, underscores and dollar signs No blanks, punctuation or other characters Case sensitive Any reference must exactly match the declaration and not be a reserved word
6
Copyright 1998-2010 Curt Hill Reserved words A word that has special meaning to Java May not be used as a name These include: –boolean, byte, char, double, float, int, void –false, true, main, new, null –class, private, protected, public, static, this –case, catch, do, else, for, if, switch, try, while –extends, implements, import, package, super –Among others There are also numerous predefined classes with names that you should avoid
7
Copyright 1998-2010 Curt Hill Naming Conventions Java has a convention for naming things Variables –Start with lower case letter –Each new word is capitalized Classes –Start with upper case letter –Each new word is capitalized Constants –All in upper case Just a convention but nice
8
Copyright 1998-2010 Curt Hill Types Two kinds of types: primitive and classes Most variables are objects –An instance of a class Primitives include the simple types of most programming languages Types determine two properties: –Legal values –Legal operators and operations
9
Aside People are smart and computers are dumb This is obvious when we speak about types The type is usually understood when people work in algebra –We do not care if the number is an integer, fraction or real – it is just a number Not so in Java – we must explicitly state the type Copyright 1998-2010 Curt Hill
10
Primitive Types boolean - true or false char - Unicode (16 bit) byte - -128 to +127, 1 byte short - -32768 to +32767, 2 bytes int - 32 bit long - 64 bit integer float - 32 bit IEEE floating point double - 64 bit IEEE floating point void - function result or parameter Copyright 1998-2010 Curt Hill
11
Constant Values for Numeric Types int: optional sign and digits –25, -90, 1452912 –No commas or decimal points double: integer or exponential floats: must be tagged with f
12
Copyright 1998-2010 Curt Hill Real constants May be used for float or double May be an integer May have one decimal point May have a trailing exponent Examples: –3.4 -3.4982 2.3E5 3.95E+5 -3E-4
13
Copyright 1998-2010 Curt Hill Non-numeric Constant Values char: numeric or letter in apostrophes –‘a’, ‘-’, 1245 byte: numeric or letter in apostrophes –‘x’, ‘*’, -127, +90 boolean: true or false
14
Copyright 1998-2010 Curt Hill Integer Operators Addition + Subtraction – Multiplication * Division / (quotient) Division % (remainder) Examples: –7 / 2 is 3 –7 % 2 is 1
15
Copyright 1998-2010 Curt Hill Real Operators Addition + Subtraction – Multiplication * Division / Examples: –7.0 / 2.0 is 3.5 –7.0 % 2.0 is 1.0
16
Copyright 1998-2010 Curt Hill Other Operators char and byte may do increment and decrement boolean may do And (&&), Or (||) or Not(!) –These will be discussed later
17
Copyright 1998-2010 Curt Hill Variable Declaration To connect a name to a type Form: [visibility] type name1, name2, … namen; A variable must be declared before it is used Visibility may be left off The declaration is only known within the enclosing set of braces
18
Copyright 1998-2010 Curt Hill Visibility Control over accesses Private - class only Public - every one Protected - class and derived classes Friendly - public for this file, private otherwise –No keyword, friendly if not any other This will be discussed later with objects
19
Copyright 1998-2010 Curt Hill Example declaration A declaration: int a, count; double average; Three variables –Two integers –One real
20
Copyright 1998-2010 Curt Hill Declarations Again A variable may be initialized when declared Follow name with = and a value Example: int a = 5, b, c=-3; double d = 3.4; The type and constant value must be compatible
21
Copyright 1998-2010 Curt Hill Value The value of a variable may be set in a variety of ways: –Initialization at declaration –A side effect operator –Input operation –Method call that changes the value Each of these are covered in a separate presentation
22
Copyright 1998-2010 Curt Hill Storage Types Storage types determine lifetime of variable Stack Heap Static and Constant Most variables have no prefix to determine storage types or visibility
23
Copyright 1998-2010 Curt Hill Stack Allocated at beginning of a method Deallocated at end of method Quick allocation and deallocation Only primitives and handles on stack –Handle is Java name for a pointer –This will be discussed again
24
Copyright 1998-2010 Curt Hill Heap Allocated by specific use of new keyword Deallocated when no longer used by garbage collector Requires a handle (pointer) All objects are on heap Variables in wrappers may also be on heap –Wrapper is an object that holds a primitive
25
Copyright 1998-2010 Curt Hill Static Allocated initially by system Survives lifetime of program Static may change in value Handle may be static but the object is on heap Static means there is only one
26
Copyright 1998-2010 Curt Hill Objects Declaration is almost the same as primitive Except what is declared is really a handle to the item It needs to be instantiated with the new keyword Sometimes done automatically, usually not Most will be considered later, strings now
27
Copyright 1998-2010 Curt Hill Strings A string is a sequence of characters Most often used to identify outputs or give information Every language needs to handle strings in order to interface with the people who use them Java has two string types: –String –StringBuffer
28
Copyright 1998-2010 Curt Hill Strings This is the native string Whenever a string is enclosed in quotes it is mapped into a string object If the same string is used twice in a class it will become one string with multiple handles Thus String type does not need the new operator –Unlike every other object
29
Copyright 1998-2010 Curt Hill String Declaration String x; // null handle String y = “Hello there”; String z = new String(“Hi there”); String n = z; Almost all predefined classes start with an uppercase letter
30
Copyright 1998-2010 Curt Hill Operators Almost every class has an assignment operator –Assigns one handle to another Assignment: x = y; y = “This item”;
31
Copyright 1998-2010 Curt Hill Other String operators Most objects do not have other operators –Operators are not overloadable in Java Concatenation (+) String y = “Hi “, z = “there”; x = y + z; –The string x now contains “Hi there”
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.