Raja has Published 760 Articles

How to write the comparator as a lambda expression in Java?

raja

raja

Updated on 06-Dec-2019 10:26:25

4K+ Views

A lambda expression is an anonymous method and doesn't execute on its own in java. Instead, it is used to implement a method defined by the functional interface. A lambda expression used with any functional interface and Comparator is a functional interface. The Comparator interface has used when sorting a collection of objects ... Read More

What is the data type of a lambda expression in Java?

raja

raja

Updated on 02-Dec-2019 09:14:51

1K+ Views

The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters. Its return type is a parameter -> expression body to understand the syntax, we can divide it into three parts.Parameters : These are function method parameters and match with the signature of ... Read More

Why the java file name should be always the same as a public class name?

raja

raja

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

15K+ Views

         In Java, the java file name should be always the same as a public class name.While writing a java program first it is saved as a ".java" file, when it is compiled it forms byte code which is a ".class" file as such that if we ... Read More

What are the differences between protected and default access specifiers in Java?

raja

raja

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

10K+ Views

The Protected access specifier is visible within the same package and also visible in the subclass whereas the Default is a package level access specifier and it can be visible in the same package.Protected Access SpecifierProtected will acts as public within the same package and acts as private outside the package.Protected will also act as ... Read More

Can we access the instance variables from a static method in Java?

raja

raja

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

4K+ Views

We cannot directly access the instance variables within a static method because a static method can only access static variables or static methods.An instance variable, as the name suggests is tied to an instance of a class. Therefore, accessing it directly from a static method, which is not tied to ... Read More

How to handle the ArithmeticException (unchecked) in Java?

raja

raja

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

6K+ Views

The java.lang.ArithmeticException is an unchecked exception in Java. Usually, one would come across java.lang.ArithmeticException: / by zero which occurs when an attempt is made to divide two numbers and the number in the denominator is zero. ArithmeticException objects may be constructed by the JVM.Example1Live Demopublic class ArithmeticExceptionTest {    public static ... Read More

Is it possible to assign a numeric value to an enum in Java?

raja

raja

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

648 Views

Yes, it's possible to assign a numeric value to an enum.Enum in Java is used to represent a list of predefined values. Most of the time, we probably not care about the associated value of an enum. However, there might be some times when we need to assign specific values ... Read More

Can an interface extend multiple interfaces in Java?

raja

raja

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

6K+ Views

Yes, we can do it. An interface can extend multiple interfaces in Java.Example:interface A {    public void test();    public void test1(); } interface B {    public void test();    public void test2(); } interface C extends A, B {    public void test3(); } class D implements ... Read More

How do we use an enum type with a constructor in Java?

raja

raja

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

183 Views

Enum type can have a private constructor that can be used to initialize instance fields. The EnumDemo class demonstrates this. It features a Food enum type with four constants: HAMBURGER, FRIES, HOTDOG, and ARTICHOKE. Notice that after each constant value is present in parentheses. This calls the constructor for that member ... Read More

Can we declare a top level class as protected or private in Java?

raja

raja

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

6K+ Views

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.Syntax// A top level class    public class TopLevelClassTest {       // Class body }If ... Read More

Advertisements