programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Unfamiliar Syntax..

Greenhorn
Posts: 24
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
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