Java中mod和%區別

來源:互聯網
上載者:User

標籤:java   mod   

    今天閑來無事,看到篇HashMap工作原理的部落格,在上面看到了mod(模數),就想到取餘符號%。

    然後就查看了一些資料,現在總結下:

    

    Java中的模數和求餘方式:

    1、模數demo

    

        package com.lbb.test;            import java.math.BigInteger;        public class RomTest {        public static void main(String[] args) {            BigInteger b1;            BigInteger b2;            BigInteger b3;            b1 = new BigInteger("-10");            b2 = new BigInteger("4");            b3 = b1.mod(b2);                        System.out.println("b3:" + b3);                        Integer i1 = -20;Integer i2 = -3;Integer i3 = i1 % i2;System.out.println("i3:" + i3);        }        }

     注意:通過模數mod()方法計算出來的值都是非負數,特別是模數裡面的參數b2必須正數。


     求餘%符號,對被除數和除數沒有特殊要求,只要求除數不能為零,否則會出現 java.lang.ArithmeticException(除零異常)

    注意:通過%求餘出來的數值,正數、負數、0都可以。


本文出自 “專註於開發技術” 部落格,請務必保留此出處http://bingge2015.blog.51cto.com/4332222/1900016

Java中mod和%區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.