• Post Reply Bookmark Topic Watch Topic
  • New Topic

I never saw a code like this, please help me understand the following code segment.  RSS feed

 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you very much for looking at this post.
I was studying for SCJP from Khalid Mughal and the following code is example 13.3
Most probably you will not need to read and understand the whole code to answer my question. I am posting the complete code just in case it is needed to answer my question.
The code of my concern is in the second listing.

by the way, the following code implements a stack and then 2nd class is creating 2 threads which are pushing and popping from the stack.




so my question, what is the significance of the following code



I have never seen anything like this.
Please let me know what is the concept they are using and where to study that from.

Thank you very much.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit kumararya wrote:

I have never seen anything like this.
Please let me know what is the concept they are using and where to study that from.

That is an anonymous subclass of Thread. If you saw this (new Thread("Pusher")).start(); you would know that the code makes a new instance of the Thread class, names it "Pusher", and starts the Thread. But the new Thread wouldn't do anything. So the code makes an anonymous subclass like this:

Now a new class (with no name == Anonymous) is created, a new instance is made and started. It still doesn't do anything, so the code overrides the public void run() method:


Note that this is generally a frowned upon approach*, you shouldn't extend Thread here, rather make a Runnable:


*EDIT: This is generally frowned upon specifically for Thread. Judicious use of Anonymous classes are okay. But you should generally not subclass Thread if all you want to do is have some task which runs in a Thread - that is what Runnable is for.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the part i have never seen before. perhaps it is what the question is about.
for(;;) { }
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in the Java Tutorials; if you miss out the middle part of the header of a for loop, the compiler will assume you mean true and you get an infinite loop.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It's in the Java Tutorials; if you miss out the middle part of the header of a for loop, the compiler will assume you mean true and you get an infinite loop.


I knew that for(;;) creates an infinite loop. But I didn't know this is because the compiler assumes the middle part ( the conditional expression ) to be true. Thanks for that, Campbell.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look in the Java Language Specification; that should confirm or refute what I said. It seems to behave like that, although the statement may terminate the loop, with break or an Exception or similar. Second • after §14.14.1.2.
It says the “statement” is executed if the “expression” evaluates to true or is absent.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Let's have a look in the Java Language Specification; that should confirm or refute what I said. It seems to behave like that, although the statement may terminate the loop, with break or an Exception or similar. Second • after §14.14.1.2.
It says the “statement” is executed if the “expression” evaluates to true or is absent.


I read the relevant parts in the JLS. Would it be right to say that yes, if we don't specify anything in the middle part, the middle expression evaluates to true. And in some cases it can result in an infinite loop. is one such case.
But I think it's right that it is rather the combination of the three parts of the for loop that determines if the loop is going to be an infinite loop. , for instance, also creates an infinite loop.
So does a .

I know you never said the reason for the infinite loop is the middle part of the for loop. It is me who didn't read it correctly. Sorry about that.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think I finally get it now. If the middle part is an empty expression, the first and third part don't matter so long as they don't result in an exception.

I take a good amount of time sometimes to understand complicated stuff. Hence the confusion.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. The first and third parts do matter, because they (ought to) determine what happens in the loop. The following loop will behave differently depending on the value of j.And this loop will behave differently againBoth loops will run endlessly, unless something inside the loop like a break or Exception stops them.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If, for some reason, you must have an infinite loop in your program, then in my opinion it's much better to use

which is much more clear than the obscure for(;;) syntax.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:If, for some reason, you must have an infinite loop in your program, then in my opinion it's much better to usewhich is much more clear than the obscure for(;;) syntax.

Hmm. Not quite sure I agree with that, particularly if any loop variables are involved. What I do think is that you should put the true in the middle explicitly.

I really dislike syntax where the absence of something has an implied meaning - particularly one that has consequences. If I was designing the language, I think my inclination would have been to make that default 'false', so that the loop does nothing; but 'for(;;)' is pretty widely known in C circles, so I guess they thought it might confuse people.

Winston
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot steve. I got your answer when you replied.

so i was studying inner classes and anonymous classes to understand what you wrote. and thought it will be better to reply when i understood.

I haven't checked this thread since then. So i am very sorry regarding this discussion went in some other direction.

Now i understood it.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . I really dislike syntax where the absence of something has an implied meaning . . . but 'for(;;)' is pretty widely known in C circles, so I guess they thought it might confuse people.

Winston
It would probably have been better to require all three parts of the for loop header otherwise there would be a compile‑time error. But as you say, they obviously wanted to attract C/C++ chappies, so they made the syntax appear familiar.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit kumararya wrote:Thanks a lot steve. I got your answer when you replied. ...I haven't checked this thread since then. So i am very sorry regarding this discussion went in some other direction.

No problem, I am glad I could help. About the topic changing directions - that is just how forums work. The best part about it is you got what you needed, Randell and Heena also learned new things, and a nice conversation followed. No need to apologize, this was a good thread, in my opinion. Lots of people got something out of it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!