1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What happens when we have an else
/else if
without curly braces?
Dangling else problem
else
will default to the last if
that came before it
Code may not execute if else was meant for the first if
in the block, and several if
’s came after it
What is the issue of going from long
→ int
and how can it be overcome?
long
is more precise than int
Could lead to a lossy conversion where data is lost and might be less precise
Requires a change in return type or an explicit cast
Where can’t switch
statements be used?
Where there are no discrete values present (e.g. case is an inequality or a range)
What is the process of dealing with a Q2/3-type programming problem?
Understand the prompt — identify nouns and verbs, and what concept they relate to
Split the task into several steps, input → output → calculation
Simplify using variables and arrows
Think about how they can be implemented in Java
How should we deal with fields implementing data structures in classes?
Declare the field in the class
Do not pass it in as a parameter in the constructor, but initialise it within the constructor block
class ClassName {
List<E> list
int p1;
int p2;
public ClassName(int p1, int p2) {
this.p1 = p1;
this.p2 = p2;
this.list = new ArrayList<>();
}
// ...
}
How should abstract classes and methods be implemented?
Declare the method as abstract
but do not implement it
Inside concrete class, implement the method using @Override
notation (without calling it abstract)
How should checked and unchecked exception classes be handled?
Unchecked → exception is a subclass of RuntimeException
Checked → exception is a subclass of Exception
How should exception-throwing methods be declared?
public [ReturnType] method throws [ExceptionName](…) { … }
throws
MUST be there if the method is throwing the exception
What is the format of a generic class?
Uses <T>
to show that it is a generic class
class ClassName<T> { … }
What would the generic class look like if the generic type can only be subclasses of another type?
Using List<?>
as an example:
class ClassName<T extends List<?>> { … }
How are interfaces implemented?
Interface only stores the method signature (no body)
@Override
notation when we implement the methods defined in the interface
Included in the class:
public class [ClassName] implements [InterfaceName] { … }
When is @Override
notation used?
Either when we implement a method from an interface, or we are overriding a method from a superclass