Java Articles - Page 270 of 745

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

246 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

457 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

794 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

452 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

Can we have integers as elements of an enum in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:15:03

11K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Integers as elements of an enumNo, we can have ... Read More

What is a variable, field, property in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:10:05

1K+ Views

In programming to hold data members we use variables, Java you can declare three types of variables namely, 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 ... Read More

Advertisements