java基础笔记

目录

知识点一:

知识点二:

格式化

        字符串格式化

        数字格式化

        日期格式化


知识点一:

        ==        比较

基本数据类型

        比较的是值。

引用数据类型

        比较的是内存地址(例如对象的地址在栈中的地址)

        &&        和        &        逻辑与

&&支持短路,同真为真,当第一个条件为false的时候,不再执行第二个判断条件。

&不支持短路,同真为真,两个判断条件都执行。

        ||             和        |        逻辑或

|| 支持短路,两个条件只要有真,就为真。当第一个条件为真时,第二个条件不再执行。(真)。

| 不支持短路,两个条件只要有真,就为真。不管第一个条件是否为真,都会执行第二个条件。

知识点二:

格式化

        数字格式化

        //格式化百分比
        double random = 0.2234;
        System.out.println(random);
        System.out.printf("格式化:%.2f%n",random);

        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        String s = numberFormat.format(random);
        System.out.println(s);
        //
        numberFormat.setMinimumFractionDigits(2);  //设置百分比后面的小数位
        String s1 = numberFormat.format(random);
        System.out.println(s1);

        numberFormat.setMinimumIntegerDigits(2);
        System.out.println(numberFormat.format(random));    //

        NumberFormat percentInstance = NumberFormat.getPercentInstance(Locale.CHINA);
        String format = percentInstance.format(random);
        System.out.println(format);

 输出结果:

 

        数字格式化

         //格式化小数位
        double random = Math.random();
        System.out.println(random);
        NumberFormat percentInstance = NumberFormat.getPercentInstance();

        //00,025.74%
        percentInstance.setMinimumFractionDigits(2);
//        percentInstance.setMinimumIntegerDigits(5); 
//设置百分数位数的,1代表百分数的个位,2十位,3百位
        System.out.println(percentInstance.format(random));

        //格式化货币
        /**
         *  NumberFormat.getCurrencyInstance(Locale.US);            获取美国的货币对象
         *
         *  NumberFormat.getCurrencyInstance(Locale.CHINA);         获取中国的货币对象
         *
         *  NumberFormat.getCurrencyInstance();                     在中国地区默认是中国的货币对象
         */
        NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.CHINA);
        String money = currencyInstance.format(random);
        System.out.println(money);

        NumberFormat instance = NumberFormat.getCurrencyInstance(Locale.US);
        String format1 = instance.format(random);
        System.out.println(format1);

        //格式化千分位
        double s = 4567913516513123L;
        NumberFormat numberInstance = NumberFormat.getNumberInstance(Locale.CHINA);
        String format = numberInstance.format(s);
        System.out.println(format);

 

        日期格式化

        /**
         *      LocalDate 类
         */
        //获取当前日期实例
        LocalDate localDate = LocalDate.now();
        //向当前日期实例中设置日期
        LocalDate newLocalDate = LocalDate.of(2000, 3, 2);

        //当前时间的5天后
        System.out.println(localDate.plusDays(5L));
        //当前时间的3天前
        System.out.println(localDate.minusDays(3L));

        //当前时间的25个月后的日期
        System.out.println(localDate.plus(25, ChronoUnit.MONTHS));
        //当前时间的15个月之前的日期
        System.out.println(localDate.minus(15, ChronoUnit.MONTHS));

        //获取年份
        System.out.println(newLocalDate.getYear()+"new");
        System.out.println(localDate.getYear());

        //获取月份
        String displayName = localDate.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA);
        String displayName1 = localDate.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINESE);
        System.out.println(displayName);
        System.out.println(displayName1);
        System.out.println(newLocalDate.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINESE)+"new");

        //获取当前月份的第几天
        System.out.println(localDate.getDayOfMonth());
        System.out.println(newLocalDate.getDayOfMonth()+"new");

        //获取当前年份的第几天
        System.out.println(localDate.getDayOfYear());
        System.out.println(newLocalDate.getDayOfYear()+"new");
        //获取周几
        System.out.println(localDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE));
        System.out.println(newLocalDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE)+"new");

        //直接输出日期
        System.out.println(localDate);
        System.out.println(newLocalDate);
        //格式化输出日期
        System.out.printf("%tF%n",localDate);
        System.out.printf("%tF%n",newLocalDate);
        System.out.printf("%tY年%<tm月%<td日 %<tA%n",localDate);
        System.out.printf("%tY年%<tm月%<td日 %<tA%n",newLocalDate);

        //判断当前年份是否为闰年
        System.out.println(localDate.getYear()+"是不是闰年"+localDate.isLeapYear());
        System.out.println(newLocalDate.getYear()+"是不是闰年"+newLocalDate.isLeapYear());

输出结果: 

Java类中的代码块

public class User {
    //属性
    private Integer age;
    private String userName;

    //方法
    public void save(){

    }

    //代码块
    {
        System.out.println("代码块1");
    }
    {
        System.out.println("代码块2");
    }

    //静态代码块
    static {
        System.out.println("静态代码块");
    }

    public static void main(String[] args) {
        User user = new User();
        user.userName = "Tom1";
        user.age = 18;
        System.out.println(user.toString());
        System.out.println("-------------------");
        User user1 = new User();
        user1.userName = "Tom2";
        user1.age = 17;
        System.out.println(user1.toString());
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", userName='" + userName + '\'' +
                '}';
    }
}


//输出结果:
//静态代码块
//代码块1
//代码块2
//User{age=18, userName='Tom1'}
//-------------------
//代码块1
//代码块2
//User{age=17, userName='Tom2'}

有输出结果来看先输出的是:静态代码块 -->代码块1 -->代码块2 -->User对象

在我们不进行new 类的对象时,被static修饰的代码块,格式static{ //代码块 },会在类加载的时候执行,并且执行一次。而没有被static修饰的代码块,会在该类执行构造方法的时候(创建对象的时候)执行,并且每执行一次,代码块就会运行一次。

static关键字

        使用static修饰的类、属性、方法、常量、变量等。不需要创建该类的对象,可以直接通过类名直接调用。

public class Demo {

    //正常的
    private String name = "Tom";
    private int remove(int a,int b){
        return a+b;
    }

    //静态的
    private static int i = 18;
    private static int save(int a,int b){
        return a+b;
    }

    public static void main(String[] args) {
        //静态访问
        System.out.println(Demo.i);
        System.out.println(Demo.save(1, 3));
        //对象访问
        Demo demo = new Demo();
        System.out.println(demo.remove(1, 8));
        System.out.println(demo.name);

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@沉住气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值