You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- An interface is like a blueprint/contract of a class (or it may be thought of as a class with methods, but without their implementation). It contains empty methods that
265
265
represent, what all of its subclasses should have in common. The subclasses provide the
266
266
implementation for each of these methods. Interfaces are implemented.
267
-
* What is serialization? How do you implement it?
268
-
- Serialization is the process of converting an object into a stream of bytes in order to store
269
-
an object into memory, so that it can be recreated at a later time, while still keeping the
270
-
object's original state and data. In Android you may use either the Serializable, Externalizable (implements Serializable) or Parcelable interfaces.
271
-
- While Serializable is the easiest to implement, Externalizable may be used if you need to insert custom logic into the process of serialization (although it is almost never used nowadays as it is considered a relic from early versions of Java). But it is highly recommended to use Parcelable in Android instead, as Parcelable was created exclusively for Android and it performs about 10x faster than Serializable, because Serializable uses reflection, which is a slow process and tends to create a lot of temporary objects and it may cause garbage collection to occur more often.
272
-
- To use Serializable all you have to do is implement the interface:
273
-
274
-
```java
275
-
/**
276
-
* Implementing the Serializeable interface is all that is required
277
-
*/
278
-
publicclassUserimplementsSerializable {
279
-
280
-
privateString name;
281
-
privateString email;
282
-
283
-
publicUser() {
284
-
}
285
-
286
-
publicStringgetName() {
287
-
return name;
288
-
}
289
-
290
-
publicvoidsetName(finalStringname) {
291
-
this.name = name;
292
-
}
293
-
294
-
publicStringgetEmail() {
295
-
return email;
296
-
}
297
-
298
-
publicvoidsetEmail(finalStringemail) {
299
-
this.email = email;
300
-
}
301
-
}
302
-
```
303
-
-Parcelable requires a bit more work:
304
-
```java
305
-
publicclassUserimplementsParcelable {
306
-
307
-
privateString name;
308
-
privateString email;
309
-
310
-
/**
311
-
* Interface that must be implemented and provided as a public CREATOR field
312
-
* that generates instances of your Parcelable class from a Parcel.
Note:For a full explanation of the <b>describeContents()</b> method see [StackOverflow](https://stackoverflow.com/questions/4076946/parcelable-where-when-is-describecontents-used/4914799#4914799).
380
-
InAndroidStudio, you can have all of the parcelable code auto generated for you, but like with everything else, it is always a good thing to try and understand everything that is happening.
381
-
*What are anonymous classes?[OracleDoc](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
382
-
*What is the difference between using `==` and `.equals` on a string?[GeeksForGeeks](http://www.geeksforgeeks.org/difference-equals-method-java/)
383
-
*How is `String` classimplemented? Why was it made immutable?
384
-
- There is no primitive variant of `String` classin Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
385
-
> Note: The String classis immutable, so that once it is created a String object cannot be changed. The String classhas a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
386
-
387
-
This classis also unique in a sense, that, when you create an instance like this:
388
-
```java
389
-
String helloWorld = "Hello, World!";
390
-
```
391
-
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
392
-
```java
393
-
String capital = "Hello, World!".toUpperCase();
394
-
```
395
-
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
396
-
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
397
-
* What does it means to say that a `String` is immutable?
398
-
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
399
-
* Do you think memory leak in java?
400
-
* What is `String.intern()`? When and why should it be used?
401
-
* What is the `hashCode()` and `equals()` used for?
402
-
* Can you list 8 primitive types in java?
403
-
* Why would you not call abstract method in constructor?
267
+
404
268
* What is the difference between iterator and enumeration in java?
405
-
* Do you agree we use composition over inheritance? ([Composition vs Inheritance)] [https://www.journaldev.com/12086/composition-vs-inheritance])
406
-
* When would you make an object value final?
407
-
* What are these `final`, `finally` and `finalize` keywords?
408
-
- `final` is a keyword in the java language. It is used to apply restrictions on class, method and variable. Final classcan't be inherited, final method can't be overridden and final variable value can't be changed.
409
-
```java
410
-
classFinalExample {
411
-
publicstaticvoidmain(String[] args) {
412
-
finalint x=100;
413
-
x=200;//Compile Time Error because x is final
414
-
}
415
-
}
416
-
```
417
-
- `finally` is a code block and is used to place important code, it will be executed whether exception is handled or not.
418
-
```java
419
-
classFinallyExample {
420
-
publicstaticvoidmain(String[] args) {
421
-
try {
422
-
int x=300;
423
-
}catch(Exception e) {
424
-
System.out.println(e);
425
-
}
426
-
finally {
427
-
System.out.println("finally block is executed");
428
-
}
429
-
}
430
-
}
431
-
```
432
-
- `Finalize` is a method used to perform clean up processing just before object is garbage collected.
433
-
```java
434
-
classFinalizeExample {
435
-
publicvoidfinalize() {
436
-
System.out.println("finalize called");
437
-
}
438
-
439
-
publicstaticvoidmain(String[] args) {
440
-
FinalizeExample f1=newFinalizeExample();
441
-
FinalizeExample f2=newFinalizeExample();
442
-
f1=null;
443
-
f2=null;
444
-
System.gc();
445
-
}
446
-
}
447
-
```
448
-
*What is garbage collector?How does it work?
449
-
-All objects are allocated on the heap area managed by the JVM.
450
-
Aslong as an object is being referenced, the JVM considers it alive.
451
-
Once an object is no longer referenced and therefore is not reachable by the application code,
452
-
the garbage collector removes it and reclaims the unused memory.
453
-
* `Arrays` vs `ArrayLists`.
454
-
* `HashSet` vs `TreeSet`.
455
-
*Typecast in Java.
456
-
-InJava, you can use casts to polymorph one classinto another, compatible one. For example:
457
-
```java
458
-
long i = 10l;
459
-
int j = (int) i;
460
-
long k = j;
461
-
```
462
-
Here we see, that, while narrowing (`long i` -> `int j`) requires an explicit cast to make sure the programmer realizes, that there may be some data or precision loss, widening (`int j` -> `long k`) does not require an explicit cast, because there can be no data loss (`long` can take larger numbers than `int` allows).
269
+
270
+
* Do you agree we use composition over inheritance? [Composition vs Inheritance](https://www.journaldev.com/12086/composition-vs-inheritance)
271
+
463
272
* Difference between method overloading and overriding.
464
273
<palign="center">
465
274
<imgalt="Overloading and Overriding"src="https://github.com/codeshef/android-interview-questions/blob/master/assets/overloading-vs-overriding.png">
@@ -573,28 +382,6 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
* How is `String` classimplemented? Why was it made immutable?
579
-
- There is no primitive variant of `String` classin Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
580
-
> Note: The String classis immutable, so that once it is created a String object cannot be changed. The String classhas a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
581
-
582
-
This classis also unique in a sense, that, when you create an instance like this:
583
-
```java
584
-
String helloWorld = "Hello, World!";
585
-
```
586
-
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
587
-
```java
588
-
String capital = "Hello, World!".toUpperCase();
589
-
```
590
-
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
591
-
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
592
-
593
-
* What does it means to say that a `String` is immutable?
594
-
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
595
-
596
-
* What is `String.intern()`? When and why should it be used?
597
-
598
385
#### Collections and Generics
599
386
600
387
* `Arrays` vs `ArrayLists`.
@@ -617,6 +404,28 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
617
404
618
405
#### Objects and Primitives
619
406
407
+
#### String
408
+
409
+
* How is `String` classimplemented? Why was it made immutable?
410
+
- There is no primitive variant of `String` classin Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
411
+
> Note: The String classis immutable, so that once it is created a String object cannot be changed. The String classhas a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
412
+
413
+
This classis also unique in a sense, that, when you create an instance like this:
414
+
```java
415
+
String helloWorld = "Hello, World!";
416
+
```
417
+
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
418
+
```java
419
+
String capital = "Hello, World!".toUpperCase();
420
+
```
421
+
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
422
+
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
423
+
424
+
* What does it means to say that a `String` is immutable?
425
+
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
426
+
427
+
* What is `String.intern()`? When and why should it be used?
428
+
620
429
* Can you list 8 primitive types in java?
621
430
622
431
* What is the difference between an Integer and int?
@@ -865,6 +674,10 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
865
674
866
675
* Why would you not call abstract method in constructor?
867
676
677
+
* What are anonymous classes?[OracleDoc](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
678
+
679
+
* Can you list 8 primitive types in java?
680
+
868
681
* When would you make an object value `final`?
869
682
870
683
* What are these `final`, `finally` and `finalize` keywords?
@@ -909,6 +722,7 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
909
722
}
910
723
```
911
724
725
+
912
726
*What does the `static` word mean in Java?
913
727
-Incase of `static` variable it means that this variable (its' value or the object it references) spans across all instances of enclosing class (changing it in one instance affects all others), while in case of `static` methods it means that these methods can be invoked without an instance of their enclosing class. It is useful, for example, when you create util classes that need not be instantiated every time you want to use them.
0 commit comments