Throw Null in Java and Upcast to NullPointerException

Maruthi Krishna
Updated on 02-Jul-2020 14:42:59

1K+ Views

In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.Object obj = null;But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.Examplepublic class Demo {    String name = "Krishna";    int age = 25;    public static void main(String args[]) {       ... Read More

When Does a Java Array Throw a NullPointerException

Maruthi Krishna
Updated on 02-Jul-2020 14:40:03

4K+ Views

In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.Object obj = null;But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.Examplepublic class Demo {    String name = "Krishna";    int age = 25;    public static void main(String args[]) {       ... Read More

Try-Catch-Finally Blocks in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:35:00

2K+ Views

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.Here are some example scenarios −If you ... Read More

Cause of NoSuchElementException and How to Fix it in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:33:06

1K+ Views

What is the cause of NoSuchElementException and how can we fix it in java?An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed. Each exception is represented by its respective class.Cause for NosuchElementExceptionThis is a Runtime exception i.e. it occurs at the execution time.While accessing the contents of a collection, array or other objects using the accessor methods of an Enumeration, Iterator or, tokenizer, such as next() or nextElement(), if you try to ... Read More

Handle MalformedURLException in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:31:32

2K+ Views

While working with client-server programming in Java (JSE), if you are using java.net.URL class object in your program, you need to instantiate this class by passing a string representing required URL to which you need to establish connection. If the url you have passed in the string which cannot be parsed or, without legal protocol a MalformedURLException is generated.ExampleIn the following Java example we are tring to get establish a connection to a page and publishing the response.We have tampered the protocol part, changed it to htt, which should be http or, https.import java.util.Scanner; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; ... Read More

Variables Defined in Try Cannot Be Used in Catch or Finally in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:30:03

6K+ Views

A class in Java will have three kinds of variables namely, static (class), instance and, local.Instance variables − These variables belong to the instances (objects) of a class. These are declared within a class but outside methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor or blocks of that particular class.Class/static variables − class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the ... Read More

Do All Properties of an Immutable Object Need to Be Final in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:29:20

1K+ Views

Immutable class/object is the one whose value cannot be modified. For example, Strings are immutable in Java i.e. once you create a String value in Java you cannot modify it. Even if you try to modify, an intermediate String is created with the modified value and is assigned to the original literal.Defining immutable objectsWhenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.ExampleFollowing Java program demonstrates the ... Read More

What Happens If an Exception Is Not Handled in a Java Program

Maruthi Krishna
Updated on 02-Jul-2020 14:28:11

3K+ Views

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.Here are some example scenarios −If you ... Read More

MalformedURLException in Java and How to Fix It

Maruthi Krishna
Updated on 02-Jul-2020 14:26:42

21K+ Views

While working with client-server programming in Java (JSE), if you are using java.net.URL class object in your program, you need to instantiate this class by passing a string representing required URL to which you need to establish connection. If the url you have passed in the string which cannot be parsed or, without legal protocol a MalformedURLException is generated.ExampleIn the following Java example we are tring to get establish a connection to a page and publishing the response.We have tampered the protocol part, changed it to htt, which should be http or, https.import java.util.Scanner; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; ... Read More

Modifiers Allowed for Local Variables in Java

Maruthi Krishna
Updated on 02-Jul-2020 14:24:08

1K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within a class, ... Read More

Advertisements