Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,...

Java Interview Preparation

https://blog.udemy.com/java-interview-questions/

1- What is JVM? Why is Java called the ‘Platform Independent Programming Language’?

2. What is the Difference between JDK and JRE?

3. What does the ‘static’ keyword mean?

4. What are the Data Types supported by Java? What is Autoboxing and Unboxing?

5. What is the difference between STRINGBUFFER and STRING?

6. What is Function Over-Riding and Over-Loading in Java?

7. What is Constructors, Constructor Overloading in Java and Copy-Constructor?

8. What is Java Exception Handling? What is the difference between Errors, Unchecked Exception and Checked Exception?

Throwable
      Error (serious problems that a reasonable application should not try to catch, out of memory etc.)
      Exception
            Checked / Unchecked

10. What is the Difference between byte stream and Character streams?
A stream is a way of sequentially accessing a file. A byte stream access the file byte by byte. A byte stream is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself.

A character stream will read a file character by character. A character stream needs to be given the file's encoding in order to work properly.
Example: FileInputStream reads bytes, FileReader reads characters.
13. What is the difference between ArrayList and LinkedList ?
LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but finding a position in the list takes time proportional to the size of the list. Javadoc says "operations that index into the list will traverse the list from the beginning or the end, whichever is closer"
ArrayList, on the other hand, allow fast random read access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap. Also, if you add more elements than the capacity of the underlying array, a new array (1.5 times the size) is allocated, and the old array is copied to the new one, so adding to an ArrayList is O(n) in the worst case but constant on average.
http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist
14. Explain the difference between ITERATOR AND ENUMERATION INTERFACE with example.
  • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  • Method names have been improved.

Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

Virtual Memory

NOTES ON COMPUTER ARCHITECTURE: Some important concepts in computer architecture