Download presentation
Presentation is loading. Please wait.
Published byElmer Douglas Modified over 9 years ago
2
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
3
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
4
Primitive Types Primitive variables are simple to use Each variable is a “xerox” holding a value Assignment copies value Update variable being assigned only
5
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, …
6
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
7
Java Enumerations Must be written in file EnumName.java public 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();
8
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 “ > ”?
9
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
10
Creating Instances Use instances of a class Keyword new creates instances: public class Kitty {... } new Kitty(...);
11
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 object TV telly = new TV(...);
12
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 object TV telly = new TV(...); telly
13
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 object TV telly = new TV(...); telly
14
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 object TV telly = new TV(...); telly
15
Using Instances Use instances of a class Each instance is unique Each instance is unique Kitty cat = new Kitty(...); cat
16
Using Instances Use instances of a class Each instance is unique Each instance is unique Kitty cat = new Kitty(...); Kitty tiger = new Kitty(...); cattiger
17
Using Instances Use instances of a class Each instance is unique Each instance is unique Kitty cat = new Kitty(...); Kitty tiger = new Kitty(...); cat = new Kitty(...); cattiger
18
Using Instances Use instances of a class Each instance is unique Each instance is unique Kitty cat = new Kitty(...); Kitty tiger = new Kitty(...); cat = new Kitty(...); cattiger
19
Working With References Assignments alias references Only way to create instance using new Refer to same instance when aliased
20
Working With References Assignments alias references Only way to create instance using new Refer to same instance when aliased Kitty cat, tiger; cat tiger
21
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
22
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
23
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
24
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
25
Your Turn Get into your groups and complete activity
26
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.