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:
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
4 % 4 is 0: a 7 % 4 is 3: d50 % 4 is 2: c
boost this prize
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
add a comment |
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.
improve this answer
reply Aug 19 "17 at 11:59
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
Featured ~ above Meta
Understanding The Modulus Operator %
What's the syntax for mod in java
about Queue data Strucuture in java utilizing circular array
What space the differences in between a HashMap and also a Hashtable in Java?
What is the difference in between public, protected, package-private and also private in Java?
What is a serialVersionUID and why have to I use it?
What walk 'synchronized' mean?
'Must Override a Superclass Method' Errors after importing a project into Eclipse
Converting selection to list in Java
Why is processing a sorted selection faster than processing an unsorted array?
What walk "Could not uncover or load main class" mean?
Why is printing "B" significantly slower than printing "#"?
What walk a "Cannot uncover symbol" or "Cannot settle symbol" error mean?
hot Network inquiries much more hot questions
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