Java-Specific Language Features Flashcards

1
Q

prep code

A

A form of pseudocode, to help you focus on the logic without stressing about syntax

  1. Instance variable declarations
  2. Method declarations
  3. Method logic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Testing code

A

A class or methods that will test the real code and validate that it’s doing the right thing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Real code

A

The actual implementation of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Coding strategy

A
  1. Figure out what the class is supposed to do
  2. List the instance variables and method
  3. Write prepcode for the methods
  4. Write test code for the methods
  5. Implement the class
  6. Test the methods
  7. Debug and reimplement as needed
  8. Express gratitude that we don’t have to test our learning experience app on actual live users
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens in Integer.parseInt() if the thing you pass isn’t a number? And does it recognize spelled-out numbers, like “three”?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between for and while

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Enhance for loop

A

Easier to iterate over all the elements in an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Casting primitives

A

A long is bigger than an int and the compiler can’t be sure where that long has been

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ArrayList vs. Array

A
  1. A plain old array has to know its size at the time it’s created
  2. Arrays use array syntax that’ s not used anywhere else in Java
  3. To put an object in a regular array, you must assign it to a specific location
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Non Short Circuit Operators (&, |)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Package

A
  1. Help the overall organization of a project or library
  2. 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
  3. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does a full-name really help package? What’s to prevent two people from giving a class the same package name?

A

Java has a naming convention that usually prevents this from happening, as long as developers adhere to it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does import make my class bigger? Does it actually compile the imported class or package into my code?

A
  1. Import only saves you from typing

2. Import is simply the way you give Java the full name of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How come I never had to import the String class?

A

Because java.lang package sort of “pre-imported” for free

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Inheritance

A
  1. The subclass inherits from the superclass

2. Subclass extends the superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Subclass

A
  1. The subclass can add new methods and instance variables of its own, and it can override the methods it inherits from the superclass
  2. A subclass can only extends from one superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why isn’t Instance variable overriden

A

Because they don’t need to be. They don’t define any special behaviors, so a subclass can give an inherited instance variable any value it chooses

18
Q

What if the superclass wants to use the subclass version of the method?

A

No, there’s no sort of reverse or backward inheritance

19
Q

What if I want to use BOTH the superclass version and my overriding subclass version of a method?

A

we can do so using super.method(), which calls the inherited version, then comes back to do your own subclass-specific code

20
Q

Are there any practical limits on the levels of subclassing? How deep can you go?

A

Most are no more than one or two levels deep. It usually makes more sense to keep your inheritance trees shallow

21
Q

Can you make a method final, without making the whole class final?

A

If you want to protect a specific method from being overridden, mark the method with the final modifier.

Mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden

22
Q

Why would you ever want to make a final class?

A

A final class prevent from overriden

23
Q

Overload method

A

A different method that happens to have the same method name. It has nothing to do with inheritance and polymorphism

24
Q

Concrete class

A

Those that are specific enough to be instantiated

25
Q

Abstract method

A
  1. No use, no value, no purpose in life, unless it is extended
  2. Must be overridden
  3. Implementing an abstract method is just like overriding a method
26
Q

Abstract class

A
  1. You can mix both abstract and non-abstract methods in the abstract class
27
Q

Abstract vs. Inheritance

A

Abstract method is that even though you haven’t put in any actual method, you’ve still defined part of the protocol for a group of subtypes

28
Q

Is class Object abstract?

A

No. Object is a non-abstract class because it’s got method implementation code that all class can inherit and use out-of-the-box, without having to override the mehtods

29
Q

Compiler

A

Check the class of the reference type-not the object type-to see if you can call a method using that reference

30
Q

Polymorphism

A

Many form

31
Q

Interface

A
  1. Solve multiple inheritance problem by giving you much of the polymorphic benefits of multiple inheritance without the pain
  2. A class can implement multiple interfaces
  3. A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract
32
Q

Super

A

The keyword super lets you invoke a superclass version of an overridden method, from within the subclass

33
Q

Stack

A
  1. Where method invocations and local variables live

2. The method on the top of the stack is always the currently executing method

34
Q

Heap

A

Where all objects live

35
Q

Instance variables

A

Declared inside a class but no inside a method. They represent the “fields” that each individual object has

36
Q

Local variables

A

Declared inside a method, including method parameters. They’re temporary, and live only as long as the method is on the stack

37
Q

Local variables

A

Declared inside a method, including method parameters. They’re temporary, and live only as long as the method is on the stack

38
Q

Constructor

A

The code that runs when you instantiate an object

39
Q

Are constructors inherited? If you don’t provide a constructor but your superclass does do you get the superclass constructor instead of the default?

A

No. Constructors are not inherited. We’ll look at that in just a few pages

40
Q

“this” method

A
  1. Use this() to call a constructor from another overloaded constructor in the same class
  2. A constructor can have a call to super() OR this(), not both
  3. this() can be used only in a constructor
41
Q

“this” method

A
  1. Use this() to call a constructor from another overloaded constructor in the same class
  2. A constructor can have a call to super() OR this(), not both
  3. this() can be used only in a constructor
42
Q

Reference variables

A

Can be used only when it’s in scope