Transcript
Page 1: COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis

COMP 14: Primitive Data and Objects

May 24, 2000

Nick Vallidis

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

Announcements

• Could you get Visual J++? Book?

• Is everyone signed up for an ISIS account?

• Office Hours?

• Lab Assistants’ hours are up

• Too fast?

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

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?

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

Outline for today

• Data in Java

• Data Types

• Variables

• Methods

• Strings

• Class libraries: Using Objects

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

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”

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

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

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

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

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

Data Type example

• Integers

• Set of values:– {…, -2, -1, 0, 1, 2, …}

• Set of operations:– addition, subtraction, multiplication, division

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

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.

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

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

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

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;

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

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.7x10308 to 1.7x10308 (approx)– char -- 16 bits -- one character such as ‘a’,

‘G’, ‘!’, ‘4’, etc. (also things like space, enter)– boolean -- holds true or false

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

Assignment

• Changes the value held by a variable

• Example:

• Destroys the old value in the variable

sum = 10;

int numBooks = 5;

numBooks = 25;

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

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;

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

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;

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

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

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

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

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

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)

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

Let’s break it apart

System.out.println (“Hello!”);

objectobject methodmethodInformation provided to the methodInformation provided to the method

(parameters)(parameters)

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

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

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

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

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

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!”;

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

Simple Program modified

public class Simple

{

public static void main(String[] args)

{

String greeting = “Hello!”;

System.out.println(greeting);

}

}

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

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!”);

<reference> = new <class>(<parameters>);

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

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!”);

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

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?”);

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

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);

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

Other string “problems”

• What if you want a <return> 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.”);

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

Escape Sequences

• There’s another way!

• If you put ‘\n’ in the string literal, it becomes a <return> 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.”);

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

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?)

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

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

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

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 <class name>

import java.lang.*;

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

Examples

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

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.


Top Related