Found 2620 Articles for Java

Can we have a return statement in the catch or, finally blocks in Java?

raja
Updated on 17-Nov-2023 16:33:50

11K+ Views

Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions. If we return a value in the catch block and we can return a value at the end of the method, the code will execute successfully. If we return a value in the catch block and we can write a statement at the end of the method after return a value, the code will not execute so it became unreachable ... Read More

Can we have a try block without a catch block in Java?

raja
Updated on 21-Nov-2023 10:08:40

24K+ Views

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.exit() it will execute always. Example 1 public class TryBlockWithoutCatch { public static void main(String[] args) { try { System.out.println("Try Block"); } finally { System.out.println("Finally Block"); } } } Output Try Block Finally Block A ... Read More

What are the differences between an Exception class and an Error class in Java?

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

559 Views

Both an Exception class and an Error class are subclasses of java.lang.Throwable class, we can able to handle the exceptions at runtime but the errors we cannot handle.Exceptions are the objects representing the logical errors that occur at the run time and makes JVM enters into the state of "ambiguity".The objects which are automatically created by the JVM for representing these run time errors are known as an Exception. An Error is a subclass of Throwable class that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.If an exception occurs we ... Read More

Does Java support multi-dimensional Arrays?

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

406 Views

No, Java does not support multi-dimensional arrays.Java supports arrays of arrays.In Java, a two-dimensional array is nothing but, an array of one-dimensional arrays.                  int[][] arr = new int[2][4];The expression arr[i] selects the one-dimensional array and the expression arr[i][j] selects the element from that array.Array indices in each dimension range from zero to "length". Where length is the array length in the given dimension. There is no array assignment operator. The number of dimensions and the size of each dimension is fixed once the array has been allocated.

Where to use a StringBuffer/StringBuilder than a String in Java?

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

191 Views

The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.A StringBuffer is synchronized while a StringBuilder is not synchronized.A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.If the Object value is not going to change use String Class because a String object is immutable.If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

Why should we use a StringBuffer instead of a String in Java?

raja
Updated on 11-Feb-2020 08:04:56

690 Views

A StringBuffer is a thread-safe, mutable sequence of characters.Unlike a String class (immutable), the StringBuffer class is mutable. That is, we can change the contents of a StringBuffer object.When we modify a string of StringBuffer class, we are not creating a new String object, but rather operating directly on the original string itself.For this reason, the StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string. A StringBuffer can be defined simply by the use of the new operator and bypassing the string value inside a   ... Read More

What are the differences between the Heap memory and the String Constant Pool in Java?

raja
Updated on 06-Feb-2020 10:00:04

4K+ Views

Heap MemoryThe heap memory is a run time data area from which the memory for all java class instances and arrays is allocated.The heap is created when the JVM starts up and may increase or decrease in size while the application runs.The size of the heap can be specified using –Xms VM option. The heap can be of fixed size or variable size depending on the garbage collection strategy. Maximum heap size can be set using –Xmx option.By default, the maximum heap size is set to 64 MB. String Constant PoolString uses a special memory location to reuse of String objects ... Read More

Why String literal is stored in String Constant Pool in Java?

raja
Updated on 17-Nov-2023 14:10:14

2K+ Views

There are two ways to create a String object in Java By using the new operator String str = new String("Tutorials Point"); By using String literal String str = "Tutorials Point"; Whenever we call new String() in Java, it will create an object in the heap memory and String literals will go into String Constant Pool (SCP). For objects, JVM used SCP which is for efficient memory management in Java. Unlike other Java objects, instead of managing String object on the heap area, they introduced the String constant pool. One of important characteristic of String constant pool is that it does not create the same String ... Read More

Why String class is immutable or final in Java?

raja
Updated on 17-Nov-2023 14:21:41

7K+ Views

The string is immutable means that we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend it and destroy its immutability. Security Parameters are typically represented as String in network connections, database connection URLs, usernames/passwords, etc. If it was mutable, these parameters could be changed easily. Synchronization and Concurrency making String immutable automatically makes them thread safe thereby solving the synchronization issues. Caching when compiler optimizes our String objects, it seems that if two objects have the same value (a =" test", and b =" test") and ... Read More

How to call an interface method in Java?

raja
Updated on 11-Feb-2020 08:06:59

17K+ Views

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.Examplepublic interface InterfaceDemo{     default public void displayNameDefault(String name){        System.out.println("Your name is : " + name);    }     public void displayName(String name);     public void displayNameAndDesignation(String name, String designation); }The above interface defines three methods for displaying a name and optionally a job title. One method is a default method that contains implementation logic. The remaining two methods do not include implementation logic.public class InterfaceDemoImpl ... Read More

Advertisements