Java Articles

Page 142 of 450

Can we declare more than one class in a single Java program?\\n

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 17K+ Views

Declaring Multiple Classes in Java Program A single Java program may contain two or more classes, it is possible in two ways: Multiple non-nested classes Nested classes The Multiple non-nested Classes We can create as many classes as we want in a single Java program but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class. When we compile a Java program with two or more classes (non-nested), the same number of .class ...

Read More

What are unreachable catch blocks in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 6K+ Views

A block of statements that the program control can never reach under any circumstances can be called an unreachable block. Java does not support unreachable blocks, such code will cause a compile-time error. Any code that is written but never executed is considered unnecessary by the Java compiler. In this article, we will discuss unreachable catch blocks in Java. But, before that, let's understand what is a catch-block in Java. The catch Block in Java The catch block is written just after the try block and cannot have any code between them. It is an exception handler and contains code ...

Read More

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

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 5K+ Views

Both String class and Char[] array in Java are used to store textual data. However, Strings are immutable, which means you can't make changes to a String once defined, and the char[] array is not immutable. In the official documentation of Java Cryptography Architecture, it is clearly written that String objects are not suitable for storing sensitive data, such as passwords, SSN, etc. Use a char array instead, as it is more secure than String. This article will help you understand why char[] array is used to store sensitive data. Char Array is more secure than String Let's discuss why ...

Read More

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

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 5K+ Views

The import statement is used to bring certain classes and interfaces from other packages into our Java program, so we can access them without using their fully qualified names. We can use the short name instead. Java also supports static import statements, which were introduced in Java 5. It helps in accessing static members such as methods and constants. In this article, we are going to learn the difference between import and static import statements in Java. The import Statement in Java To access a class or method from another package, we need to either use the fully qualified ...

Read More

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

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 5K+ Views

A Type casting in Java is used to convert objects or variables from one data type to another. When we are converting or assigning one data type to another, automatic conversion will take place (if the types are compatible and the conversion is safe). However, if there is a risk of data loss, the conversion must be done explicitly by the programmer. Let's understand the types of Java type casting and the difference between them in this article. Types of Type Casting in Java Java Type Casting is classified into two types, which are: ...

Read More

How to resolve a NullPointerException in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 20-May-2025 2K+ Views

The NullPointerException is a subclass of the RuntimeException class. It is defined in java.lang package of Java. In this article, we are going to understand the reasons for NullPointerException and how to resolve them. Reason for NullPointerException in Java A NullPointerException is a runtime exception thrown by the JVM when our application code, another referenced API, or the middleware encounters the following conditions: Attempting to invoke an instance method of a null object. ...

Read More

Differences between org.simple.json and org.json libraries in Java?

Manisha Chand
Manisha Chand
Updated on 20-May-2025 3K+ Views

In Java, org.simple.json and org.json are two libraries that help in reading, writing, and manipulating JSON. But still, they are different. In this article, we are going to learn about these differences. Difference between JSON.simple vs JSON Let's see the below differences and they are - Features JSON.simple JSON ...

Read More

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

Shriansh Kumar
Shriansh Kumar
Updated on 19-May-2025 2K+ Views

In Java, the string literals (or, string objects) are stored in a separate memory area called string constant pool to improve the performance of string operations and optimize the memory while using them. Let's understand how. Creating String Objects in Java There are two ways to create a String object in Java: Using the new operator Using String literal Example The example given below shows how to create a string object: public class Example { public static void main(String[] args) { ...

Read More

When to use @JsonValue annotation using Jackson in Java?\\n

Manisha Chand
Manisha Chand
Updated on 19-May-2025 6K+ Views

In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether it is a variable, is a JsonProperty, should be ignored, or what condition should be applied to it. So basically, Annotations make JSON output clearer as we required. In this Article, we will learn about ...

Read More

How to convert a JSON to Java Object using the Jackson library in Java?

Manisha Chand
Manisha Chand
Updated on 19-May-2025 42K+ Views

Jackson is a Java library that is used to convert JSON to Java objects and vice versa. Conversion of JSON to a Java object is called deserialization, and Java object to JSON is known as serialization. Both of these tasks can be done by using the Jackson library. In this article, we are going to learn how to convert JSON to Java objects using the Jackson library. Jackson Library: Convert a JSON to a Java Object The ObjectMapper class belongs to the Jackson library. This class is responsible for the serialization and deserialization of Java objects. The ObjectMapper class is used ...

Read More
Showing 1411–1420 of 4,496 articles
« Prev 1 140 141 142 143 144 450 Next »
Advertisements