Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Unfamiliar Syntax.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unfamiliar Syntax.." Watch "Unfamiliar Syntax.." New topic
Author

Unfamiliar Syntax..

Josh Tambini
Greenhorn

Joined: Nov 30, 2004
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]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
 
GeeCON Prague 2014
 
subject: Unfamiliar Syntax..