Found 4338 Articles for Java 8

How to read DataInputStream until the end without need to catch an EOFException in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:09:12

977 Views

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.In the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.ExampleFollowing program demonstrates how to handle the EOFException in Java.import java.io.DataInputStream; import ... Read More

Is it possible to have multiple try blocks with only one catch block in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:10:01

8K+ Views

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.Exampleimport java.util.Scanner; public class ExceptionExample {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number: ");       int a = sc.nextInt();       System.out.println("Enter second number: ");       int b = sc.nextInt();       int c = a/b;       System.out.println("The result is: "+c);   ... Read More

How can an exception be thrown manually by a programmer in java?

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

5K+ Views

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.Exampleimport java.util.Scanner; public class ExceptionExample {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number: ");       int a = sc.nextInt();       System.out.println("Enter second number: ");       int b = sc.nextInt();       int c = a/b;       System.out.println("The result is: "+c);   ... Read More

When a lot of changes are required in data, which one you choose String or StringBuffer in java?

Maruthi Krishna
Updated on 05-Aug-2019 14:42:20

524 Views

The String type is a class in Java, it is used to represent a set of characters. Strings in Java are immutable, you cannot change the value of a String once created.Since a String is immutable, if you try to reassign the value of a String. The reference of it will be pointed to the new String object leaving an unused String in the memory.Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters.You can modify/manipulate the contents of a StringBuffer over and over ... Read More

How to access Static variable of Outer class from Static Inner class in java?

Maruthi Krishna
Updated on 05-Aug-2019 14:39:31

926 Views

A class with in another class is known as inner class, you cannot declare a class static unless it is an inner class. A static inner class is just like other class variables. You can access it (static inner class) without instantiationExampleYou can access the static variable of an outer class just using the class name. Following Java example demonstrates how to access static variables of a class from a static inner class.public class Outer {    static int data = 200;    static class InnerDemo {       public void my_method() {          System.out.println("This is ... Read More

How to call another enum value in an enum's constructor using java?

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

2K+ 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 }Methods and variables in an enumerationEnumerations are similar to classes and, you can have variables, methods (Only concrete methods) and constructors within them.For suppose we have elements in an enumeration with values as −enum Scoters {    ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), ... Read More

Constructor overloading in enumerations in Java.

Maruthi Krishna
Updated on 02-Jul-2020 14:17:19

714 Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Constructor overloadingSimilar to methods you can also overload constructors i.e. you can write more than constructor with different parameters.And, based on the parameters we pass at the time of instantiation, respective constructor will be invoked.Examplepublic class Sample{    public Sample(){       System.out.println("Hello how are you");    }    public Sample(String data){       System.out.println(data);    }    public ... Read More

Why interfaces don't have static initialization block when it can have static methods alone in java?

Maruthi Krishna
Updated on 05-Aug-2019 14:28:28

2K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ... Read More

What is blank final variable? What are Static blank final variables in Java?

Maruthi Krishna
Updated on 02-Jul-2020 14:19:27

2K+ Views

Static variables − Static variables are also known as class variables. You can declare a variable static using the keyword. Once you declare a variable static there would only be one copy of it in the class, regardless of how many objects are created from it.public static int num = 39;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.You must access instance variables using an ... Read More

How to access the object of a class without using the class name from a static context in java?

Maruthi Krishna
Updated on 05-Aug-2019 14:17:52

583 Views

The only possible solution is to get the stack trace of the current thread. Get the class name using an element in the stack trace. Pass it to the forName() method of the class named Class.This returns a Class object and you can get an instance of this class using the newInstance() method.Examplepublic class MyClass {    String name = "Krishna";    private int age = 25;    public MyClass() {       System.out.println("Object of the class MyClass");       System.out.println("name: "+this.name);       System.out.println("age: "+this.age);    }    public static void demoMethod() throws Exception {   ... Read More

Advertisements