• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

byte related Qn

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic