Download presentation
Presentation is loading. Please wait.
Published byKristopher Harrison Modified over 9 years ago
1
Lab 4 - Variables
2
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
3
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...
4
Example of Accessors public class SimpleClass { private int instanceVariable; public int getInstanceVariable() { return instanceVariable; } public void setInstanceVariable(int val) { instanceVariable = val; } Instance Variable Getter Setter SimpleClass x = new SimpleClass(); x.setInstanceVariable(32); System.out.println(x.getInstanceVariable()); Use
5
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
6
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.
7
All Primitive Types We typically use int for integers and double for real values even if they hold more information than we require
8
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.
9
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? 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 0899 +0130 ------- 1029
10
Storing Real Values Real values are essentially stored in scientific notation 12.33 = 0.1233 * 10 2 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
11
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
12
ASCII (8 bit subset of unicode) * Table from http://www.jimprice.com/ascii-0-127.gif
13
Primitive vs. Reference in Assignment Statements int first; int second; first = 32; second = first; System.out.println("first = " + first + " second = " + second); second = 14; System.out.println("first = " + first + " second = " + second); first second 32 --- 14 Output: first = 32 second = 14 first = 32 second = 32
14
---- -6 Now – With Reference Variables SimpleClass first; SimpleClass second; first = new SimpleClass(); first.setInstanceVariable(32); second = first; System.out.println(second.getInstanceVariable()); second.setInstanceVariable(-6); System.out.print(first.getInstanceVariable()); first second SimpleClass instanceVariable 32 Output: 32 -6
15
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)
16
That’s All!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.