Download presentation
Presentation is loading. Please wait.
Published byMonica Summers Modified over 9 years ago
1
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
2
“AP Computer Science with.NET and J#” Lecture 4: Primitive Types vs. Object References Microsoft.NET Workshops for Teachers
3
4-3 MicrosoftAP Computer Science with.NET and J# Workshop Track LectureTopic 1Java — History, Philosophy, and Overview 2Procedural Programming in Java and J# 3Object-oriented Programming in Java and J# 4Primitive Types vs. Object References (==, equals, toString, arrays) 5Writing Your Own Classes 6Keeping Track of Your Objects with the Collection Classes 7Algorithms and Algorithm Analysis 8Debugging and Exception Handling...... 18Collection Classes and Iteration 19Additional Resources and Ideas
4
4-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “Java is a statically type-checked language, meaning the compiler checks to ensure variables are being used correctly. For example, when you write “ i++; ”, the variable i must be an integer or real number. This places great importance on the types in Java, and understanding exactly how they work…” Topics: –Primitive types vs. object references –Comparing types –Converting types –Arrays [ Joe Hummel, Lake Forest College ]
5
4-5 MicrosoftAP Computer Science with.NET and J# Part 1 Primitive Types vs. Object References
6
4-6 MicrosoftAP Computer Science with.NET and J# Recall… Recall our discussion that some things in Java are not objects This makes Java programs more efficient This makes Java programming more complex Object obj; obj =...; // let obj represent *any* object in Java int i; i =...; // let i represent any integer value String s1, s2; s1 = obj.toString(); // every object has a toString() method to convert object to a string s2 = i.toString(); s2 = String.valueOf(i); // since i is not an object, we have to ask String class to convert it Object obj; obj =...; // let obj represent *any* object in Java int i; i =...; // let i represent any integer value String s1, s2; s1 = obj.toString(); // every object has a toString() method to convert object to a string s2 = i.toString(); s2 = String.valueOf(i); // since i is not an object, we have to ask String class to convert it
7
4-7 MicrosoftAP Computer Science with.NET and J# Primitive Types The primitive types are not objects –short, int, long, float, double, char, boolean, … –value is stored entirely within the variable boolean b; char c; double d; int i; b = true; c = 'a'; d = 3.14159; i = 99; boolean b; char c; double d; int i; b = true; c = 'a'; d = 3.14159; i = 99; true ‘a’ 3.14159 99
8
4-8 MicrosoftAP Computer Science with.NET and J# Object References Objects are quite different… Objects are chunks of memory created to hold data + methods Object variables hold references to these chunks of memory –recall that a Student object has a first and last name, both strings… String s1; Student s2; s1 = "Hello world!"; s2 = new Student(); String s1; Student s2; s1 = "Hello world!"; s2 = new Student(); "Hello world!" 100 81 "Kim" "Lee"
9
4-9 MicrosoftAP Computer Science with.NET and J# So? The difference between primitive types and object types has ramifications throughout Java –Let’s discuss a few…
10
4-10 MicrosoftAP Computer Science with.NET and J# Part 2 Comparing Types
11
4-11 MicrosoftAP Computer Science with.NET and J# Comparison When comparing values, the type matters: –Primitive types are compared using operators: ==, !=,, … –Object types are compared using methods:.equals,.compareTo, … int i, j; i =...; j =...; if (i == j) // == works as expected for primitive types System.out.println("integers are the equal!"); String s1, s2; s1 =...; s2 =...; if (s1 == s2) // with objects, == may or may not work as expected… System.out.println("strings are the equal!"); int i, j; i =...; j =...; if (i == j) // == works as expected for primitive types System.out.println("integers are the equal!"); String s1, s2; s1 =...; s2 =...; if (s1 == s2) // with objects, == may or may not work as expected… System.out.println("strings are the equal!"); X
12
4-12 MicrosoftAP Computer Science with.NET and J# Demo! Comparing types…
13
4-13 MicrosoftAP Computer Science with.NET and J# Comparing Objects When comparing objects, use: –.equals to see if 2 objects contain the same data –.compareTo to see if 1 object’s data is another object’s data String s1, s2; s1 =...; s2 =...; if (s1.equals(s2)) System.out.println("strings are equal!"); if (s1.toLowerCase().equals(s2.toLowerCase())) System.out.println("strings are equal regardless of case!"); if (s1.compareTo(s2) < 0) // think “s1 < s2” System.out.println("s1 is alphabetically less than s2"); else if (s1.compareTo(s2) > 0) // think “s1 > s2” System.out.println("s1 is alphabetically greater than s2"); else System.out.println("s1 is equal to s2"); String s1, s2; s1 =...; s2 =...; if (s1.equals(s2)) System.out.println("strings are equal!"); if (s1.toLowerCase().equals(s2.toLowerCase())) System.out.println("strings are equal regardless of case!"); if (s1.compareTo(s2) < 0) // think “s1 < s2” System.out.println("s1 is alphabetically less than s2"); else if (s1.compareTo(s2) > 0) // think “s1 > s2” System.out.println("s1 is alphabetically greater than s2"); else System.out.println("s1 is equal to s2");
14
4-14 MicrosoftAP Computer Science with.NET and J# Part 3 Converting Types
15
4-15 MicrosoftAP Computer Science with.NET and J# Conversion Java is statically-typed, so you have to convert between types Example: –String-based input must be converted if you want numeric value –Rule of thumb: when converting to type T, use class T… String input; int i; double d; System.out.println("Please enter an integer: "); input = keyboard.readLine(); i = Integer.valueOf(input).intValue(); System.out.println("Please enter a real number: "); input = keyboard.readLine(); d = Double.valueOf(input).doubleValue(); d = i * d; System.out.println("The result = " + String.valueOf(d)); String input; int i; double d; System.out.println("Please enter an integer: "); input = keyboard.readLine(); i = Integer.valueOf(input).intValue(); System.out.println("Please enter a real number: "); input = keyboard.readLine(); d = Double.valueOf(input).doubleValue(); d = i * d; System.out.println("The result = " + String.valueOf(d));
16
4-16 MicrosoftAP Computer Science with.NET and J# Demo! Converting types…
17
4-17 MicrosoftAP Computer Science with.NET and J# Type-casting Type-casting is another way to “convert” types… Fundamentally different types must be converted –For example, string to integer Types that are similar can be type-cast –Type-cast is needed to get past Java’s static type-checking –For example, double to integer… int i; double d; d =...; i = (int) d; // type-cast will cause Java to truncate digits to right of decimal System.out.println(d); System.out.println(i); int i; double d; d =...; i = (int) d; // type-cast will cause Java to truncate digits to right of decimal System.out.println(d); System.out.println(i);
18
4-18 MicrosoftAP Computer Science with.NET and J# Part 4 Arrays in Java
19
4-19 MicrosoftAP Computer Science with.NET and J# Arrays Arrays are conceptually the same: –Index-based addressing, starting at 0 –Support for single or multi-dimensional arrays Key difference: –Arrays are objects, and must be created… int[] quizzes; quizzes = new int[10]; quizzes[0] =...; quizzes[1] =...;. quizzes[9] =...; int sum = 0; for (int i = 0; i < quizzes.length; i++) // for each element in array... sum = sum + quizzes[i]; double avg; avg = sum / 10.0; int[] quizzes; quizzes = new int[10]; quizzes[0] =...; quizzes[1] =...;. quizzes[9] =...; int sum = 0; for (int i = 0; i < quizzes.length; i++) // for each element in array... sum = sum + quizzes[i]; double avg; avg = sum / 10.0; … 0 1 2 9 0 0
20
4-20 MicrosoftAP Computer Science with.NET and J# Demo! Arrays…
21
4-21 MicrosoftAP Computer Science with.NET and J# Multi-dimensional Arrays Declared with a somewhat unusual syntax… double[,] matrix; matrix = new double[10,10]; // a two-dimensional, 10-by-10 array matrix[0,0] = 3.14159;. double[,] matrix; matrix = new double[10,10]; // a two-dimensional, 10-by-10 array matrix[0,0] = 3.14159;.
22
4-22 MicrosoftAP Computer Science with.NET and J# Summary Primitive types behave differently than objects You need to program differently based on types: –When comparing types –When converting types –When working with arrays Welcome to the world of object-oriented programming! –where almost everything is an object :-)
23
4-23 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: –http://blogs.msdn.com/daryllmc/default.aspxhttp://blogs.msdn.com/daryllmc/default.aspx –http://www.lakeforest.edu/~hummel/workshops-HS.htmhttp://www.lakeforest.edu/~hummel/workshops-HS.htm –https://www.mainfunction.com/home/training/https://www.mainfunction.com/home/training/
24
4-24 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 5Writing Your Own Classes......
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.