Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:46:32+00:00 2026-06-18T07:46:32+00:00

How to convert the below JavaScript example to corresponding code in Java 8 with

  • 0

How to convert the below JavaScript example to corresponding code in Java 8 with lambda. I am trying to get the Java 8 lambda.
Made up example.

// js
var isFlag = true,
    name = (function () {
        if (isFlag) return "A";
        else return "B";
    })();
console.log(name);

//outputs: A

I have tried writing it in Java 8, but the syntax or something is wrong. See the below code.

// LambdaTest.java
class LambdaTest {
    public LambdaTest() {
        boolean isFlag = true;
        String name = () -> { //I know this is wrong. It is not an IIFE. But how to write one?
            if (isFlag) return "A";
            else return "B";
        };
        System.out.println(name);
    }
    public static void main(String... args) {
        LambdaTest lt = new LambdaTest();
    }
}

// outputs:
// LambdaTest.java:4: error: incompatible types: String is not a functional interface
//        String name = () -> {
//                      ^
// 1 error

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:46:33+00:00Added an answer on June 18, 2026 at 7:46 am

    I am not java programmer but seems that something like this do the trick:

    import java.util.concurrent.Callable;
    import java.lang.String;
    
    class LambdaTest {
        public LambdaTest() {
            boolean isFlag = true;
            Callable<String> name = () -> {
                if (isFlag) return "A";
                else return "B";
            };  
            try {
                System.out.println(name.call());
            } catch (Exception e) {
                //cause call can throw exception so we need to handle that
            }   
        }   
        public static void main(String... args) {
            LambdaTest lt = new LambdaTest();
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the value of input using javascript using below code. var name=document.getElementById(firstName).value;
I'm trying to convert the below bit of code into VB (and obviously apply
Any help would be appreciated, I'm trying to convert the code below to C#,
The following code is from 'JavaScript by Example Second Edition',I think the code below
How do I convert the php code below into javascript? I google it and
I have this piece of code where I'm trying to convert the below C#
I want to convert below string to an array in javascript. {a:12, b:c, foo:bar}
How do I convert the below PHP code to VB.NET? <?php $X_HOST =foo.com; $X_URL
I'm struggling to convert the below code to C#. Class Class1 Implements IMyInterface Public
I am trying to convert an array of the RECT structure (given below) into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.