shilpigupta22

shilpigupta22

Credit: 3870
Favorite: 0
Register at: 4 months ago
shilpigupta22 questions
shilpigupta22 4 months ago
shilpigupta22 4 months ago
shilpigupta22 4 months ago
shilpigupta22 4 months ago
shilpigupta22 Questions of answers
shilpigupta22 4 months ago Answer question What are Reflections?

Reflections are a mechanism in Java allowing to inspect or modify the application code at runtime. One of the most common usage of the reflection API is retrieving information for a certain class at runtime. Some runtime information that can be retrieved for a class is: class name, fields, methods, super classes, implemented interfaces etc. The usage of the Reflection API adds some performance overhead, so the usage of it should be limited, and if possible, avoided.

shilpigupta22 4 months ago Answer question What does the following code usually output? And why usually?

Output:

true,

true,

true,

false

With casting the values (Integer), a boxing of the primitive values to the object wrapper is done. Since this boxing has some performance impact, the JVM keeps a cache of the object wrappers in some range (for Integer the default range is [-127, 127]). This means that in the example (Integer)127 == (Integer)127 , the wrapper object for 127 is taken from the cache and that's why they are same by reference. In the example (Integer)128 == (Integer)128 , the value is false because the wrapper object is not cached (it is out of the default range), and the result is false because they are different by reference.

This is the usual case because the range is a JVM property that can be changed.

shilpigupta22 4 months ago Answer question Where would you use LinkedList and where an ArrayList?
  • LinkedList is used in cases where we have a lot of add, remove and sequential iteration but not a lot of random access ( list.get(n) )
  • ArrayList is used when there is a lot of random access used ( list.get(n) ), but not a lot of add, remove.
shilpigupta22 4 months ago Answer question Identify the Autoboxing and the Unboxing in the following example:

list.add(1L) causes autoboxing

Long l = list.get(0) causes unboxing

shilpigupta22 4 months ago Answer question What is Auto-boxing and Unboxing, and does it impact performance?

Autoboxing is the process of automatic conversion (by the Java compiler) from the primitive type to the corresponding object type (wrapper class of the primitive type). Unboxing is the reverse process, the conversion from the object type to the primitive type. Some primitive-object type pairs are (int, Integer), (long, Long), (double, Double) etc.

Since there's additional work required to execute them, both autoboxing and unboxing have an impact on the performance, so the developer should be careful and identify the points where autoboxing/unboxig happens to take care of some optimizations if needed.

shilpigupta22 4 months ago Answer question Primitive vs Object types?
  • Primitive types are the most basic types that Java supports. They are predefined as reserved keywords by the language and use a small amount of memory to represent a unit of data. Some primitive types are int, long, byte, double etc.
  • The other Object types are types that have classes (which extend Object.class) and can be instantiated with the new keyword (if there's an appropriate constructor). The object types are usually complex types, a composition of properties and methods that take up a bigger chunk of the memory.
shilpigupta22 4 months ago Answer question What is the parent class that every other class extends from?

The parent class of every other class is the Object.class.

shilpigupta22 4 months ago Answer question Why is Java said to be 'write once, run everywhere'?

Java is said to be 'write once, run everywhere' thanks to the bytecode which acts as the intermediate step between the source code and the machine code. This allows the Java Bytecode to be executed on any hardware that has a proper JVM installed.

shilpigupta22 4 months ago Answer question What are static methods and fields?

Static methods and fields are class members. This means that they belong to the class, and not to an instance of the class. So, an instance of the class is not needed to gain access to these static members. The static fields are usually used for constants and the static methods for utility classes.

shilpigupta22 4 months ago Answer question What is JPA and how is it different from JDBC?

JPA stands for Java Persistence API and represents an API for managing relational data in the application by mapping operations between Java objects and some relational database. The difference from JDBC is that JDBC defines how the client connects to the DBMS, while JPA defines how the data is managed.

shilpigupta22 4 months ago Answer question What is the difference between an Application server and servlet container?

The application server provides the whole JavaEE support, while the servlet container provides only the Servlet specification. The servlet container (most commonly used implementation is Tomcat) can also run some of the JavaEE technologies if they are installed separately or with some framework providing some of them.

shilpigupta22 4 months ago Answer question Given the following example code, which overrides are valid and which one aren't (with explanation)?

Overrides of m1() and m3() are valid. In the case of m1(), the override in C2 defines an Exception which is a sub-class of the one declared on the parent. This is defining a more precise exception than the parent, and it's completely legal. In the second case, m2() throws a more general exception, and this is an illegal override.

shilpigupta22 4 months ago Answer question What types of Exceptions exist in Java (and what is the difference)? Name few common Exceptions of each type.

In Java there are 2 type of Exceptions, checked and unchecked.

The checked exceptions are checked at compile and they must be either handled in a catch block, or they must be specified in the throws section of the method. The checked exceptions are subclasses of the Exceptions class and some of the most common exceptions of this type are: IOException, SQLException, ClassNotFoundException etc.

The unchecked Exceptions are not checked at compile time and don't have to be handled in a catch block or declared in the throws section of the method. The unchecked Exceptions are subclasses of the RuntimeException and some of the most common exceptions of this type are: IllegalArgumentException, NullPointerException, IllegalStateException.

shilpigupta22 4 months ago Answer question What are default methods?

Default methods are methods defined on an interface. They were introduced in Java 1.8. Before Java 1.8, interfaces could only have abstract methods, meaning that interfaces only provided the contract (the signature of the method) and the actual implementation (functionality) had to be provided in a class implementing that interface. With the default methods, the functionality (implementation) can be provided on interface level.

shilpigupta22 4 months ago Answer question Why are exceptions expensive in Java?

The most expensive part of the exceptions is the stacktrace that is created when the Exception is constructed.

shilpigupta22 4 months ago Answer question What is the problem with the following example and does it compile?

The problem is that the constructor is used without the type defined in the class definition. This leads to some warnings, but the code will still compile.

shilpigupta22 4 months ago Answer question What are generics in Java?

Generics are a mechanism that enables types to be parameters of the class, interface or method definition. This mechanism allows reusing of the same code with different inputs, while providing compile-time checks.

shilpigupta22 4 months ago Answer question What's the difference between a HashSet and a LinkedHashSet?

They are both Sets, but the LinkedHashSet maintains a doubly-linked list of the items, preserving the insertion order.

shilpigupta22 4 months ago Answer question What's the synchronized version of a HashMap?

The synchronized version of the HashMap is the HashTable.

shilpigupta22 4 months ago Answer question What's the difference between StringBuilder and StringBuffer?

StringBuffer is a synchronized type, while the StringBuilder is not. The StringBuilder was created as a replacement for the StringBuffer used in a single thread, making the StringBuilder usually faster.