Found 2616 Articles for Java

What is the contract between equals() and hashCode() methods in Java?

raja
Updated on 06-Feb-2020 11:44:53

12K+ Views

Every Java object has two very important methods equals() and hashCode() and these methods are designed to be overridden according to their specific general contract. An Object class is the parent class of every class, the default implementation of these two methods is already present in each class. However, we can override these methods based on the requirement.hashCode() Methodpublic int hashCode()This method returns an integer value, which is referred to as the hash code value of an object. Every Object, at the time of creation assigned with a unique 32-bit, signed int value. This value is the hash code value of ... Read More

What are the differences between tight coupling and loose coupling in Java?

raja
Updated on 06-Feb-2020 11:46:37

15K+ Views

Tight coupling means classes and objects are dependent on one another. In general, tight coupling is usually not good because it reduces the flexibility and re-usability of the code while Loose coupling means reducing the dependencies of a class that uses the different class directly.Tight CouplingThe tightly coupled object is an object that needs to know about other objects and is usually highly dependent on each other's interfaces.Changing one object in a tightly coupled application often requires changes to a number of other objects.In the small applications, we can easily identify the changes and there is less chance to miss ... Read More

What is the importance of "Java.lang.Class" in Java?

raja
Updated on 30-Jul-2019 22:30:26

343 Views

The java.lang.Class is one of the most important class in Java and it can provide several utility methods like getClass(), forName() which is used to find and load a class. It can also provide methods like Class.newInstance() which is the backbone of reflection and allow us to create an instance of a class without using new() operator.Importance of java.lang.ClassInstances of the class Class represent classes, interfaces,  enum and annotation in a running Java application.Whenever a java file is compiled, the compiler will insert a public, static, final field named Class of the type java.lang.Class into generated .class fileEach and every class exposes its code in the form of an ... Read More

How to create an immutable class with mutable object references in Java?

raja
Updated on 06-Feb-2020 11:48:17

10K+ Views

Immutable objects are those objects whose states cannot be changed once initialized. Sometimes it is necessary to make an immutable class as per the requirement. For example, All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java. String class is also an immutable class.To create a custom immutable class we have to do the following stepsDeclare the class as final so it can’t be extended.Make all fields private so that direct access is not allowed.Do not provide setter methods (methods that modify fields) for variables, so that it can not be set.Make all mutable ... Read More

What are the differences between Widening Casting (Implicit) and Narrowing Casting (Explicit) in Java?

raja
Updated on 06-Feb-2020 11:50:17

3K+ Views

A Type casting in Java is used to convert objects or variables of one type into another. When we are converting or assigning one data type to another they might not compatible. If it is suitable then it will do smoothly otherwise chances of data loss.Type Casting Types in JavaJava Type Casting is classified into two types.Widening Casting (Implicit) – Automatic Type ConversionNarrowing Casting (Explicit) – Need Explicit ConversionWidening Casting (smaller to larger type)Widening Type Conversion can happen if both types are compatible and the target type is larger than source type. Widening Casting takes place when two types are ... Read More

Can a constructor throw an exception in Java?

raja
Updated on 30-Jul-2019 22:30:26

4K+ Views

Yes, constructors are allowed to throw an exception in Java.A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class. Each object of a class will have its own state (Instance variables) and access to methods of its class.Throw an Exception from a ConstructorA checked exception can be used to indicate a legitimate problem when trying to create an instance, while an unchecked exception typically indicates a bug either in the ... Read More

How to throw an exception from a static block in Java?

raja
Updated on 30-Jul-2019 22:30:26

5K+ Views

A static block is a set of statements, which will be executed by the JVM before the execution of the main() method. At the time of class loading if we want to perform any activity we have to define that activity inside a static block because this block executes at the time of class loading.Throw an exception from a Static BlockA static block can throw only a RunTimeException, or there should be a try and catch block to catch a checked exception.A static block occurs when a class is loaded by a class loader. The code can either come in the form ... Read More

How to instantiate member inner class in Java?

raja
Updated on 11-Feb-2020 09:21:14

1K+ Views

A class that is declared inside a class but outside a method is known as member inner class.We can instantiate a member Inner class in two waysInvoked within the classInvoked outside the classRules for Inner ClassThe outer class (the class containing the inner class) can instantiate as many numbers of inner class objects as it wishes, inside its code.If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.No inner class objects are automatically instantiated with an outer class object.If the inner ... Read More

What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?

raja
Updated on 30-Jul-2019 22:30:26

4K+ Views

Both ClassNotFoundException and NoClassDefFoundError are the errors when JVM or ClassLoader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked.There are different types of ClassLoader loads classes from difference sources, sometimes it may cause library JAR files missing or incorrect class-path which causes loader not able to load the class at run-time.ClassNotFoundExceptionClassNotFoundException comes when we try to load a class at run-time using Reflection and if those class files are missing then application or program thrown with ClassNotFoundException Exception. There is nothing to check at compile-time since it is loading the class ... Read More

How to rethrow an exception in Java?

raja
Updated on 30-Jul-2019 22:30:26

17K+ Views

Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) {    System.out.println("An exception ... Read More

Advertisements