Found 4338 Articles for Java 8

What is EOFException in Java? How do we handle it?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ 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.Let us consider 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. Live ... Read More

What value does read() method return when it reaches the end of a file in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

948 Views

The read() method of the input stream classes reads the contents of the given file byte by byte and returns the ASCII value of the read byte in integer form. While reading the file if it reaches the end of the file this method returns -1.ExampleAssume we have a text file (sample.txt) in the current directory with a simple text saying “Hello welcome”. Following Java program reads the contents of the file byte by byte using the read() method and prints the integer value returned for each byte.Since we are not checking for end of the file, the read() method ... Read More

Can we overload a method based on different return type but same argument type and number, in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

8K+ Views

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Exampleclass Test{ public int division(int a, int b){ int result = a/b; return result; } public double division (float a, float b){ double result = a/b; ... Read More

What are the restrictions placed on method overloading in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

789 Views

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Example Live Democlass Test{ public int division(int a, int b){ int result = a/b; return result; } public double division (float a, float b){ double result = ... Read More

Why is operator overloading not supported by java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

10K+ Views

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Operator overloadingOperator overloading is the ability to redefine the functionality of the operators. Programming languages like c++ supports operator overloading.you can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.Overloaded operators are functions with special names: the keyword "operator" followed ... Read More

What are copy constructors in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

4K+ Views

Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.writing a copy constructorUsually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values.int name; int age; public Student(String name, int age){ this.name = name; this.age ... Read More

How do we copy objects in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

8K+ Views

In Java you can copy an object in several ways, among them, copy constructor and the clone method are the mostly used.Using copy constructorGenerally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. Java does support for copy constructors but you need to define them yourself.ExampleIn the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.Then, we have another constructor which accepts an object of the current class and initializes the ... Read More

How to convert a double value to String in Java?

Venkata Sai
Updated on 04-May-2022 13:10:21

2K+ Views

The double data type in Java stores double-precision 64-bit IEEE 754 floating point number. It is used as the default type for decimal value in Java.Like all other primitive variables double also have a wrapper class (Double) wrapping the primitive data type. Since java supports auto boxing, the primitive value and the object can be used interchangeably.You can convert a double value into String in various different ways in Java −Using the “+” operator −The + operator is an addition operator but when used with Strings it acts as a concatenation operator. It concatenates the other operand to String and ... Read More

What are the rules to be followed while making a variable static and final?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

237 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;final − Once you declare a variable final you cannot reassign value to it. When you declare a variable of a class static and final we are making it constant.Rules to be followedInitialization is mandatory − It is not mandatory to initialize the instance variables of a class in java. ... Read More

How to implement constants in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.You can create a constant in c language using the constant keyword (one way to create it) as −const int intererConstant = 100; or, const float floatConstant = 16.254; ….. etcConstants in javaUnlike in C language constants are not supported in Java(directly). But, you can still create a constant by declaring a variable static and final.Static − Once you declare a ... Read More

Advertisements