• 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

Unfamiliar Syntax..

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...i have the following code:


i cant understand this syntax:
"set((byte)0); "
why to call "set" with the (byte)0???
cant i just put only 0(for eg: set(0)??? if i can..so what are the diffrences?
CODE]class Cord

{



private int x,y;

private byte color;



public void set(int a,int b, byte color)

{

this.x=a;



this.y=b;


this.color=color;

}

public void set(int a,int b)

{

x=a;

y=b;

}



public void set(byte clr)

{

color=clr;

}



public void set()

{

x=y=0;

set((byte)0);

}



public void set(int a)

{

x=a;

y=a;

}



public boolean equals(Cord otherCord)

{

if (otherCord.x==x && otherCord.y==y)

return true;

else

return false;

}

}
[/CODE]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can call set(0), if you want, but a different method will be called. All constant integers automatically have int type. However, if you want to use the constant as a byte instead, you have to cast it. So set(0) will call the set() method that takes an int parameter, but set((byte)0) will call the set() method that takes a byte argument.

Personally, I think this is a poor programming practice since these two methods seem to be for different purposes and not at all related. It would be much more clear if set(byte clr) were setColor(byte clr) instead. Likewise, set(int a) should be setCoords(int a). Using names to clarify code is always a good thing.

HTH

Layne
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic