• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

byte data typeOCA 1Z0 -808

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain me this program logic.
According to me i belive the line 1(commented) will increment and print . but the actual scenario i get no output.
class Fish
{
static byte count;
}
class mainn
{
  public static void main(String[] args)
   {
     boolean hungry = false;
while(hungry = true)
{
          ++Fish.count;
}
System.out.println(Fish.count); // line 1
  }
}
 
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an infinity loop. In while(hungry = true) you assign true to the hungry variable.
So the loop is the same as while(true)
 
Priyanka Leo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply I am able to understand the logic now.
 
Marshal
Posts: 71103
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from. Did you intend to write == true instead? Don't do that because it is poor style and very error‑prone.
 
Priyanka Leo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no this was one of the question from the book by Mala Gupta. and the single = was mentioned in the question
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic