Lecture 1 Flashcards
What are primitive types (mention some)?
Types that are already defined by the program (int, float, double, char, boolean)
What are non-primitive types (mention some)?
Types that the programmer defines for the program (arrays, strings, classes)
Primitive types:
Describe whole numbers and decimal numbers
Whole numbers: long, byte, int, short
Decimal numbers: float, double (default)
What is a value literal?
The letter at the end of a variable initialisation:
float 1.2f (without f, it will be interpreted as double)
double 1.6 or 1.6d (interpreted as double, since it’s default (d optional))
What is a local variable?
Variables that are declared inside the body of a method
class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}
What is an instance variable?
Defined without the static keyword. They are defined outside a method declaration and are object specific
class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}
What is a static variable?
Initialised only once, at the start of the program execution. They should be initialised first (before any instance variables)
class Guru99 {
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}
What does null reference mean?
null (used for non-primitive types) will reference to nowhere, since it has not been set to target a proper element
Write the skeleton of an “if” statement.
boolean condition = true; // has to be true or false
// …
if (condition) {
// … happens if true (then block, mandatory)
} else {
// … happens if false (else block, optional)
}
Write the skeleton of a “while” statement.
boolean condition = true;
while (condition) {
// Perform operations repeatedly
// Modify condition to eventually end loop
}
(if the condition is initially false, it will not execute, and will repeat as long as the condition holds)
Write the skeleton of a “for” loop statement. What does the (initialiser, condition, increment/decrement) (IN THAT ORDER) do?
e.g.:
for (int i = 0; i < 10; i++) {
// Perform operation ten times
}
Initialiser: one time before the first loop
Condition: checked every time before loop
Increment/Decrement: every time
after loop
Write the skeleton of a “for each” loop
for (type variableName : arrayName) {
// code block to be executed
}
Can you use “==” (equality operator) on Strings?
NO. Instead use “equals()”.
Example:
if (message.equals(“Hello World”)){
//…
}
What is Class-based OOP and what does it contain?
You have a Class, that Class contains Data and Actions on that Data. Then you have an Object that is an Instance of the whole Class
What is a Class?
Represents many objects that belong to the Class. Will group data and actions on that data for data which logically belong closely together (high coherence)
What is an object (instance of a class)?vend tilbage
Is the property of a specific element. A class is the super, and an object is inside of the class.