Raja has Published 806 Answers

What are the differences between GridLayout and GridBagLayout in Java?

raja

raja

Updated on 07-Feb-2020 06:09:22

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid ... Read More

Can we extend an enum in Java?

raja

raja

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

No, we cannot extend an enum in Java. Java enums can extend java.lang.Enum class implicitly, so enum types cannot extend another class.Syntaxpublic abstract class Enum> implements Comparable, Serializable {    // some statements }EnumAn Enum type is a special data type which is added in Java 1.5 version.An Enum is used to define ... Read More

Can we change the order of public static void main() to static public void main() in Java?

raja

raja

Updated on 07-Feb-2020 05:47:14

Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then ... Read More

When can we use intern() method of String class in Java?

raja

raja

Updated on 07-Feb-2020 05:45:33

The intern() method of String class can be used to deal with the string duplication problems in Java. Using intern() we can save a lot of memory consumed by duplicate string instances. A string is duplicate if it contains the same content as another string but it can be occupied different memory ... Read More

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

raja

raja

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

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

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

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

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

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

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

Advertisements