03_Using_Classes_and_Objects

03_Using_Classes_and_Objects - 3. Using Classes and Objects...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Slide 3 - 1 COMP 1210 – Fundamentals of Computing I 3. Using Classes and Objects • Objectives - when we have completed this set notes, you should be familiar with: object creation and reference types the String class packages and the import declaration the Random class the Math class formatting output: NumberFormat and DecimalFormat wrapper classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 3 - 2 COMP 1210 – Fundamentals of Computing I Review: Primitive Types • Recall that a variable can be used to store a primitive type: int number; • sets aside 32 bits of storage for an integer called number number = 67; • the variable number now holds a value of 67 • Recall that Java has 8 primitive types: byte, short, int, long - - integer types float, double - - floating point types char - - holds a single character (e.g., 'A' , 'a' , '$' ) boolean - - values of true , false • All other types are object (or reference) types
Background image of page 2
Slide 3 - 3 COMP 1210 – Fundamentals of Computing I Objects: Basics • Objects are defined by classes; the type for an object is the class rather than a primitive type Variables for objects are be declared using the class name; consider a variable for a String object String title; Objects are created with the new operator; and a variable can then be initialized by assignment: title = new String( "A book" ); Or both declared and initialized with a new object: String team = new String( "Red Sox" ); The String is used so often that Java allows: String location = "Shelby Center" ;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 3 - 4 COMP 1210 – Fundamentals of Computing I Creating Objects • Object variables are reference variables ; they don’t hold the object; they hold a memory location where the object is stored If primitive types are ‘suitcases’ that store contents then reference variables are suitcases that contain an address that ‘points’ to the location of the contents. • Represented graphically… "Steve Jobs" name1 num1 52 [memory address] Primitive Type: Reference Type:
Background image of page 4
Slide 3 - 5 COMP 1210 – Fundamentals of Computing I Creating Objects • Declaration does not create an object. Sets aside space for the memory address that title will hold String title; The placeholder memory address can be set to null to indicate that no String object has been created, which allows the program to check for the existence of the object. title = null; // not the same as title = ""; if (title == null ) { System.out.println( "No title set!" ); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 3 - 6 COMP 1210 – Fundamentals of Computing I Creating Objects • The new operator is used to create an object title = new String( "Intro to Computing" ); Calls a constructor in the String class, which is a special method that sets up the String object • Creating an object is called instantiation - creates an instance of the class • An object is an instance of a particular class Scanner myScan = new Scanner(System.in);
Background image of page 6
Slide 3 - 7 COMP 1210 – Fundamentals of Computing I Invoking Methods
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

03_Using_Classes_and_Objects - 3. Using Classes and Objects...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online