{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03_Using_Classes_and_Objects_2_per_page

03_Using_Classes_and_Objects_2_per_page - 3 Using Classes...

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

View Full Document Right Arrow Icon
1 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 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 1

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

View Full Document Right Arrow Icon
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" ; 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 2
3 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!" ); } 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 3

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

View Full Document Right Arrow Icon