Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects.

Similar presentations


Presentation on theme: "Objects."— Presentation transcript:

1 Objects

2 Getting classy Your current job
Gain experience creating and manipulating objects from the standard Java types Why Prepares you for defining your own classes and creating and manipulating the objects of those classes

3 Values versus objects Numbers
Have values but they do not have behaviors Objects Have attributes and behaviors System.in References an InputStream Attribute: keyboard Behaviors: reading System.out References an OutputStream Attribute: monitor Behaviors: printing Java treats object variables differently. Although an object variable is the symbolic name for a memory location being used by the program, the memory location for an object variable does not store a value of that object type. Instead the value in the memory location tells the program where to find a value (object) of that type. We say that an object variable references or points to an object of the object type. Thus, a String variable references a memory location that holds a value of type String. Similarly, a BufferedReader variable references a memory location holding a BufferedReader value.

4 Other Java object types
String Rectangle Color JFrame

5 Consider Statements int peasPerPod = 8;
String message = "Don't look behind the door!“ How show we represent these definitions according to the notions of Java? Java treats object variables differently. Although an object variable is the symbolic name for a memory location being used by the program, the memory location for an object variable does not store a value of that object type. Instead the value in the memory location tells the program where to find a value (object) of that type. We say that an object variable references or points to an object of the object type. Thus, a String variable references a memory location that holds a value of type String. Similarly, a BufferedReader variable references a memory location holding a BufferedReader value.

6 Representation

7 Shorthand representation

8 Examples Consider String a = "excellence“; String b = a;
What is the representation?

9 Examples Consider String a = "excellence“; String b = a;
What is the representation?

10 Uninitialized versus null
Consider String dayOfWeek; BufferedReader inStream; What is the representation?

11 Uninitialized versus null
Consider String dayOfWeek; BufferedReader inStream; What is the representation?

12 Uninitialized versus null
Consider String fontName = null; BufferedReader fileStream = null; What is the representation?

13 Uninitialized versus null
Consider String fontName = null; BufferedReader fileStream = null; What is the representation?

14 Assignment Consider String word1 = "luminous";
String word2 = "graceful"; Word1 = word2; Initial representation

15 Assignment Consider String word1 = "luminous";
String word2 = "graceful"; Word1 = word2; After assignment

16 Using objects Consider BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine();

17 Using objects Consider BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine();

18 Using objects Consider BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine(); Suppose the user interaction is Enter your account name: artiste

19 String representation
Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Truer representation Another useful String member method is charAt(). This method expects a single index as its parameter and returns the value of the character at that position in its String. While such behavior seems relatively natural given the method name and its parameter, what is not natural is that in Java, as in several other programming languages, the first character of a String is located at index 0.

20 String representation
Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); char c2 = alphabet.charAt(15); char c3 = alphabet.charAt(2); What are the values of c1, c2, and c3? Why? Another useful String member method is charAt(). This method expects a single index as its parameter and returns the value of the character at that position in its String. While such behavior seems relatively natural given the method name and its parameter, what is not natural is that in Java, as in several other programming languages, the first character of a String is located at index 0.

21 Program WordLength.java
public class WordLength { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a word: "); String word = stdin.readLine(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); }

22 More String methods Consider String weddingDate = "August 21, 1976";
String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output?

23 More String methods Consider String weddingDate = "August 21, 1976";
String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output? Month is August.

24 More String methods Consider String fruit = "banana";
String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output?

25 More String methods Consider String fruit = "banana";
String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output? First search: 1 Second search: 3 Third search: -1

26 More String methods Consider int v1 = -12; double v2 = 3.14;
char v3 = 'a'; String s1 = String.valueOf(v1); String s2 = String.valueOf(v2); String s3 = String.valueOf(v3);

27 Final variables Consider
final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; What is the representation? In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

28 Final variables In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

29 Rectangle

30 Rectangle

31 Rectangle Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2);
BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

32 Rectangle Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2);
BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

33 Final variables Consider final String LANGUAGE = "Java";


Download ppt "Objects."

Similar presentations


Ads by Google