3.1- 3.2 Creating Objects and the String Class
default value for char
'\u0000'
dot operator
Can be used to invoke methods once an object has been instantiated -appended after the object reference ex: numChars title. length();
What can a variable hold?
Can either hold 1. Primitive value (boolean, char, int, etc) 2. Reference to an object
Why is it important to manage aliases carefully?
Changing an object through 1 reference changes the object for all of its aliases because there is only 1 object -address directs program to new object, and old address is not used to find value and goes in garbage
Object reference variable
Holds address of an object (object must be created separately)
default value for byte
0
default value for int
0
default value for short
0
default value for double
0.0d
default value for float
0.0f
default value for long
0L
Why is garbage collection needed?
1. A primitive knows the amount of space it will use but an object does not 2. If object does not know memory it will take up, computer gets rid of old object to ensure there is enough room for new object
What happens when a field is declared but not initialized?
1. It is NOT always necessary to assign a value when a field is declared 2. Fields that are declared but NOT initialized may be set to a reasonable default by the compiler - default will be zero or null (depending on data type) BUT relying on these data values is considered bad programming style
Automatic Garbage Collection
1. Java performs this process periodically to determine which objects are candidates for garbage collection 2. After collecting useless objects, Java tries to return object's memory to the program. (to make sure memory is not lost/ wasted on useless objects) - programmer is responsible for garbage collection in other programming languages
Difference between a primitive variable and object variable
1. Primitive variable - contains value itself 2.Object variable - contains ADDRESS of object
When does an object become "garbage"?
1. When an object has no valid references, it can no longer be accessed by program 2. This makes object useless and eligible for garbage collection
Aliases
2 or more references that refer to the same object (object can be accessed using multiple variables) - should be managed carefully
Explain the code: title = new String("Java Software Solutions");
A string object is created by calling the String constructor - string object includes ALL methods associated with it (not just "Java Software Solutions")
Class variable
A variable of a class that exists as one copy that all instances of a class refer to. Class variables include the keyword static.
Global Variable
A variable that can be used in any part of the program.
String Object
An object in programming that references a String. 1. does not need "new" operator because string objects are so common in program -> string literal is enclosed in double quotations 2. Immutable (if you retype a string, a new string object is made and the old string object is NOT changed) - there are several methods of the String class to return new String objects that are modified versions of the original
Which properties are true of string objects? a. Their lengths never change b. the shortest string has zero length c. Individual characters within a string may be changed using the replace method. d. Both A and B
Answer : D 1. Explain A: Strings are IMMUTABLE (CANNOT BE CHANGED) 2. Explain B: Shortest string length is "" (there are no characters btwn the quotes, so length is zero) 3. The "replace" method can change some characters by replacing them with others, but it does NOT Change the original string -> it changes a COPY!
Reference Assignment
Assignment copies the ADDRESS And puts it in new variable so that both variables use same address to find the same object ex: name1 ->"Steve Jobs" name2 -> "Steve Wozniak" name1 = name2 name1 -> "Steve Jobs" name2 -----^ -makes copy of name1's address and puts that address into name2 so now both point to same object
How does a regular assignment work?
Assignment takes COPY of value and stores it in a new variable ex: num 1 = 38 num 2 = 96 num 1 = num2; so NOW: num 1= 38 num 2= 38 - makes copy of 38 to put it in num2 variable
Local Variables
Compiler never assigns a default if this is uninitialized -ASSIGN A VALUE before you use a local variable (not doing so results in compile-time error)
Instantiation
Creation of an object using the new operator - object is an INSTANCE of a particular class
What do parentheses at the end of a method do?
Indicate that the method is being invoked
Null Reference
Indicates that reference variable does not refer to an object -The word "null" can check for references before following them.
String Index
It is helpful to refer to a particular character within a string -> must specify a character's numeric index (position in a string) - index begins at zero *SPACES, PUNCTUATION, ETC counts as characters ex: title = "Hello"; H is at index 0, and O is at index 4
Assume s1, s2, and s3 are string variables initialized to "Amanda", "bobby", and "Chris", respectively. Which String variable or variables are changed by each of the following statements? System.out.println(s2.replace('B','M'));
S2 does not change. - this is because the method only changes a COPY of s2, not the original string object
Assume s1, s2, and s3 are string variables initialized to "Amanda", "bobby", and "Chris", respectively. Which String variable or variables are changed by each of the following statements? s3=s2.concat(s1);
S3 is changed from "Chris" to "BobbyAmanda."
Immutable
Once an object is created, its value nor its length can be changed
Object reference
POINTER to a location of an object - depict references graphically rather than with arbitrary address -address tells program to follow "pointer" to value
Constructor
Special method that has same name as the class and sets up the object
Substring(int offset, int endIndex)
Start at index offset and extend it through endIndex-1
Write a declaration for a String variable called front, and initialize it to the first 10 characters of another String object called description.
String front = description.substring(0,10);
Write a statement that prints the 8th character of a string object called introduction.
System.out.println (introduction.charAt(7));
Uninitialized
When variables do not have data to hold
Method invocation
calling a method to execute its code; can be thought of as ASKING AN OBJECT TO PERFORM A SERVICE - a method may return a value, which can be used in assignment or expression
Classname
can be used as a type to declare an object reference variable (class= type of object) ex: String title; - this creates a variable (title) that references a string object (NO OBJECT IS CREATED)
concat(String str)
concatenates the character string in parentheses or str to the end of the current string ex: String phrase = "Change is inevitable"; mutation1= phrase.concat(", except from vending machines"); mutation1 contains "Change is inevitable, except from vending machines"
default value for boolean
false
default value for String/ any object
null (this is a reserved word)
What output is produced by the following code fragment? String s1= "Foundations"; String s2; System.out.println(s1.charAt(1)); s2=s1.substring(0,5); System.out.println(s2); System.out.println(s1.length()); System.out.println(s2.length());
o Found 11 5
"new" operator
used to create an object; allocates memory for a variable and returns the address of a new object
