Found 2616 Articles for Java

What are the differences between protected and default access specifiers in Java?

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

10K+ Views

The Protected access specifier is visible within the same package and also visible in the subclass whereas the Default is a package level access specifier and it can be visible in the same package.Protected Access SpecifierProtected will acts as public within the same package and acts as private outside the package.Protected will also act as public outside the package only with respect to subclass objects.Protected fields or methods cannot be used for classes and Interfaces.The Fields, methods, and constructors declared as protected in a superclass can be accessed only by subclasses in other packages.The classes in the same package can also access protected fields, methods and constructors as ... Read More

What is an OutOfMemoryError and steps to find the root cause of OOM in Java?

raja
Updated on 24-Feb-2020 11:27:10

1K+ Views

The OutOfMemoryError is thrown by JVM, when JVM does not have enough available memory, to allocate. OutOfMemoryError falls into the Error category in Exception class hierarchy.To generate OutOfMemoryErrorWe will allocate a large chunk of memory, which will exhaust heap memory storage.We will keep on allocating the memory and point will reach, when JVM will not have enough memory to allocate, then OutOfMemoryError will be thrown.Once we will catch the OutOfMemory error, we can log the error.ExampleLive Demopublic class OutOfMemoryErrorDemo {    public static void main(String[] args) throws Exception {       int dummyArraySize = 15;       System.out.println("Max JVM memory: " + Runtime.getRuntime().maxMemory()); ... Read More

Can we write any code after throw statement in Java?

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

1K+ Views

No, we can not place any code after throw statement, it leads to compile time error Unreachable Statement.Throw keyword in JavaThe throw keyword is used to throw an exception manually.Whenever it is required to suspend the execution of the functionality based on the user-defined logical error condition, we will use this throw keyword to throw an exception.We need to handle these exceptions using try and catch blocks.Rules to use throw keyword in JavaThe throw keyword must follow Throwable type of object.The throw keyword must be used only in the method logic.Since it is a transfer statement, we cannot place statements after throw statement. It leads to ... Read More

How many types of anonymous inner classes are defined in Java?

raja
Updated on 11-Feb-2020 09:26:32

989 Views

An anonymous inner class is an inner class which is declared without any class name at all. In other words, a nameless inner class is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.We can define an anonymous inner class and create its object using the new operator at the same time in one step.Syntaxnew(argument-list){    // Anonymous class body }Types of Anonymous Inner Class in JavaAnonymous inner class that extends a classAnonymous inner class that implements an interfaceAnonymous inner ... Read More

What are the differences between import and static import statements in Java?

raja
Updated on 11-Feb-2020 09:29:38

4K+ Views

We can use an import statement to import classes and interface of a particular package. Whenever we are using import statement it is not required to use the fully qualified name and we can use short name directly. We can use static import to import static member from a particular class and package. Whenever we are using static import it is not required to use the class name to access static member and we can use directly.import statementTo access a class or method from another package we need to use the fully qualified name or we can use import statements.The class or method ... Read More

What are the differences between compareTo() and compare() methods in Java?

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

6K+ Views

The Comparable interface provides a compareTo() method for the ordering of objects. This ordering is called the class’s natural ordering and the compareTo() method is called its natural comparison method. The Comparator interface provides the methods for performing sorting operations. By using the Comparator interface we can do multiple sorting sequences. We can sort the objects with respect to multiple data members.compareTo()compareTo() method compares this object with an o1 object and returns an integer.Syntaxpublic int compareTo(Object o1)It returns –ve number if & only if this object is less than o1.It returns +ve number if & only if this object is greater than o1.It returns 0 if ... Read More

Can we write any statements between try, catch and finally blocks in Java?

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

2K+ Views

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.  The functionality of try keyword is to identify an exception object and catch that exception object and transfer the control along with the identified exception object to the catch block by suspending the execution of the try block. The functionality of the catch block is to receive the exception class object that has been sent by the try and catch that exception class object and assigns that exception class object to the reference of the corresponding exception class defined in the catch block. The finally blocks are ... Read More

What are the differences between recursion and iteration in Java?

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

3K+ Views

The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.RecursionRecursion uses selection structure.Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition (base case) and Infinite recursion can crash the system.Recursion terminates when a base case ... Read More

What are unreachable catch blocks in Java?

raja
Updated on 06-Feb-2020 11:42:15

5K+ Views

A block of statements to which the control can never reach under any case can be called as unreachable blocks. Unreachable blocks are not supported by Java. The catch block mentioned with the reference of Exception class should and must be always last catch block because Exception is the superclass of all exceptions. When we are keeping multiple catch blocks, the order of catch blocks must be from most specific to most general ones. i.e subclasses of Exception must come first and superclasses later. If we keep superclasses first and subclasses later, the compiler will throw an unreachable catch block error.Syntaxtry ... Read More

Why Char[] array is more secure (store sensitive data) than String in Java?

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

5K+ Views

Both String and Char[] array are used to store the textual data but choosing one over the other is more difficult. Maybe we can get the idea from the immutability of String why char[] array is preferred over String for storing sensitive information data like password, SSN,  etc.Using the plain string is a much higher chance of accidentally printing the password to logs or some other insecure places where char[] array is less vulnerable.Since String is immutable, there is no method defined that allow us to change or overwrite the content of the string. This feature makes string objects unstable for storing ... Read More

Advertisements