Java-Specific Language Features Flashcards
prep code
A form of pseudocode, to help you focus on the logic without stressing about syntax
- Instance variable declarations
- Method declarations
- Method logic
Testing code
A class or methods that will test the real code and validate that it’s doing the right thing
Real code
The actual implementation of the class.
Coding strategy
- Figure out what the class is supposed to do
- List the instance variables and method
- Write prepcode for the methods
- Write test code for the methods
- Implement the class
- Test the methods
- Debug and reimplement as needed
- Express gratitude that we don’t have to test our learning experience app on actual live users
What happens in Integer.parseInt() if the thing you pass isn’t a number? And does it recognize spelled-out numbers, like “three”?
Integer.parseInt() works only on Strings that represent the ascii values for digits (0, 1, 2,3, 4, 5, 6, 7, 8, 9). If you try to parse something like “two”, the code will blow up at runtime
Difference between for and while
A while loop has only the boolean test; it doesn’t have a built-in initialization or iteration.
If you know how many times to loop, a for loop is cleaner
Enhance for loop
Easier to iterate over all the elements in an array
Casting primitives
A long is bigger than an int and the compiler can’t be sure where that long has been
ArrayList vs. Array
- A plain old array has to know its size at the time it’s created
- Arrays use array syntax that’ s not used anywhere else in Java
- To put an object in a regular array, you must assign it to a specific location
Non Short Circuit Operators (&, |)
When used in boolean expressions, the & and | operators act like their && and “” counterparts, except that they force the JVM to always check both sides of the expression
Package
- Help the overall organization of a project or library
- Give you a name-scoping, to help prevent collisions if you and 12 other programmers in your company all decide to make a class with the same name
- Provide a level of security, because you can restrict the code you write so that only other classes in the same package can access it
How does a full-name really help package? What’s to prevent two people from giving a class the same package name?
Java has a naming convention that usually prevents this from happening, as long as developers adhere to it
Does import make my class bigger? Does it actually compile the imported class or package into my code?
- Import only saves you from typing
2. Import is simply the way you give Java the full name of a class
How come I never had to import the String class?
Because java.lang package sort of “pre-imported” for free
Inheritance
- The subclass inherits from the superclass
2. Subclass extends the superclass
Subclass
- The subclass can add new methods and instance variables of its own, and it can override the methods it inherits from the superclass
- A subclass can only extends from one superclass