Objects with Strings Flashcards
Object
Is an in-memory data structure that combines state and behavior into a usable and useful abstraction.
Class
Source code that defines how to create an object and what state and behavior that object will have.
Steps to instantiate or create an Object from a Class
- Declare a variable to hold the object. The class will be the data type.
- Instantiate a new object from the class using the new keyword.
- Initialize variables inside the object with initial values using the constructor.
Data Types Memory
Value Type - primitive
Reference Type - Object
Value Type (primitive)
Reference a single static space in memory to hold the value. This memory is allocated on the stack.
Only 8 value types: byte, char, short, int, long, float, double, and boolean.
Reference Type (Object)
Holds a reference to where the object is located in free-floating, dynamic memory, the heap. Everything except the 8 value types is a reference type.
Pass by Value
Pass the value of the variable.
Is Java Pass by Value or Pass by Reference?
Java is always Pass by Value. Java passes the value on the Stack when passing a variable to a method.
Immutability
An object whose state cannot be changed after it is created. A string is immutable, meaning after a string is created the value cannot be changed, but instead, a new String must be created with the new value.
Null
Not the same thing as empty. Refers to no value on the Stack. Empty refers to the value of an existing object on the Heap.
Object Equality
The value of Reference Types (Objects) cannot use == to compare the value of objects, instead they use the .equals() method.
Reference Equality ( == )
The equality operator compares the value of 2 variables on the stack. For objects, this is the reference to the Object on the heap
Value Equality ( .equals() )
The .equals() method compares the value of Objects on the Heap
Object characteristics
- Live in the computer’s memory and only exist while a program is executing.
- Is distinct and separate from every other object in a program.
- Not written in a source code. Instead, they are what is created when a program executes, based on the source code.
- Everything in Java is an Object, except the primitive data types (byte, short, int, long, float, double, boolean, and char).
Class characteristics
- Is a blueprint for an object.
- Classes don’t exist when the program is running, they only exist in source code.
- Defines a Data Type, which makes everything in Java a Data Type.