栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基础 Day 3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基础 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);
        
        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 
  • 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) {
            
            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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1041088.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号