Java Articles - Page 150 of 540

How to access variables from enclosing class using lambda in Java?

raja
Updated on 14-Jul-2020 13:00:28

832 Views

The variables defined by the enclosing scope of a lambda expression can be accessible within the lambda expression. A lambda expression can access to both instance, static variables and methods defined by the enclosing class. It has also access to "this" variable (both implicitly and explicitly) that can be an instance of enclosing class. The lambda expression also sets the value of an instance or static variable.Exampleinterface SimpleInterface {    int func(); } public class SimpleLambdaTest {    static int x = 50;    public static void main(String[] args) {       SimpleInterface test = () -> x;     // Accessing of static variable ... Read More

How to create a thread using method reference in Java?

raja
Updated on 14-Jul-2020 12:54:42

828 Views

Method reference is one of a way in a lambda expression to refer a method without executing it. In the body part of a lambda expression, we can call another method if they are compatible with a functional interface. We can also capture "this" and "super" keywords in method references.In the below two examples, we can create a thread with the help of "this" and "super" keywords using method reference.Example of this keywordpublic class MethodRefThisTest {    public void runBody() {       for(int i = 1; i < 10; i++) {          System.out.println("Square of " + i + " ... Read More

How to implement DoubleUnaryOperator using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 12:48:28

378 Views

DoubleUnaryOperator is a functional interface defined in java.util.function package. This functional interface expects a parameter of type double as input but produces the output of the same type. DoubleUnaryOperator interface can be used as an assignment target for lambda expression and method reference. This interface contains one abstract method: applyAsDouble(), one static method: identity() and two default methods: andThen() and compose().Syntax@FunctionalInterface public interface DoubleUnaryOperator {  double applyAsDouble(double operand); }Example of Lambda Expressionimport java.util.function.DoubleUnaryOperator; public class DoubleUnaryOperatorTest1 {    public static void main(String[] args) {       DoubleUnaryOperator getDoubleValue = doubleValue -> { // lambda expression          return doubleValue * 2;       }; ... Read More

How to implement LongSupplier using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 12:25:24

547 Views

LongSupplier is a built-in functional interface from java.util.function package. This interface doesn’t expect any input but produces a long-valued output. Since LongSupplier is a functional interface, it can be used as an assignment target for lambda expression and method reference and contains only one abstract method: getAsLong().Syntax@FunctionalInterface public interface LongSupplier {  long getAsLong(); }Example of Lambda Expressionimport java.util.function.LongSupplier; public class LongSupplierLambdaTest {    public static void main(String args[]) {       LongSupplier supplier = () -> {     // lambda expression          return 75;       };       long result = supplier.getAsLong();       System.out.println(result);    } }Output75Example ... Read More

How to Install Rundeck on a Debian 8 (Jessie) Server

Sharon Christine
Updated on 22-Jan-2020 07:16:25

310 Views

Rundeck allows you to run commands/scripts on a remote computer. It is used to create a job by defining a single step or a workflow that can execute any set of commands, scripts, or tools on any number of local or remote nodes. Jobs can be triggered by the scheduler or on-demand via the web interface or API. This article explains about ‘How to install Rundesk on Debian 8 server’Rundeck is written in java programming language, so it requires you to install java in your machine. To install Java programming on Debian, use the following commands –$ sudo dpkg --add-architecture ... Read More

How to Install Java with Apt-Get on Ubuntu 16.04

Sharon Christine
Updated on 22-Jan-2020 06:15:40

1K+ Views

Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core a component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]).The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. Ex − J2EE for Enterprise Applications, J2ME for Mobile Applications.The new J2 versions was renamed as Java SE, Java EE and Java ME respectively. Java is guaranteed to be a Write Once, Run Anywhere.This article explains about ‘How to ... Read More

How to implement LongPredicate using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 12:06:47

320 Views

LongPredicate is a functional interface defined in java.util.function package. This interface can be used mainly for evaluating an input of type long and returns an output of type boolean. LongPredicate can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: test() and three default methods: and(), negate() and or().Syntax@FunctionalInterface interface LongPredicate {  boolean test(long value); }Example of Lambda Expressionimport java.util.function.LongPredicate; public class LongPredicateLambdaTest {    public static void main(String args[]) {       LongPredicate longPredicate = (long input) -> {    // lambda expression          if(input == 50) {       ... Read More

How to create a constructor reference for an array in Java?

Aishwarya Naglot
Updated on 20-Aug-2025 12:22:04

4K+ Views

In Java, a constructor refers to a special method within a class that is always executed automatically while creating an instance of the class using the new keyword. Its main purpose is initializing the object by providing its fields with initial values so that it is ready for use. A constructor shares the name of the class itself, has no return type, and can be overloaded so that there will be different ways of initializing the objects. If you don’t provide one, Java inserts a default no-argument constructor automatically. What is a Constructor Reference in Java? Constructor reference is like ... Read More

Difference between Object level lock and Class level lock in Java

Himanshu shriv
Updated on 21-Jan-2020 10:10:57

7K+ Views

In multithreading environment, two or more threads can access the shared resources simultaneously which can lead the inconsistent behavior of the system. Java uses concept of locks to restrict concurrent access of shared resources or objects. Locks can be applied at two levels −Object Level Locks − It can be used when you want non-static method or non-static block of the code should be accessed by only one thread.Class Level locks − It can be used when we want to prevent multiple threads to enter the synchronized block in any of all available instances on runtime. It should always be used ... Read More

How to implement a constructor reference with one or more arguments in Java?

Aishwarya Naglot
Updated on 20-Aug-2025 12:24:21

10K+ Views

In this article, we are going to learn about implementing a constructor reference with an argument or multiple arguments in Java. Constructor references in Java provide us with a concise way of creating new objects via method references. These are going to enable us to reference a constructor without running the constructor itself so the code looks cleaner and it will be more readable. What is a Constructor Reference? A method reference may also be used with Java 8 constructors. A constructor reference may be defined by employing the class name and a new keyword. The constructor reference may be ... Read More

Advertisements