Download presentation
Presentation is loading. Please wait.
1
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis
2
Announcements Could you get Visual J++? Book? Is everyone signed up for an ISIS account? Office Hours? Lab Assistants’ hours are up Too fast?
3
Review What are the rules for a Java identifier? What symbol indicates the end of a statement in Java? What symbols start and end a section in a Java program? What are the two ways of indicating comments?
4
Outline for today Data in Java Data Types Variables Methods Strings Class libraries: Using Objects
5
Need 2 things for a program Just like baking a cake. You need: –ingredients –a recipe in programming, we need: –data –algorithm Today we’ll talk about our “ingredients”
6
How does Java handle data? Primitive data –data fundamental to a computer –numbers, characters (because people use them) Objects –represent more complex concepts by combining primitive data –e.g., a car or a hotel
7
Data Types Both primitive data and objects can be described by a data type Data types have two parts: –a set of possible values –a set of operations
8
Data Type example Integers Set of values: –{…, -2, -1, 0, 1, 2, …} Set of operations: –addition, subtraction, multiplication, division
9
Another example Let’s take the car object example Values: –more complex: we’ll limit it to speed, amount of gas in tank, and what gear it’s in Operations: –speed up, slow down, change gear, etc.
10
Objects and Data Types You tell the computer the data type for an object by writing a class A class contains: –variables to keep track of the value –methods to perform the operations
11
Variables A location in memory associated with an identifier and a specific data type Declaration: –tell the compiler to make space for a specific kind of data and assign it a name –you can also specify a value for the variable int sum; int sum = 0;
12
Primitive Data Types We’ll only use int, double, char, boolean: –int -- 32 bits -- integer from -2 billion to 2 billion (approx.) –double -- 64 bits -- floating point value from -1.7x10 308 to 1.7x10 308 (approx) –char -- 16 bits -- one character such as ‘a’, ‘G’, ‘!’, ‘4’, etc. (also things like space, enter) –boolean -- holds true or false
13
Assignment Changes the value held by a variable Example: Destroys the old value in the variable sum = 10; int numBooks = 5; numBooks = 25;
14
Constants What do we do if the variable’s value never changes? We can tell the compiler this: final means “I won’t ever change the value of this variable” (thus, it’s value is constant) use all capital letters for identifier final int MAX_BRAINS = 1;
15
Expressions You can combine operators and operands to form expressions (usually as part of an assignment statement) for int some operations are: +, -, *, /, % answer = 2 + 2; answer = 17 / 3 + 6; answer = answer * 2;
16
Remember this? You tell the computer the data type for an object by writing a class A class contains: –variables to keep track of the value –methods to perform the operations
17
Methods Methods are collections of programming statements that are given a name Usually they perform one of the operations of the data type Called procedures and functions when not in an object-oriented language
18
We’ve already seen methods public class Simple { public static void main(String[] args) { System.out.println(“Hello!”); } Here we are using a method (making a “call” to that method)
19
Let’s break it apart System.out.println (“Hello!”); object method Information provided to the method (parameters)
20
System.out.println(“Hello!”); println is the name of a method that belongs to the System.out object It tells the System.out object that we’d like to print Hello! on the screen System.out.print is the same, but it doesn’t go to the beginning of the next line
21
So what is “Hello!”? We’re sending this data to the println method, but what is its data type? It’s a special case in Java called a string literal It’s data type is actually the String class
22
Objects work like primitive data You declare an object variable just like primitive data, but with a class data type: So we could do our simple program in a slightly different way if we wanted... String greeting; String greeting = “Hello!”;
23
Simple Program modified public class Simple { public static void main(String[] args) { String greeting = “Hello!”; System.out.println(greeting); }
24
Things work a little differently... When you declare a variable with a class data type, it really just creates a reference You have to create an object of that data type (class) -- this is called instantiation String greeting; greeting = new String(“Hello!”); = new ( );
25
You saw a shortcut for strings... Before, I did it this way though: That’s because strings are so common, that Java lets you use string literals as a shortcut: String greeting = “Hello!”; greeting = “Hello!”; is the same as gretting = new String(“Hello!”);
26
Concatenating Strings What if we want to print out something long that doesn’t fit on one line? Java doesn’t like this: string literals must be on one line System.out.println(“Hello, how are you this fine day?”);
27
The solution The + operator can also be used to glue two strings together This works with primitive data too! System.out.println(“Hello, how” + “ are you this fine day?”); int result = 5; System.out.println(“Result: “ + result);
28
Other string “problems” What if you want a in the string? You can’t do this: You could do: System.out.println(“Hello. Nice to meet you.”); System.out.println(“Hello.”); System.out.println(“Nice to meet you.”);
29
Escape Sequences There’s another way! If you put ‘\n’ in the string literal, it becomes a in the output System.out.println(“Hello.\nNice to meet you.”); works the same as System.out.println(“Hello.”); System.out.println(“Nice to meet you.”);
30
Escape Sequences Called this because the ‘\’ is an escape character, which means we want to do something special Two others you should know: –\” -- puts a “ in the string (why do we need it?) –\\ -- puts a \ in the string (why do we need it?)
31
Class Libraries Java comes with a bunch of classes that you can use in your programs (e.g., String ) a class library is just a name for a set of such classes Java puts a bunch of classes together in a package
32
Packages When you want to use a class in a package, you have to let the compiler know Even though you don’t have to type it, Java behaves like all programs have this import: import import java.lang.*;
33
Examples
34
Homework Reading: 2.1-2.7 P1 (Program1) goes out today and is due Friday. There is no “programming” involved in this assignment. The purpose is to get you used to Visual J++. Additionally, you see how you will hand in your programs.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.