copyright 1998-2010 curt hill variables what are they? why do we need them?
TRANSCRIPT
![Page 1: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/1.jpg)
Copyright 1998-2010 Curt Hill
Variables
What are they?Why do we need them?
![Page 2: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/2.jpg)
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
![Page 3: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/3.jpg)
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
![Page 4: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/4.jpg)
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
![Page 5: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/5.jpg)
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
![Page 6: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/6.jpg)
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
![Page 7: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/7.jpg)
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
![Page 8: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/8.jpg)
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
![Page 9: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/9.jpg)
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
![Page 10: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/10.jpg)
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
![Page 11: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/11.jpg)
Copyright 1998-2010 Curt Hill
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
![Page 12: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/12.jpg)
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
![Page 13: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/13.jpg)
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
![Page 14: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/14.jpg)
Copyright 1998-2010 Curt Hill
Integer Operators• Addition +• Subtraction – • Multiplication *• Division / (quotient)• Division % (remainder)• Examples:
– 7 / 2 is 3– 7 % 2 is 1
![Page 15: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/15.jpg)
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
![Page 16: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/16.jpg)
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
![Page 17: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/17.jpg)
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
![Page 18: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/18.jpg)
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
![Page 19: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/19.jpg)
Copyright 1998-2010 Curt Hill
Example declaration• A declaration:
int a, count;double average;
• Three variables– Two integers– One real
![Page 20: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/20.jpg)
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
![Page 21: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/21.jpg)
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
![Page 22: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/22.jpg)
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
![Page 23: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/23.jpg)
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
![Page 24: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/24.jpg)
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
![Page 25: Copyright 1998-2010 Curt Hill Variables What are they? Why do we need them?](https://reader035.vdocument.in/reader035/viewer/2022062803/56649f455503460f94c664c2/html5/thumbnails/25.jpg)
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