COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.

Download COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.

Post on 22-Dec-2015

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis
  • Slide 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?
  • Slide 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?
  • Slide 4
  • Outline for today Data in Java Data Types Variables Methods Strings Class libraries: Using Objects
  • Slide 5
  • Need 2 things for a program Just like baking a cake. You need: ingredients a recipe in programming, we need: data algorithm Today well talk about our ingredients
  • Slide 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
  • Slide 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
  • Slide 8
  • Data Type example Integers Set of values: {, -2, -1, 0, 1, 2, } Set of operations: addition, subtraction, multiplication, division
  • Slide 9
  • Another example Lets take the car object example Values: more complex: well limit it to speed, amount of gas in tank, and what gear its in Operations: speed up, slow down, change gear, etc.
  • Slide 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
  • Slide 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;
  • Slide 12
  • Primitive Data Types Well 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
  • Slide 13
  • Assignment Changes the value held by a variable Example: Destroys the old value in the variable sum = 10; int numBooks = 5; numBooks = 25;
  • Slide 14
  • Constants What do we do if the variables value never changes? We can tell the compiler this: final means I wont ever change the value of this variable (thus, its value is constant) use all capital letters for identifier final int MAX_BRAINS = 1;
  • Slide 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;
  • Slide 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
  • Slide 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
  • Slide 18
  • Weve 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)
  • Slide 19
  • Lets break it apart System.out.println (Hello!); object method Information provided to the method (parameters)
  • Slide 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 wed like to print Hello! on the screen System.out.print is the same, but it doesnt go to the beginning of the next line
  • Slide 21
  • So what is Hello!? Were sending this data to the println method, but what is its data type? Its a special case in Java called a string literal Its data type is actually the String class
  • Slide 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!;
  • Slide 23
  • Simple Program modified public class Simple { public static void main(String[] args) { String greeting = Hello!; System.out.println(greeting); }
  • Slide 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 ( );
  • Slide 25
  • You saw a shortcut for strings... Before, I did it this way though: Thats 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!);
  • Slide 26
  • Concatenating Strings What if we want to print out something long that doesnt fit on one line? Java doesnt like this: string literals must be on one line System.out.println(Hello, how are you this fine day?);
  • Slide 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);
  • Slide 28
  • Other string problems What if you want a in the string? You cant do this: You could do: System.out.println(Hello. Nice to meet you.); System.out.println(Hello.); System.out.println(Nice to meet you.);
  • Slide 29
  • Escape Sequences Theres 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.);
  • Slide 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?)
  • Slide 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
  • Slide 32
  • Packages When you want to use a class in a package, you have to let the compiler know Even though you dont have to type it, Java behaves like all programs have this import: import import java.lang.*;
  • Slide 33
  • Examples
  • Slide 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.

Recommended

View more >