• 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

Calling variables.

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class flag{
int x,y;
String name;
public static void main(String args[]){

flag a= new flag();
System.out.println(a.x+a.y+a.name+a.x);

}
}
Output of this program is 0null0.
I think it should be 00null0(i.e two 0s before null. Can someone
kindly explain this mystery to me.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think so.
a.x+a.y is evaluated first. This is interpreted 0+0. So the answer is integer 0.
Does that make it clearer?
/Mike
[This message has been edited by Mikael Jonasson (edited May 18, 2001).]
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
well no major mystery here
i just modified your code to add a space as a String literal b/w
a.x and a.y and voila !!! ... there is the result you wanted
public class flag{
int x,y;
String name;
public static void main(String args[]){
flag a= new flag();
System.out.println(a.x + " " + a.y+a.name+a.x);
}
}
as you might have figured out .... what was happening in you code was that a.x+a.y would add up to zero ... thats all
hope that helps
Samith.P.Nambiar
---------------------------------------
the harder you try the luckier you get
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic