Found 7442 Articles for Java

What are the characteristics of lambda expressions in Java?

raja
Updated on 10-Jul-2020 12:44:50

1K+ Views

The lambda expressions were introduced in Java 8 and facilitate functional programming. A lambda expression works nicely together only with functional interfaces and we cannot use lambda expressions with more than one abstract method.Characteristics of Lambda ExpressionOptional Type Declaration − There is no need to declare the type of a parameter. The compiler inferences the same from the value of the parameter.Optional Parenthesis around Parameter − There is no need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.Optional Curly Braces − There is no need to use curly braces in the expression body if the body contains a ... Read More

Count the number of columns in a MySQL table with Java

Alshifa Hasnain
Updated on 15-Jul-2025 17:58:08

428 Views

In this article, we will learn how to count the number of columns in a MySQL table using JDBC. We will be using the ResultSetMetaData to get details of the table by using simple examples. What is ResultSetMetaData? The ResultSetMetaData is an interface that is present in the java.sql package. Using ResultSetMetaData, we can get information about the table, for example, what are the column names of each and every table, and how many columns are there?. To create the object for ResultSet: ResultSet rs=st.executeQuery("Select * from Student"); The executeQuery method writes the records, which are then stored in the ... Read More

How to handle an exception using lambda expression in Java?

raja
Updated on 10-Jul-2020 12:01:45

3K+ Views

A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.Exampleinterface Student {    void studentData(String name) throws Exception; } public class LambdaExceptionTest {    public static void main(String[] args) {       // lamba expression        Student student = name -> {          System.out.println("The Student name is: " + name);          throw new Exception();       };       try {          student.studentData("Adithya");       } catch(Exception e) {       }    } }OutputThe Student name is: Adithya

How to use a final or effectively final variable in lambda expression in Java?

raja
Updated on 10-Jul-2020 11:58:59

2K+ Views

The effectively final variables refer to local variables that are not declared final explicitly and can't be changed once initialized. A lambda expression can use a local variable in outer scopes only if they are effectively final.Syntax(optional) (Arguments) -> bodyIn the below example, the "size" variable is not declared as final but it's effective final because we are not modifying the value of the "size" variable.Exampleinterface Employee {    void empData(String empName); } public class LambdaEffectivelyFinalTest {    public static void main(String[] args) {       int size = 100;       Employee emp = name -> {        // lambda ... Read More

What are the advantages of Lambda Expressions in Java?

raja
Updated on 10-Jul-2020 11:46:57

3K+ Views

A lambda expression is an inline code that implements a functional interface without creating a concrete or anonymous class. A lambda expression is basically an anonymous method.Advantages of Lambda ExpressionFewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface. For instance, Runnable is a functional interface, so we can easily apply lambda expressions.Sequential and Parallel execution support by passing behavior as an argument in methods − By using Stream API in Java 8, the functions are passed to collection methods. Now ... Read More

How to implement lambda expression without creating an anonymous class in Java?

raja
Updated on 10-Jul-2020 11:09:51

315 Views

A lambda expression is an anonymous function without having any name and does not belong to any class that means it is a block of code that can be passed around to execute.Syntax(parameter-list) -> {body}We can implement a lambda expression without creating an anonymous inner class in the below program. For the button's ActionListener interface, we need to override one abstract method addActionListener() and implement the block of code using the lambda expression.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaExpressionButtonTest extends JFrame {    private JButton btn;    public LambdaExpressionButtonTest() {       btn = new JButton("Click on the button"); ... Read More

Differences between anonymous class and lambda expression in Java?

raja
Updated on 10-Jul-2020 11:10:31

3K+ Views

Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.Anonymous class vs Lambda ExpressionAn anonymous class object generates a separate class file after compilation that increases the size of a jar file while a lambda expression is converted into a private method. It uses invokedynamic bytecode instruction to bind this method dynamically, which saves time and memory.We use this keyword to represent the current class in lambda expression while in the ... Read More

How to create a thread using lambda expressions in Java?

raja
Updated on 10-Jul-2020 11:11:11

7K+ Views

The lambda expressions are introduced in Java 8. It is one of the most popular features of Java 8 and brings functional programming capabilities to Java. By using a lambda expression, we can directly write the implementation for a method in Java.In the below program, we can create a thread by implementing the Runnable interface using lamda expression. While using the lambda expressions, we can skip the new Runnable() and run() method because the compiler knows that Thread object takes a Runnable object and that contains only one method run() that takes no argument.Examplepublic class LambdaThreadTest {    public static void main(String args[]) {     ... Read More

Are lambda expressions objects in Java?

raja
Updated on 10-Jul-2020 11:04:59

2K+ Views

Yes, any lambda expression is an object in Java. It is an instance of a functional interface. We have assigned a lambda expression to any variable and pass it like any other object.Syntax(parameters) -> expression              or (parameters) -> { statements; }In the below example, how a lambda expression has assigned to a variable and how it can be invoked.Example@FunctionalInterface interface ComparatorTask {    public boolean compare(int t1, int t2); } public class LambdaObjectTest {    public static void main(String[] args) {       ComparatorTask ctask = (int t1, int t2) -> {return t1 ... Read More

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

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

5K+ 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 compared with each other.In the below example, we can sort the employee list by name using the Comparator interface.Exampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee {    int id;    String name;    double salary;    public Employee(int id, String name, double salary) {       super();   ... Read More

Advertisements