Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic