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

Started by
6 comments, last by yves032784 20 years, 6 months ago
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;
}
 
Advertisement
% 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.
% 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.
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][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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]
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 Formats______________________________________________________________________________________
shouldn''t it use a do { ... } while instead? otherwise 0 will generate an empty string.
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][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement