-2

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?

Berit Larsen
  • 739
  • 1
  • 12
  • 29
  • 8
    *""pass the larger one of an empty array versus void""* WTF? Please read: http://stackoverflow.com/questions/3970219/c-sharp-lambda – Tom Mar 10 '16 at 12:40

2 Answers2

1

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.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
0

First,

static void ReadEvents

does not mean ReadEvents returns true under any circumstances. The void keyword means that the method has no return (like a Sub in VB).

Second, you define your array as:

byte[] buffer = new byte[4096];

The default value for byte is 0, so you never actually have an empty array, you instead, have an array of 4096 bytes with the value 0. Unless somewhere further in the code (which you are not showing) you redefine the array as byte[] or null.

Kevin
  • 1,462
  • 9
  • 9