Announcements If you need more review of Java… I have lots of good resources – talk to me Use...

26
LECTURE 4: REFERENCES & ENUM

Transcript of Announcements If you need more review of Java… I have lots of good resources – talk to me Use...

LECTURE 4:REFERENCES & ENUM

Announcements

If you need more review of Java… I have lots of good resources – talk to me Use “Additional Help” link on webpage

Weekly assignments problems due before class Remove rust from summer and get back

into coding Problems designed to help learn new

material, too

Data Types

8+1 primitive data types Examples: boolean, byte, char, int, double, String*

Only types that work with Java operators Operators include: +, -, %, &&, ||, >=, <, !

Primitives used natively by computers Means that using them is very quick Implies greater support within the language

Primitive Types

Primitive variables are simple to use Each variable is a “xerox” holding a

value Assignment copies value Update variable being assigned only

Java Enumerations

enum a fancy type of Java class Includes fields, methods & constructors like

any class But constructors must be private… … so cannot create new instances of an enum

Specifies predefined list of instances Instances declared at start of enum Instances accessed as EnumName.instanceName

Represent known, finite data with an enum 3 Musketeers, 7 Dwarfs, Card Suits, …

Enumeration Methods

enum instances always include these methods public String name()

Returns String equal to actual instances' name public int ordinal()

Position in listing where instance was declared

Can also find all enumerated instances public static EnumName[] values()

Returns array of values enumerated by EnumName

Java Enumerations

Must be written in file EnumName.javapublic enum EnumName {

Instance0, Instance1, Instance2;}

Can be used in methods elsewhere:EnumName var = EnumName.Instance0;System.err.println(var.name());if (var.ordinal() > 1) { System.err.println(“Got Instance2!”);}EnumName[] arr = EnumName.values();

Quick Quiz

Should you be taking notes? Let’s see…

1. What are the 3 methods defined by each enum?

2. What do each of these methods do?3. Can you use ever compare an enum

using “>”?

Classes

Real world needs more than primitives Additional types defined by classes in

Java Classes usually begin with:public class ClassNameGoesHere { By convention, name starts with capital

letter Use interior capitals to highlight words in

name Should be in file named ClassNameGoesHere.java

ClassNameGoesHere becomes type to use

Creating Instances

Use instances of a class Keyword new creates instances:public class Kitty { ... }new Kitty(...);

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly telly

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);

cat

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);

cat tiger

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

cat tiger

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

cat tiger

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;

cat

tiger

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);

cat

tiger

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);tiger = new Kitty(...); cat

tiger

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);tiger = new Kitty(...);tiger = cat;

cat

tiger

Aliased variables refer to same instance Single instance get changes to aliased variables Aliased variables see all changes that are made But, assignments only affect the one variable

Kitty cat, tiger, kat;cat = new Kitty(...);tiger = new Kitty(...);kat = tiger;tiger = cat;cat = kat;tiger = null;

Aliasing

cat

tiger

kat

Your Turn

Get into your groups and complete activity

For Next Lecture

Reading from AF Chapters 8 & 9 for Wednesday What is the advantages of using arrays? When and why sucks to use arrays? Are arrays like primitives or references?

There is weekly assignment problem on Angel Due before Wednesday’s lecture (via e-mail) Get back into the swing of writing Java code