实训第五天

1.

package com.java.object01;

import java.math.BigDecimal;

public class Bigdecimal01 {
    public static void main(String[] args) {
        double d1 = 0.1 + 0.2;
        System.out.println(d1);
        System.out.println("-------------------");

        BigDecimal bd1 = new BigDecimal("0.3");
        BigDecimal bd2 = new BigDecimal("0.1");
        // 加法
        System.out.println(bd1.add(bd2));
        // 减法
        System.out.println(bd1.subtract(bd2));
        // 乘法
        System.out.println(bd1.multiply(bd2));
        // 除法
        System.out.println(bd1.divide(bd2));
        // 四舍五入
        System.out.println(bd1.divide(bd2,BigDecimal.ROUND_HALF_UP));

    }
}

2.

package com.java.object01;

public class Demo01 {
    public static void main(String[] args) {
        System.out.println(add());
        System.out.println(add(1));
        System.out.println(add(1,2));
        System.out.println(add(1,2,3));
    }

    private static int add(int...args) {
        int sum = 0;
        for (int i = 0; i < args.length; i++) {
            sum += args[i];
        }
        return sum;
    }
}

3.

package com.java.object01;

public class IntegerDemo01 {
    public static void main(String[] args) {
        Integer it1 = new Integer(10);
        System.out.println("it1 = " + it1);


        Integer it2 = new Integer("20");
        System.out.println("it2 = " + it2);
    }
}

4.

package com.java.object01;

public class Student {
    String name;
    int id;

    public Student() {}

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    //equals 默认比较对象的地址,如果要比较对象的内容,则需要重写这个方法
    @Override
    public boolean equals(Object o) {
        //表示两个引用指向同一个地址
        if (this == o) {
            return true;
        }
        if (null == o) {
            return false;
        }
        if (o instanceof Student) {
            Student s = (Student) o;
            if (this.getName().equals(s.getName()) && this.getId() == s.getId()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}
package com.java.object01;

public class StudentTest {
    public static void main(String[] args) {
        Student s1 = new Student("乐乐",1001);
        Student s2 = new Student("乐乐",1001);
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1);
    }
}

5.

package com.java.string01;

public class StringDemo01 {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "123";
        System.out.println(str1 == str2);

        String str3 = new String("abc");
        String str4 = new String("abc");
        System.out.println((str1 == str3));
        System.out.println(str3 == str4);
        System.out.println("-----------------");
        String str5 = "abc";
        System.out.println(str1 == str5);
    }
}

6.

package com.java.string01;

public class StringDemo02 {
    public static void main(String[] args) {
        String str = new String("   Let me give you some color to see see !   ");
        System.out.println(str.charAt(8));
        System.out.println(str.length());
        System.out.println(str.contains("me"));
        System.out.println(str.toUpperCase());
        System.out.println(str.trim());
        System.out.println(str.startsWith("Let"));
        System.out.println(str.endsWith(" "));

        System.out.println("==============");
        String s = new String();
        System.out.println("s = " + s);

        byte[] brr = {97,98,99,100,101};
        String str2 = new String(brr,0,5);
        System.out.println(str2);
    }
}

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值