• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Doubt in For Loop..

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Result is :- Prints: 1212013
But I thought :- Prints: 1212010 (Can any one explain it to me..)
(Source:- www.danchisholm.net)
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

The static int i; is what's get printed in the statement of System.out.print(JMM125.i);
It has the value of 3 since it's used in the loop statement of :


The others has no relevance of the static i variable.

This statement makes use of block variable inside (declare inside loop, and vanishes outside loop)


This code makes use of local variable (declare inside method, and vanishes outside method)


Cheers,
Albert Kam
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
explain very clearly.
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code makes use of local variable (declare inside method, and vanishes outside method)


Cheers,
Albert Kam

Thanks for reply Albert, but it still confused me...if (JMM125.i); Is it invoking the local variable 'i' or class variable i, if its class variable then the result should be 1212010...

As this code will print 0....
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ..

The last code you posted will return 0, because the static variable i has not been incremented
In the original code, the static variable is incremented by the loop 1

So, to make your code more similar to the orignal code, i modified your code by adding the loop code from the original question


Here's the modified code :


Cheers,
Albert Kam
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oksssssss...Ya I got it,,that was a silly mistake,,,Thanks Ablert
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem ...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic