Lab 4 - Variables

Lab 4 - Variables
Information Hiding
• General Principle:
– Restrict the access to variables and methods as much
as possible
• Can label instance variables and methods with:
– private – only that class can see
– public – any class can see
• Standard:
– all instance variable are private
– methods others uses are public
– methods used only by the current class are private
Getters and Setters
• Since we have hidden our instance variables
we need some way to let others view or
modify them
• Make public methods to provide that access:
– getter: returns the value in the variable
– setter: changes the value in the variable
– naming conventions . . .
Example of Accessors
public class SimpleClass
{
Instance Variable
private int instanceVariable;
public int getInstanceVariable()
{
return instanceVariable;
}
public void setInstanceVariable(int val)
{
instanceVariable = val;
}
Getter
Setter
}
SimpleClass x = new SimpleClass();
Use
x.setInstanceVariable(32);
System.out.println(x.getInstanceVariable());
Why did we bother with that?
• There is one place where the value of the
variable changes
– since variable is private, no code in other classes
can change it
– only the setter changes it in our code
• A single breakpoint catches ANY changes to
the value
Primitive Types for Integers
• Remember: primitive means that the compiler
knows how much space to allocate
• This means that these types have fixed space
and, therefore, have limitations on the values
they can hold.
All Primitive Types
We typically use int for integers and double for real values even if they hold
more information than we require
Integers and Sign Bits
• Every integer has a boolean that represents
whether the number is negative (true implies
the number is negative)
• That boolean is stored as a 1 or 0 at the high
end of the integer.
• Suppose we can hold three decimal digits in
an integer, then -999 would be stored as 1999
and 999 would be stored as 0999.
What Happens When Things Get Too
Large?
• Suppose we can only hold three decimal digits (so
we can store numbers from -999 to 999)
• What happens if we add 130 to 899?
0899
+0130
------1029
• So, 899 + 130 = -29?
• This is called overflow and it happens when we
try to represent a number larger than our integer
variable will hold
Storing Real Values
• Real values are essentially stored in scientific
notation
• 12.33 = 0.1233 * 102
• So the computer just has to store the two
parts: 1233 and 2
• Instead of having firm upper and lower
bounds, real variable types vary in the
precision they can represent
char Type and Unicode
• The computer stores EVERYTHING as a
number
• When we store characters (in char variables or
in Strings), we actually store integers
• Unicode defines the matching of these
integers to characters
ASCII (8 bit subset of unicode)
* Table from http://www.jimprice.com/ascii-0-127.gif
Primitive vs. Reference in Assignment
Statements
int first;
int second;
first
first = 32;
second = first;
second
System.out.println("first = " + first + " second = " + second);
second = 14;
System.out.println("first = " + first + " second = " + second);
Output:
first = 32 second = 32
first = 32 second = 14
32
--- 14
32
Now – With Reference Variables
first
SimpleClass first;
SimpleClass second;
first = new SimpleClass();
first.setInstanceVariable(32);
second = first;
second
System.out.println(second.getInstanceVariable());
second.setInstanceVariable(-6);
System.out.print(first.getInstanceVariable());
Output:
32
-6
SimpleClass
instanceVariable
---- -6
32
BigIntegers
• A class that comes with Java that can hold
integers that are arbitrarily large
– Think about the three ways you can interpret
“class” in that statement
– class implies reference (not primitive)
• As part of the lab, you have to go out and find
the java API (Application Programming
Interface)
That’s All!