Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] what does the '%' identifier do?

Started by yves032784 Oct 13, 2003 at 4:05 PM 6 replies 800+ views
Original Post
yves032784
yves032784
in an operation what does the ''%'' identifier do? i''ve also seen this used with a string, e.g
 int y=137;
String x="";
while (y>0)
{
     x+=(y % 10);
     y/=10;
}
 
Miserable
Miserable
% is an operator, not an identifier. It is, in fact, an arithmetic operator called the modulus operator; it returns the remainder of an integer division (i.e. 10/3=3, 10%3=1). One very simple and common application is to check whether a number is odd or even; if(n%2 == 0), it is even; else it is odd.
DukeAtreides076
DukeAtreides076
% is the modulus operator. If you don''t know about modular arithmetic, basically its just a remainder. Ex: 137 % 10 would be 7, since 137 / 10 = 13 remainder 7.
CaptainJester
CaptainJester
The thing with the String is that Java automatically converts numbers to a String if they are added to a String. So:

x="My ";
x+=1;
System.out.println("x="+x);

gives you:

x=My 1





First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Arild Fines
Arild Fines
quote:
Original post by CaptainJester
The thing with the String is that Java automatically converts numbers to a String if they are added to a String.


And they say Java has no operator overloading

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thunder_Hawk
Thunder_Hawk
The listed code should fill a string with the digits of the number in reverse order. Any number modulus 10 will return the digit in the ones place. Think about it for a while and it should make sense.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
CaptainJester
CaptainJester
quote:
Original post by Arild Fines
And they say Java has no operator overloading



Very little. The main thing is that programmers cannot create their own overloaded operations.



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.