31-40 Flashcards
Is string a data type in Java?
String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object.
Why Strings in Java are called as Immutable?
In java, string objects are called immutable as once value has been assigned to a string, it can’t be changed and if changed, a new object is created.
What’s the difference between an array and Vector?
An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types.
What is multi-threading?
Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program.
Why Runnable Interface is used in Java?
Runnable interface is used in java for implementing multi threaded applications. Java.Lang.Runnable interface is implemented by a class to support multi threading.
What are the two ways of implementing multi-threading in Java?
- By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented.
- By writing a class that extend Java.Lang.Thread class.
When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?
Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it’s always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead.
What’s the purpose of using Break in each case of Switch Statement?
Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn’t flow in the proceeding cases too.
If break isn’t used after each case, all cases after the valid case also get executed resulting in wrong results.
How garbage collection is done in Java?
In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.