• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unfamiliar Syntax..

 
Josh Tambini
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic