PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] Why is this not working in Java

 September 20, 2022     consumer, java     No comments   

Issue

import java.util.function.Consumer;

public class test {
    public static void main(String[] args) {
        Consumer<String> c = (x) --> System.out.println(x.toLowerCase());
        c.accept("Java2s.com");
    }
}

with error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: x cannot be resolved to a variable x cannot be resolved at test.main(test.java:5)

But in all official documents, this is working... Could someone help me?


Solution

Change --> to -> and it should work. It would work only in java8 and later. It's a way to define lambda expression which would consume a string (in your case) and would change the case to lower.



Answered By - SMA
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing