Search code examples
javac#code-translation

C# code passing void as argument? What's that in Java?


I'm translating a small program from C# to Java. There's 1 line left that I'm wondering about:

Thread eventReadingThread = new Thread(() => ReadEvents(url, streamingMode));
...
        static void ReadEvents(String serviceURL, bool streamingMode)
    {
        if (streamingMode)
        {
            WebRequest httpClient = WebRequest.Create(serviceURL);
            httpClient.Method = "GET";
            byte[] buffer = new byte[4096];
...

I interpret the first line here as "True if ReadEvents returns less than empty array". However it doesn't make any sense, both because void arguments don't compile and because a boolean argument doesn't fit the constructor for Thread.

What would this be in Java?


Solution

  • What would it be in Java?

    In Java 8 you just turn => to ->.

    {
        Thread thread = new Thread(() -> readEvents(url, streamingMode));
    }
    
    static void readEvents(String serviceUrl, boolean streamingMode) {
        // ...
    }
    

    I interpret the first line here as .... What is the code trying to do?

    You need to read up on lambda expressions (Java, C#). In this case it is "create me a Runnable or ThreadStart that calls the method readEvents.