Found 9150 Articles for Object Oriented Programming

How to 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

Importance of the parseBoolean() method in Java?

raja
Updated on 01-Jul-2020 06:13:23

173 Views

The parseBoolean() method is an important method of a Boolean class. The parseBoolean() is a static method and can parse the String method argument into a Boolean object. The parseBoolean() method of Boolean class returns the boolean represented by the string argument.Syntaxpublic static boolean parseBoolean(String s)Exampleimport java.util.Scanner; public class ParseBooleanMethodTest {    public static void main(String[] args) {       System.out.print("Are you ready to play cricket(true/false)?");       Scanner scanner = new Scanner(System.in);       String str = scanner.nextLine();       scanner.close();       // Convert the user input into boolean       boolean answer ... Read More

What is the cause of NoSuchElementException and how can we 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

IlleagalStateException Vs NoSuchElementException in java?

Maruthi Krishna
Updated on 19-Sep-2019 15:31:21

236 Views

When you call a method at illegal or inappropriate time an IlleagalStateException is generated.For example, the remove() method of the ArrayList class removes the last element after calling the next() or previous methods.After removing the element at the current position you need to move to the next element to remove it i.e. per one call of the next() method you can invoke this remove() method only once.Since the initial position of the list (pointer) will be before the first element, you cannot invoke this method without calling the next method.If you invoke the remove() method otherwise it throws an java.lang.IllegalStateException.Example: ... Read More

Different scenarios that cause NoSuchElementException in Java.

Maruthi Krishna
Updated on 06-Aug-2019 08:47:46

431 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurs 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.NosuchElement ExceptionThis 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 get the elements from an empty object, or if you try to get the ... Read More

What are 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

How to create an immutable set in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:34:07

1K+ Views

Whenever 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.A Set is an interface in collection framework, which does not allow duplicate values.You need to keep following points in mind while creating an immutable set −We should not be able to add or delete elements from it.we should not be able to add null values to an immutable set.Once you create an immutable set you cannot add ... Read More

How to append data to a file in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:31:13

785 Views

In most scenarios if you try to write contents to a file, using the classes of the java.io package, the file will be overwritten i.e. data existing in the file is erased and the new data is added to it.But, in certain scenarios like logging exceptions into a file (without using logger frame works) you need to append data (message) in the next line of the file.You can do this using the Files class of the java.nio package. This class provides a method named write() which acceptsAn object of the class Path, representing a file.A byte array holding the data ... Read More

Can the main method in Java return a value?

Maruthi Krishna
Updated on 06-Aug-2019 08:26:53

2K+ Views

The public static void main(String args[]) is the entry point of a Java program Whenever you execute a program the JVM searches for the main method and starts executing the contents of it. If such method is not found the program gets executed successfully, but when you execute the program it generates an error.As the matter of fact you should declare the main method with public static as modifiers, void return type and String arguments if you change anything, JVM doesn’t considers as the entry point method and prompts an error at run time.Therefore, you cannot change the return type ... Read More

What happens when you declare a method/constructor final in Java?

Maruthi Krishna
Updated on 08-Aug-2019 13:23:52

444 Views

Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).Still, if you try to override a final method a compile time error is generated.Exampleinterface Person{    void dsplay(); } class Employee implements Person{    public final void dsplay() {       System.out.println("This is display method of the Employee class");    } } class Lecturer extends Employee{    public void dsplay() {       System.out.println("This is display method of ... Read More

Advertisements