Variables in Java

Download Report

Transcript Variables in Java

Variables
What are they?
Why do we need them?
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
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
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
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
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
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
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
Copyright 1998-2010 Curt Hill
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
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
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
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
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
Copyright 1998-2010 Curt Hill
Integer Operators
•
•
•
•
•
•
Addition +
Subtraction –
Multiplication *
Division / (quotient)
Division % (remainder)
Examples:
– 7 / 2 is 3
– 7 % 2 is 1
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
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
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
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
Copyright 1998-2010 Curt Hill
Example declaration
• A declaration:
int a, count;
double average;
• Three variables
– Two integers
– One real
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
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
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
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
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
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
Copyright 1998-2010 Curt Hill