基础 Day 3

Day3

类型转换

  • 运算中,不同类型的数据必须先转为同类型再进行运算

  • byte,short,char--->int--->long--->float--->double(低------>高)

  • 强制转换:(类型)变量名(高--->低)

  • 自动转换:低--->高

package com.pan.base;
​
public class Demo3 {
    public static void main(String[] args) {
        int i = 130;
//        强制转换      (类型)变量名  高--低
        byte b = (byte) i;//内存溢出
//        自动转换   低--高
        double be = i;
        System.out.println(i);
        System.out.println(b);
        System.out.println(be);
        /*
        注意点
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转化成低容量时**强制转换**
        4.转换的时候可能存在内存溢出或者精度问题
         */
        System.out.println("========================");
        System.out.println((int)23.7);//23
        System.out.println((int)-45.89f);//-45
        System.out.println("========================");
        char c = 'a';
        int d = c+1;
        System.out.println(d);//98
        System.out.println((char) d);//b
        System.out.println("========================");
//        操作比较大的数时,注意溢出问题
//        数字之间可以使用下划线分割不会被输出
        int money = 10_0000_0000;
        int years = 20;
        int total1 = money*years;
        long total2 = money*years;
        long total3 = money*((long)years);
        System.out.println(total1);//-1474836480,计算的时候溢出
        System.out.println(total2);//默认是int,在进行类型转换之前计算结果已经存在问题!!
        System.out.println(total3);//先把一个数转化成Long类型,计算结果按照long
//        代码中表示long类型后缀通常用L,小写的l和1(数字)不易区分
    }
}
​

变量

  • Java是强类型语言,每个变量必须声明其类型!!!!

  • 为保证程序可读性,尽量不在一行声明多个变量

  • 变量的命名规范

    • 所有的变量、方法、类名一定要见名知意

    • 类成员变量和局部变量:首字母小写和驼峰原则:即除第一个单词外,后面单词首字母大写

    • 常量(constant):大写字母和下划线

    • 类名:首字母大写和驼峰原则

    • 方法名:首字母小写和驼峰原则

  • package com.pan.base;
    public class Demo4 {
        //修饰符不存在先后顺序
        static final double PI = 3.14;
        //类变量 static
        static double salary = 2500;
        //属性(变量)
        //实例变量:从属于对象;如果不进行初始化,则会变成此类型的默认值 0 0.0 u0000
        //布尔值:默认false
        //除了基本类型,其余的默认值都是null;
        String name;
        int age;
        //main方法
        public static void main(String[] args) {
            System.out.println("===============局部变量===================");
            //局部变量;必须声明和初始化值!!!!
            int i = 10;
            System.out.println(i);
            System.out.println("===============实例变量===================");
            //使用实例变量:变量类型 变量名字 = new com.pan.base.Demo4;
            Demo4 demo4 = new Demo4();
            System.out.println(demo4.age);//输出int默认值为0
            System.out.println(demo4.name);//输出String默认值为null
            System.out.println("================类变量===================");
            //类变量 static
            System.out.println(salary);//不用static会报错不能输出;
            System.out.println("================常量====================");
            System.out.println(PI);//常量的引用
        }
        //其他方法
        public void add(){
    //        System.out.println(i);离开方法后变量i不可用
        }
    }
    ​

运算符

  • 算术运算符:+ - * / % ++ --

  • 赋值运算符:=

  • 关系运算符:> < >= <= == != instanceof

  • 逻辑运算符: && || ! (与 或 非)

  • 位运算符:& | ^ ~ >> << >>>

  • 条件运算符:?:

  • 扩展运算符:+= -= *= /=

  • a++:先赋值再运算

  • ++a:先运算再赋值

运算符------代码

  • package com.pan.operator;
    ​
    public class Demo01 {
        public static void main(String[] args) {
            //二元运算符
            int a = 10;
            int b = 20;
            int c = 30;
            int d = 40;
            System.out.println(a+b);
            System.out.println(a-b);
            System.out.println(a*b);
            System.out.println(a/(double)b);
        }
    }
    ​
  • package com.pan.operator;
    ​
    public class Demo02 {
        public static void main(String[] args) {
            long a = 123123123123123L;
            int b = 123;
            short c = 10;
            byte d = 8;
            System.out.println(a+b+c+d);//Long
            System.out.println(b+c+d);//Int
            System.out.println(c+d);//Int
        }
    }
    ​
  • package com.pan.operator;
    ​
    public class Demo03 {
        public static void main(String[] args) {
            //关系运算符返回的结果:正确,错误   布尔值
            int a = 10;
            int b = 20;
            int c = 21;
            System.out.println(c%a);//c/a  21/10=2....1
            System.out.println(a>b);
            System.out.println(a<b);
            System.out.println(a==b);
            System.out.println(a!=b);
        }
    }
    ​
  • package com.pan.operator;
    ​
    public class Demo04 {
        public static void main(String[] args) {
            //++    --  自增,自减   一元运算符
            int a = 3;
    ​
    ​
            int b = a++;//执行完这行代码之后先赋值,再自增
            //a = a + 1
            System.out.println(a);
            //a = a + 1
            int c = ++a; //执行完代码前,先自增,再赋值
    ​
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
    ​
            //幂运算 2^3 2*2*2 = 8
            double pow = Math.pow(2,3);
            System.out.println(pow);
        }
    }
    ​
  • package com.pan.operator;
    ​
    //逻辑运算符
    public class Demo05 {
        public static void main(String[] args) {
            //与(and)    或(or)   非(取反)
            boolean a = true;
            boolean b = false;
    ​
            System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
            System.out.println("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,结果才为真
            System.out.println("! (a && b):"+!(a&&b));//弱国为真,则变为假;如果为假,则变为真
    ​
            //短路运算:若前面为假则会直接输出false,不会判断和执行后面的语句
            int c = 5;
            boolean d = (c<4)&&(c++<4);
            System.out.println(d);
            System.out.println(c);
    ​
        }
    }
    ​
  • package com.pan.operator;
    ​
    public class Demo06 {
        public static void main(String[] args) {
            /*
            A = 0011 1100
            B = 0000 1101
            A&B = 0000 1100 上下都为1才为1,否则都为0
            A|B = 0011 1101 上下都为0才为0,否则都为1
            A^B = 0011 0001 上下相同则为0,否则为1
            ~B  = 1111 0010 取反
            2*8 = 16 2*2*2*2
            <<  *2
            >>  /2
            0000 0000   0
            0000 0001   1
            0000 0010   2
            0000 0011   3
            0000 0100   4
            0000 1000   8
            0001 0000   16
             */
            System.out.println(2<<3);
        }
    }
    ​
  • package com.pan.operator;
    ​
    public class Demo07 {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
    ​
            a+=b;//a=a+b
            a-=b;//a=a-b
            System.out.println(a);
    ​
            //字符串连接符    + , String
            System.out.println(a+b);
            System.out.println(""+a+b);//空字符在前面---拼接
            System.out.println(a+b+"");//空字符在后面--运算
        }
    }
    ​
  • package com.pan.operator;
    ​
    //三元运算符
    public class Demo08 {
        public static void main(String[] args) {
            // x ? y : z
            //如果x==true,则结果为y,否则结果为z
    ​
            int score = 80;
            String type = score < 60 ?"不及格":"及格";
            System.out.println(type);
        }
    }
    ​

包机制

一般利用公司域名倒置作为包名

JavaDoc

  • @author 作者名

  • @version 版本号

  • @since 指名需要最早使用的jdk版本

  • @param 参数名

  • @return 返回值情况

  • @throws 寻常抛出情况

生成 Javadoc

Java -encoding UTF-8 -charset UTF-8(编码方式) 文件名

Jdk帮助文档地址j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值