Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Objects & How They Work

Java Objects & How They Work

A presentation I made to my AP Computer Science class on basic Java.

Ethan Turkeltaub

September 26, 2011
Tweet

More Decks by Ethan Turkeltaub

Other Decks in Programming

Transcript

  1. What is an Object? • An Object is anything: a

    tree, person, book, anything. • The blueprint for an Object is a Class. • Classes have fields. A field is what the Object will "know" (more on Types of fields later). • Classes have constructors. It can have a default constructor with no parameters, and/or many with parameters. • Classes have methods. Methods can be mutators (change information) or accessors (get information)
  2. Creating a new object. Employee FF = new Employee("Fred", "Flintstone",

    25, "Bronto Crane Operator", 33000.0); • Creating a new Employee with the name "Fred" and the surname "Flintstone". • His age is 25, he works as a Bronto Crane Operator, and he makes $33000.00 per year. How does Java remember this?
  3. Random Access Memory (RAM) • RAM has many "slots" for

    storing data in. • When you create a variable or object, Java stores information in a slot or slots. • Then, when you want that information again, you call the variable or method. ◦ Java finds the slot it's stored in and returns its' contents to you.
  4. Types Primitive byte short int long float double boolean char

    Reference Array Class Interface (Object) String String[] Scanner Random ...
  5. What's the difference? • Primitive types hold the actual value

    — reference types hold the memory location (#667Fddee12134, or something similar) • When compared (==), primitive type's value is compared. Reference type's location is compared. • The Object's contents is one line (the memory location). For that reason, only something one line can fit there (so no Strings, can be more than one line!)
  6. The P.O. Box Analogy • When you open a P.O.

    Box, you get a key. That key is like an Object's memory address. • If you open your P.O. Box, you can get the contents of it. Memory Location ("Key") Contents
  7. The P.O. Box Analogy • Inside the P.O. Box are

    many letters or packages. • Those letters have information (our variables).
  8. Talking to Objects • In Java, we communicate with Objects

    using dot-notation. • We access or modify information associated with the Object using methods. Employee FF = new Employee("Fred", "Flintstone", 25, "Bronto Crane Operator", 33000.0); FF.getName(); // => "Fred" When you do this, you're requesting information. Or, you're modifying the information that is there.