15.Java的equals测试代码

对于String的==操作测试


public class TestEqual {

    public static void main(String[] args) {
        TestEqual testEqual = new TestEqual();
        testEqual.test1();
        testEqual.test2();
        testEqual.test3();
        testEqual.test4();
        testEqual.test5();
        testEqual.test6();
        testEqual.test7();
        testEqual.test8();
        testEqual.test9();
        testEqual.test10();
        testEqual.test11();

    }

    private void test1() {
        String s = "s";
        String s1 = "s";
        System.out.println(s == s1);
    }

    private void test2() {
        String s2 = new String("s");
        String s3 = new String("s");
        System.out.println(s2 == s3);
    }

    private void test3() {
        String s = "friend";
        String s1 = "friend";
        String s2 = "fri" + "end";
        System.out.println(s == s1);
        System.out.println(s1 == s2);
    }

    private void test4(){
        String s = "friend";
        String s1 = new String("friend");
        String s2 = "fri" + new String("end");
        System.out.println(s==s1);
        System.out.println(s==s2);
        System.out.println(s1==s2);
    }
    private void test5(){
        String s = "fri";
        String s1 = "end";
        String s2 = s1 + s;
        System.out.println(s2 == "friend");
    }
    private void test6(){
        String s = "A1";
        String s1 = "A"+1;
        String s2 = "A3.4";
        String s3 =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Miller-Rabin 素数测试Java 代码实现: ```java import java.math.BigInteger; import java.util.Random; public class MillerRabin { private static final int CERTAINTY = 50; // 确定性 public static boolean isPrime(BigInteger n) { if (n.compareTo(BigInteger.ONE) == 0 || n.compareTo(BigInteger.TWO) == 0) { return true; } if (n.mod(BigInteger.TWO).equals(BigInteger.ZERO)) { return false; } BigInteger d = n.subtract(BigInteger.ONE); int r = 0; while (d.mod(BigInteger.TWO).equals(BigInteger.ZERO)) { r++; d = d.divide(BigInteger.TWO); } for (int i = 0; i < CERTAINTY; i++) { BigInteger a = getRandom(n.subtract(BigInteger.TWO)).add(BigInteger.ONE); BigInteger x = a.modPow(d, n); if (x.equals(BigInteger.ONE) || x.equals(n.subtract(BigInteger.ONE))) { continue; } boolean flag = false; for (int j = 0; j < r - 1; j++) { x = x.modPow(BigInteger.TWO, n); if (x.equals(BigInteger.ONE)) { return false; } if (x.equals(n.subtract(BigInteger.ONE))) { flag = true; break; } } if (!flag) { return false; } } return true; } private static BigInteger getRandom(BigInteger n) { Random rnd = new Random(); BigInteger result = new BigInteger(n.bitLength(), rnd); while (result.compareTo(n) >= 0) { result = new BigInteger(n.bitLength(), rnd); } return result; } } ``` 其中,`isPrime` 方法接收一个 `BigInteger` 类型的数值作为输入,返回一个布尔值,表示输入的数值是否为素数。`CERTAINTY` 常量表示测试的确定性,即测试的正确率。一般来说,50 次测试足够保证正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值