Chapter 3 - In person Class Flashcards
What does scan.nextline(); do? (differently than scan.nextInt).
It gobbles up the enter key you used for the inputted numbers.
How do you make sure your scan can take a string as an integer.
val1 = Integer.parseInt(scan.nextLine());
What is a null pointer?
It’s when the String title; has not been initialized yet. It points to nothing and we call it “null pointer”.
How do you create an object?
title = new String (“Java Software Solutions”);
What is the constructor?
The constructor is a special method that sets up the object (the name must match).
It is a special method that has the same name as the class.
How do you invoke a method?
count = title.length();
How do you access the methods of the scanner object scan?
You type a period after the name i.e.
scan.
String s; -> declares a variable s.
s = new String (“Hello World”); - -> assigns the string value.
We can do both by combining step 1 and 2.
String s = new String (“Hello World”);
s. (—–methods…
- —–method 2
- —–method 3
- —–method 4…)
What happens to the memory space and object that doesn’t have an ARROW pointing to it?
That object becomes an ORPHAN, and it needs to be taken out like GARBAGE.
Have controls/documentation in place to stop this from happening. Garbage will not modify your code.
String is the only object that allows you to create WITHOUT using NEW. (primitive types are not objects).
title = “Star Wars”;
What does it mean when we say a string is immutable?
A string is immutable because you cannot change it once it’s created. BUT you can create new string from it.
How do we replace the object with a new string?
String s = new String("Hello World"); s = s.replace( 'l' , 'p' );
What do we assume about the following initialization?
String phrase = “Change is Inevitable”;
String phrase = new String(“Change is Inevitable”);
For Strings Java allows us to exclude new String();
Where is the method in this code?
String phrase = “Change is Inevitable”;
String mutation 1, mutation2, mutation3, mutation4;
mutation1 = phrase.concat (“,except from vending machines.”);
Right after the phase, where we say .concat();
.Concat is one of the many methods of the String Class.
Do the methods belong to the String Class or String Object?
The methods come from the String Object.
How can we compare two strings to see if they are equal?
Can we use if ( a1 == a2); ?
we use if (a1.equal(a2));
The == sign can only be used to compare two PRIMITIVE types. Everything else needs a method.