JAVA TERM 1
53 flashcards · Programming Languages · by inquisitive learner
block
a sequence of statements, local class declarations and local variable declarations
How does a block act?
A block acts as a single statement
==
equal to (relational operator)
!=
not equal to (relational operator)
>
greater than relational operator
<
"less than" relational operator
>=
"greater than or equal to" relational operator
<=
"less than or equal to" relational operator
A relational expression returns a result of type Boolean
!
"not" combinatory relational operator
||
"or" combinatory relational operator
&&
"and" combinatory relational operator
if-else syntax (Allman style)
if (\\relational expression) { // statement or block } else { // statement or block }
if-else multi-way selection
if (\\relational expression) { // statement or block } else if (relational expression) // as needed { // statement or block } else { // statement or block }
switch-case selection statement
switch (exampleexpression) { case 'literal constant': // block or statement }
fall-through
(in switch-case selection statements) compiler executes the next statement if there is no break keyword; control flow 'falls-through' to the next case
default
Java keyword: in switch-case statements, this is the statement the compiler will execute if none of the previous cases match the conditions
break
Java keyword: causes the remainder of the switch-case statement to be skipped
What is an advantage of fall-through?
When multiple cases produce the same output, fall-through allows us to avoid redundant code
Switch-case statements should be mutually exclusive and exhaustive.
Punctuation in java ; marks the end of a statement
inheritance
the ability to derive a new class from an existing one. Inherited variables and methods of the original (parent) class are available as if they were declared locally
parent class
the class from which variables and methods are derived
subclass
the derived class
superclass
the parent class
Inheritance should be used to model an "is a" relationship. A subclass represents a specialised version of the superclass
accessor method getPages() // example
An accessor method provides read-only access to a particular value
extends
defines a subclass as being derived from its superclass
instance variable (aka attributes, Data members)
A variable that must be referenced through a particular instance of a class, as opposed to a class variable; defines the state of an object
Differences between instance variables and class variables Class variables are declared with the static keyword; instance variables are not Instance variables are specific to an object - they have a more limited scope - whereas class variables are shared across all occurrences | instances of a class The class name is used to access a class variable; instance variables are accessed directly within the class
declaration, scope, access
What should you do if the behaviour of a parent class is unsuitable for the subclass?
override the superclass's behaviour by providing a method with the same signature as a superclass method
Why is using inheritance good for your programme?
Inheritance promotes reuse
An object has: state - descriptive characteristics e.g each String object contains a group of specific characters in a particular order behaviours - what it can do (or be done to it) e.g toUpperCase method is a service the String object can perform.
class
the blueprint for creating objects defines a new data type
method (aka operations, member functions)
a named group of declarations and programming states that can be invoked (executed when needed); define behaviour
object (3)
the primary software construct in an object-oriented paradigm an encapsulated collection of data variables and methods an instance of a class
cohesion
the strength of the relationship among the parts of a software component
abstraction
the concept of hiding details , e.g through a class
coupling
the strength of the relationship between two software components. Ideally, programs are loosely coupled
private
Java reserved keyword; method or instance variable that can only be accessed by methods of the same class
public
can be accessed by external code
Visibility modifiers public private protected
package
a group of classes
How visible is a member declared without a visibility modifier?
Members declared without a visibility modifier have default visibility
support method
a method created to assist a service method; should be declared with private visibility
Which classes can access a member with default visibility?
Members with default visibility can be accessed by any class in the same package
Java convention for naming classes
Class names start with a capital letter and use camel case
Variable names start with lower case and use camel case and should be informative
Relational operators are boolean operators: they result in either true| false or false | true
Relational operators: == !=
When is it best to use a for loop?
When looping a fixed number of times When iterating through collections of objects
for loop
initialisation, termination, decrement
When is it best to use a do-while loop?
A do-while loop is best for when code must always be executed at least once
When is it best to use a while loop?
a while loop is best for when unmet conditions should result in unexecuted code