Lectures Flashcards
(77 cards)
Where to declare the result of the method?
When the method is created the type of the result is declared.
The result value is passed with the return keyword.
What does it mean that Java is statically typed?
The type of a variable is explicitly defined at compile time.
What does it mean that Java is strongly typed?
The compiler gives an error when types do not match.
How to iterate over each element in a list?
List numbers = new ArrayList<>;
for (Interger number : numbers) {…}
What are the values of byte type?
-128 until 127
8 bits
What are the values of short type?
-32768 until 32767
16 bits
What are the values of int type?
-2^31 until 2^31-1
32 bits
What are the values of long type?
-2^63 until 2^63-1
64 bits
What are the values of float type?
Real number approximation.
32 bits
What are the values of double type?
More precise real number approximation
64 bits
What are the values of char type?
A single Unicode character
16 bits
What are the values of boolean type?
True or False
When can you convert a primitive variable to a different type?
Automatic conversion is only possible if the conversion goes from a more specific to a more general type.
What is a static method?
We can write static methods within a class, but we are not allowed to use the current or local instance variables. Only instance variables of objects of whom the reference was passed as an argument can be accessed.
You are never allowed to use this keyword, for example.
public static A_combine(A object1, A object2)
What is “this” keyword?
This allows to explicitly refer the current or local instance variables.
How to indicate that no object was found while executing a method?
Use the null reference.
What is a null reference?
The null reference is used to indicate that there is no object.
What is the default value of a instance variable of a non-primitive type?
Instance variables with a non-primitive type always get null as the default value.
What happens if we try to call a method on a variable that is null?
will get a NullPointerExceptionand the program will stop.•
How to generate a random numbers?
import java.util.Random;
Random ran = new Random(1234); int myDie = 1+ran.nextInt(6);
What is a random seed?
import java.util.Random;
Random ran = new Random(1234);
The argument 1234is called the random seed. You can use any number here. It fixes the random sequence to be generated (so 1234gives one random sequence, 5231another).It is useful to fix the random seed so the results of your run becomes reproducible. If you want to try how your program works with a different random sequence, just change the seed.
What are different types of errors in Java?
1. Compiler Syntax Errors Type errrs Structural errors (example with return) 2. Runtime errors 3. Logical errors
What are different types of errors in Java?
1. Compiler Syntax Errors Type errors Structural errors (example with return) 2. Runtime errors Gives an exception 3. Logical errors The program executes, but you get a wrong answer
How to raise an exceptions?
“throw” keyword
if (condition) { throw new TATATAException("Something went wrong") }