More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. There is no concept of de referencing with primitive types and hence they are implicitly immutable. Having wrapper classes as mutable offers disadvantages compared to primitive types. Wrapper classes being immutable offer similar advantage as primitive types.It actually overshadows the disadvantage wrapper class could have if they are immutable.
Help us improve. Please let us know the company, where you were asked this question :
Ans. When we create reference for primitive type, it's memory is allocated. So even if we don't assign any value to it, the default value is initialized.
int x; // default value 0 initialized
But for object references, it's different as references hold nothing till an object is assigned.
Object obj; // contains null
So all primitive types when declared , contains their respective default values on the basis of their type whereas all wrapper class References contains null irrespective of their types.
Help us improve. Please let us know the company, where you were asked this question :
Q430. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. streams are used for sequential processing of data and parallel streams are used for parallel processing of data (only if the underlying processor is multicore).
Help us improve. Please let us know the company, where you were asked this question :
Ans. Any function call is slower than the code in the same method as Java has to maintain stack of meta and function call information. So recursion is slower.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Because String being an immutable object creates a new object upon each concatenation cycle. If there is any such need , we should use String Builder whose objects are mutable.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Because it doesn't make the change in the existing string but would create a new string by concatenating the new string to previous string. So Original string won't get changed but a new string will be created. That is why when we say
str1.concat("Hello");
It means nothing because we haven't specified the reference to the new string and we have no way to access the new concatenated string. Accessing str1 with the above code will still give the original string.
Help us improve. Please let us know the company, where you were asked this question :
Object Oriented and hence helps build maintainable and easily enhanceable code.
Platform Independence and hence can be executed anywhere
Fast due to JIT compiler
Help us improve. Please let us know the company, where you were asked this question :
Q448. Lets say we have a set "set1" with objects of Class A, Class has a non static string called element1, Write a code ( using lambda expression ) to see if there is any object in the set with element1 = "Hello".