String<> name = "a", "b", "c", "d" ;System.out.println(name<4 % name.length>);System.out.println(name<7 % name.length>);System.out.println(name<50 % name.length>);Output:

adc


You are watching: What does percent mean in java

*

*

That"s the remainder operator, it gives the remainder of integer division. Because that instance, 3 % 2 is 1 due to the fact that the remainder the 3 / 2 is 1.

It"s being supplied there to store a worth in range: If name.length is less than 4, 7, or 50, the result of % name.length ~ above those values is a value that"s in the variety 0 come name.length - 1.

So that code picks entries indigenous the variety reliably, also when the number (4, 7, or 50) are out that range. 4 % 4 is 0, 7 % 4 is 3, 50 % 4 is 2. Every one of those space valid range indexes for name.

Complete example (live copy):

class Example public static void main (String<> args) throw java.lang.Exception String<> name = "a" , "b" , "c" , "d"; int n; n = 4 % name.length; System.out.println(" 4 % 4 is " + n + ": " + name); n = 7 % name.length; System.out.println(" 7 % 4 is " + n + ": " + name); n = 50 % name.length; System.out.println("50 % 4 is " + n + ": " + name); Output:

4 % 4 is 0: a 7 % 4 is 3: d50 % 4 is 2: c
re-superstructure
boost this prize
monitor
edited Aug 19 "17 in ~ 12:05
answered Aug 19 "17 at 11:59

*

T.J. CrowderT.J. Crowder
911k168168 yellow badges16721672 silver- badges16881688 bronze title
3
add a comment |
1
Simple: this is the modulo, or come be an accurate the remainder operator.

This has actually nothing to perform with arrays per se. That is just a numerical computation on the value that gets provided to compute the array index.


re-superstructure
improve this answer
monitor
reply Aug 19 "17 at 11:59
*

GhostCatGhostCat
129k2323 gold badges158158 silver- badges227227 bronze title
include a comment |

Not the prize you're looking for? Browse various other questions tagged java or questioning your very own question.


The Overflow Blog
Upcoming occasions
Featured ~ above Meta
Linked
76
Understanding The Modulus Operator %
245
What's the syntax for mod in java
0
about Queue data Strucuture in java utilizing circular array
associated
4058
What space the differences in between a HashMap and also a Hashtable in Java?
3413
What is the difference in between public, protected, package-private and also private in Java?
3219
What is a serialVersionUID and why have to I use it?
1062
What walk 'synchronized' mean?
1286
'Must Override a Superclass Method' Errors after importing a project into Eclipse
1140
Converting selection to list in Java
25939
Why is processing a sorted selection faster than processing an unsorted array?
1629
What walk "Could not uncover or load main class" mean?
2875
Why is printing "B" significantly slower than printing "#"?
459
What walk a "Cannot uncover symbol" or "Cannot settle symbol" error mean?
hot Network inquiries much more hot questions
*

lang-java
ridge Overflow
products
firm
ridge Exchange Network
site style / logo © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.25.40561




See more: Where To Get Exp Share In Leaf Green ? How Do You Use The Exp Share In Leaf Green

Stack Overflow works ideal with JavaScript enabled
*

your privacy

By click “Accept every cookies”, you agree stack Exchange have the right to store cookie on your machine and disclose info in accordance v our Cookie Policy.