This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

for loop and byte to int coversion.

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

public static void main(String[] args){
for(byte i =0 ; i<10; i++){
System.out.println("Value of i = " + i);

}}}

Why the above code works fine and below one not. Only difference is increment difference in for loop.

public class ContinueGotcha {
public static void main(String[] args){
for(byte i =0 ; i<10; i=i+1){
System.out.println("Value of i = " + i);

}}}

Can someone explain me in detail . Thanks in advance. I was thinking that both will not work but the above code is working.
 
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because i=i+1 is automatically converting byte to int, where ++ isn't. You must also explicitly tell to compiler that the result is really byte type..


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

Tapio Niemela wrote:Because i=i+1 is automatically converting byte to int, where ++ isn't. You must also explicitly tell to compiler that the result is really byte type..





Thanks Niemela for a good explanation.

I think same explanation works with all operators like. +=, -=, ++, --, *= etc.

like

byte i =10;
//i += 1; // will not give any compilation error.
i = i +1; //will give error cannot convert int to byte. you need explicit cast here to make it work.

similar with others
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For those who like a pointer to the specification...

The reason "i++" works is defined in section 15.14.2. Notice that an implicit primative narrowing conversion is one of the operations which may be applied.

The reason "i += 1" works (although not shown in example in this topic) is defined in section 15.26.2. Notice the cast when the expression is expanded.

Henry
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic