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

byte related Qn

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

I'm preparing for SCJP, & practicing some Qns, & my Qn is why this code is working fine, & printing the answer c=10.


public class simple {
public static void main(String arg[]){
byte a,b=5,c; // line 1

a=c=b=10; // line 2
System.out.println("c="+c); // line 3


}
}

it should give error at line 2

please Answer my Qn, I am getting confused


Thanxs
Sheetal
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks perfectly legal, what error do you expect ?
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, it should give error at line 1, byte a,b=5,c;

is it legal statement? yes then why?
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sheetal Kaul:
Oh sorry, it should give error at line 1, byte a,b=5,c;

is it legal statement? yes then why?



You have declared 3 variables a, b and c of type byte, and are initializing b alone to 5. That's fine. You can mix declaration and initialization in one statement.
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ya now i got that, thanks alot for the Quick reply.

Thanks again
sheetal
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is combination of variable declaration and initialization.It is perfectly legal declaration & initialization of instance member variable or local variable.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic