String<> surname = "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 the percent sign mean in java

*

*

That"s the remainder operator, it provides the remainder of essence division. For instance, 3 % 2 is 1 because the remainder that 3 / 2 is 1.

It"s being offered there to save a worth in range: If name.length is much less than 4, 7, or 50, the result of % name.length on those values is a value that"s in the range 0 to name.length - 1.

So that password picks entries from the selection reliably, even when the number (4, 7, or 50) are out that range. 4 % 4 is 0, 7 % 4 is 3, 50 % 4 is 2. All of those room valid selection indexes because that name.

Complete example (live copy):

class Example public revolution void key (String<> args) throws 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
share
follow
edited Aug 19 "17 at 12:05
reply Aug 19 "17 in ~ 11:59

*

T.J. CrowderT.J. Crowder
914k168168 yellow badges16801680 silver badges16931693 bronze title
3
include a comment |
1
Simple: this is the modulo, or to be precise the remainder operator.

This has actually nothing to do with arrays per se. The is simply a numerical computation top top the value that gets offered to compute the selection index.


re-superstructure
monitor
answer Aug 19 "17 in ~ 11:59
*

GhostCatGhostCat
130k2323 yellow badges160160 silver badges228228 bronze badges
add a comment |

Not the answer you're feather for? Browse various other questions tagged java or ask your very own question.


The Overflow Blog
Featured on Meta
Visit chat
Linked
76
Understanding The Modulus Operator %
245
What's the syntax for mod in java
0
about Queue data Strucuture in java making use of circular array
connected
4066
What space the differences between a HashMap and also a Hashtable in Java?
3419
What is the difference in between public, protected, package-private and private in Java?
3226
What is a serialVersionUID and also why should I use it?
1066
What walk 'synchronized' mean?
1287
'Must Override a Superclass Method' Errors after importing a project right into Eclipse
1146
Converting array to perform in Java
25969
Why is handling a sorted range faster than handling an unsorted array?
1635
What walk "Could not find or fill main class" mean?
2880
Why is to press "B" substantially slower than printing "#"?
465
What does a "Cannot find symbol" or "Cannot solve symbol" error mean?
warm Network inquiries an ext hot questions
*

lang-java
ridge Overflow
commodities
agency
stack Exchange Network
site style / logo design © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.5.40661




See more: Where Is Steven In Pokemon Ruby ? Steven Stone

Stack Overflow works best with JavaScript enabled
*

her privacy

By clicking “Accept every cookies”, friend agree ridge Exchange deserve to store cookies on your an equipment and disclose details in accordance v our Cookie Policy.