Java Flashcards
What did you learn about static functions?
static signature makes the function unique. That is, we don’t need to create an instance of the class in order to use the function. We can simply call <classname>.<functionname>.</functionname></classname>
There’s a big difference between j = i-1; and j=i–;
What is it?
i– changes the value of i. i-1 does not.
Is it possible to use macros in java?
It is, not recommended though.
Using macros is using the C pre-processor. We can use macros, then run the C pre-processor, through the command line, on our code to generate the required code to be compiled.
What does static mean in Java?
it means that you can call a method, even if you’ve never created the object to which it belongs
What does <> mean in java
- It is placed after “public static” with some capital letter between ‘<>’, like <t>, and it signals a <strong>generic function</strong>. That is, the function can get any <strong>object.</strong></t>*
- For instance,*
- Public static <t> T[] justReturn(T[] arr){</t>*
- return arr;*
- }*
What is the difference between extends and implements?
extends is for extending a class
implement is for implementing an interface
What does serialization mean?
points to explain - marker interface, serialize, deserialized, serial version UID
If we implement Serializable it means we tell the java serialization classes that this class is intended for serialization. That is, intended for writing(serialization) and reading(deserialization) of objects.
java.io.Serializable - is the Java interface. It is called a marker interface since we don’t need to implement any method.
Only thing we should include is a private static final long serialVersionUID, which signifies the current “version” of our class, so that if we wrote some object to disk, and then changed the class, when we readback, it knows it now reads to a different class.
What does transient mean?
It is a keyboard which marks a memeber variable not to be serialized. They will not be saved using serialization; they are lost intentionally.
Explain the comparable interface
- We implement the Comparable interface when we want to compare between objects.*
- This interface has only one method, with the signature: int compareTo(obj o)*
Describe the super keyboard
Extending class uses the super keyboard for calling a method/variable of the parent class(a method they both share, of course) or initiate the constructor of the parent class.
Note that the constructor of the base class is called automatically
NOTE: when a class uses some method/variable, the compiler first looks within its fields/methods. If the name is not found, it searches in the parent’s class. If not found in the parent’s class, it goes to the parent of the parent’s class and so on.
Describe this keyboard
It is a keyboard used to refer to the instance of the object:
- using it we can call the constructor; this(..);
- using it we can the object’s methods, this.method();
- using it we can refer to the object’s fields: this.field;
- using it we can give the instance of the object as a method parameter; display(this);
- using it we can give the instance of the object as a parameter to a constructor; A a = new A(this);
- using it we can return the current class instance; return this;
What is the need of instance of?
You may have several classes extending some parent class, and you want to have a function which takes as argument an object which extends the parent class, and operate according to the class which was given. The argument is of course of the type ParentClass p, so that the extending classes could be passed as parameters. How can we identify between all these classes? using instanceof.
if(p instanceof <someextendingclass>)</someextendingclass>
do something