Raja has Published 760 Articles

How to use the substring() method of java.lang.String class in Java?

raja

raja

Updated on 07-Feb-2020 05:41:38

163 Views

The substring() method returns a String datatype which corresponds to the original String starting from the begin index until the end Index. If the end index is not specified, it is imperative that the endIndex is the String length. Since we are dealing with the String, the index starts at '0' ... Read More

Explain the basic structure of a program in Java?

raja

raja

Updated on 07-Feb-2020 05:37:58

4K+ Views

A typical structure of a Java program contains the following elementsPackage declarationImport statementsCommentsClass definitionClass variables, Local variablesMethods/BehaviorsPackage declarationA class in Java can be placed in different directories/packages based on the module they are used. For all the classes that belong to a single parent source directory, a path from source ... Read More

How to handle the NumberFormatException (unchecked) in Java?

raja

raja

Updated on 06-Feb-2020 12:47:43

2K+ Views

The NumberFormatException is an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number.The NumberFormatException can be thrown by many methods/constructors in the classes of java.lang package. Following are some of them.public static int parseInt(String s) throws NumberFormatExceptionpublic static Byte valueOf(String s) throws NumberFormatExceptionpublic static byte parseByte(String s) throws NumberFormatExceptionpublic ... Read More

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

raja

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 ... Read More

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

raja

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 ... Read More

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

raja

raja

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

9K+ 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 ... Read More

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

raja

raja

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

14K+ 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 ... Read More

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

raja

raja

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

11K+ 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 ... Read More

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

raja

raja

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

4K+ 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 ... Read More

What are unreachable catch blocks in Java?

raja

raja

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

4K+ 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 ... Read More

Advertisements