• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
according to OCA  8book( Boyarsky/Selikoff, page 82) the following code outputs  0 1 2 3 4  :



I see a different result in the code below:  0 1 2 3 4 5


I understand that x cannot exceed 4 (x<5) but the output is  0 1 2 3 4 5 according to the class above.

Can anyone please explain why?

Thank You,
Iulian
 
Saloon Keeper
Posts: 27707
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you use code tags to wrap pre-formatted text such as Java code, XML, SQL and so forth, it's easier for us to read. I've edited your message to do that.

Let me re-write your code into something that means the same thing:

The "for" statement is really just a shorthand way of writing this, and you can probably see now why the final value of x is actually 5.

Note that just for giggles I made the constant 4 be "4L" to indicate that it's a long value. The compiler will accept 4 (which is an int value) and fold up to 4L, but this is forcing it to be 4L anyway.  Likewise, the 10 has to be promoted if you don't indicate explictly. Compilers are smart these days, so they can do that efficiently and automatically, but it's still good practice.

I could have done the same for 0 (0L), but 0 is sort of  the Universal Nothing so commonly we don't bother to qualify it.
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Compilers are smart these days, so they can do that efficiently and automatically, but it's still good practice.

The compiler only does the promotion if you write 4L; otherwise the promotion is done at runtime, which is why it is good practice to add the L.

I could have done the same for 0 (0L), . . . .

I would write 0L myself.

Are you suggesting this is an erratum? Have you looked here for the errata? I don't think it is recorded there.
 
badeanu iulian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim,
Thank You for your thoughtful response.
I .Maybe it would be helpful for other beginners (like me) to know that the result presented in the book is not accurate: 0 1 2 3 4 instead of   0 1 2 3 4 5 (correct).

II Here is another example from the same book (pages 110-111):

String start = "AniMaL ";
String trimmed = start.trim(); // "AniMaL"
String lowercase = trimmed.toLowerCase(); // "animal"
String result = lowercase.replace('a', 'A'); // "Animal" WRONG--> the correct result is  AnimAl.
System.out.println(result);

I know those are tiny mistakes, but they are the difference between a correct answer and a wrong one on the OCA exam.

Best Regards,

Iulian
 
Tim Holloway
Saloon Keeper
Posts: 27707
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to send a Purple Moosage to Scott or Jean. They are always interested in errata.
 
author & internet detective
Posts: 41855
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You might want to send a Purple Moosage to Scott or Jean. They are always interested in errata.


Jeanne, not Jean. And no need for a PM. We do check this forum. Not as often as I'd like, but regularly.

badeanu iulian wrote:Hello Tim,
Thank You for your thoughtful response.
I .Maybe it would be helpful for other beginners (like me) to know that the result presented in the book is not accurate: 0 1 2 3 4 instead of   0 1 2 3 4 5 (correct).


This is already in the errata as "In the first example on the page, System.out.print(x) should be removed" for page 82.

badeanu iulian wrote:II Here is another example from the same book (pages 110-111):

String start = "AniMaL ";
String trimmed = start.trim(); // "AniMaL"
String lowercase = trimmed.toLowerCase(); // "animal"
String result = lowercase.replace('a', 'A'); // "Animal" WRONG--> the correct result is  AnimAl.
System.out.println(result);


This was almost in the errata. It was there for the paragraph below, but not the comment so I amended the description in the errata

badeanu iulian wrote:I know those are tiny mistakes, but they are the difference between a correct answer and a wrong one on the OCA exam.


We agree! That's why we have a super detailed list. It also helps us put out better later books. I checked and both of these were already fixed in the Java 11 version of the book.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic