Java基础学习笔记

----Java基础----

1. 注释

public class Cl1 {
    //单行注释note
    /*
    * 多行注释
    *换行 /n /r
    */
    /**
     * 文档注释
     * @description BaseClass.Cl1
     */
    public static void main(String[] args) {
        System.out.println("狂神第一节课学注释");
        System.out.println("单行注释\n" +
                "多行注释\n" +
                "文档注释(和javadoc有关)");
    }
}

2. 标识符和关键字

public class Cl2 {
    public static void main(String[] args) {
        System.out.println("狂神讲标识符");
        String woshishei = "wcl";
        System.out.println(woshishei);
        /* string
        注意命名方式
         */
        String 名字 = "wcl";
        System.out.println(名字);
    }
}

3. 数据类型

public class Cl3 {
    /* 数据类型
    java是强类型语言,弱类型例子js、vb
     */
    public static void main(String[] args) {
        Integer a = 10;
        a = a + 2;
        System.out.println(a);
        float b = 1f;
        System.out.println(b);
    }
    /*
    数据类型分为两大类:基本类型和引用类型
    1、八大基本数据类型
    int num1 = 10
    byte num2 = 20
    short num3 = 30
    long num4 = 40
    小数:
    float num5 = 50.1F;
    double num6 = 3.14159265358
    字符类型:
    char name = 'A'(一个);
    String不是关键字,是一个类
    String namea = "wcl";
    布尔值:
    boolean flag = true(或者false);
     */
}
3.1. 扩展
public class Cl4 {
    public static void main(String[] args) {
        //整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
        int i = 10;
        int i2 = 010;//八进制0
        int i3 = 0x1B;//十六进制0x   0-9,A-F
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);

        System.out.println("====================================");
        //浮点数拓展
        //float
        float f = 0.1f;
        //double
        double d = 1.0/10;
        System.out.println(f==d);
        System.out.println(f);
        System.out.println(d);

        System.out.println("====================================");
        //字符类拓展
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);
        //所有的字符本质还是数字
        //编码 Unicode
        System.out.println((char) 97);

        char c3 = '\u0061';
        System.out.println(c3);

        //转义字符
        // \t 制表符
        // \n 换行符
        System.out.println("Hello\tWorld");

        //=========
        System.out.println("========================================");
        String sa = new String("BaseClass.hello world");
        String sb = new String("BaseClass.hello world");
        System.out.println(sa==sb);

        String sc = "BaseClass.hello world";
        String sd = "BaseClass.hello world";
        System.out.println(sc==sd);
        //对象 从内存分析

        //布尔值扩展
        boolean flag = true;

        if (flag==true){}
        if (flag){}//这两行都一样
    }
}

4. 类型转换

public class Cl5 {
    /*
    类型转换
    类型的优先级如下:
    低 ----------------------------------------------- 高
    byte,short,char --> int --> long --> float --> double
     */
    public static void main(String[] args) {
        int i =128;
        byte b = (byte) i;//内存溢出(byte<128),()表示强制转换

        System.out.println(i);
        System.out.println(b);
        //=======================================
        System.out.println("====================");
        //=======================================
        int i2 =128;
        double b2 = i2;
        //强制转换 (类型)变量名  高--低
        //自动转换     低--高

        System.out.println(i2);
        System.out.println(b2);

        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题
         */
        System.out.println("================");
        System.out.println((int)23.7); //23
        System.out.println((int)-45.89f);//-45
        char c = 'a';
        int d = c+1 ;
        System.out.println(d);
        System.out.println((char) d);

    }
}
4.1. 注意溢出
public class Cl6 {
    public static void main(String[] args) {
        //操作比较大的时候注意溢出问题
        //数字之间可以用下划线分割 jdk7新特性
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//溢出
        System.out.println(total);
        long total2 = money*years;//默认是int,转换之前的计算已经溢出了
        System.out.println(total2);
        long total3 = money*((long)years);//先把一个数转换为long
        System.out.println(total3);
    }
}

5. 变量

public class Cl7 {
    //变量,要素包括:变量名,变量类型和作用域

    //main方法
    public static void main(String[] args) {
        //int a,b,c
        int a=1,b=2,c=3;
        String name = "wcl";
        char x = 'x';
        double pi = 3.14;
    }
    /*

    变量作用域:类变量,实例变量,局部变量(注意位置)

     */
    static int allClicks=0; //类变量
    String str="BaseClass.hello world"; //实例变量

    public void method(){
        int i = 0; //局部变量
    }


}
5.1. 变量的具体使用
public class Cl8 {

    //类变量 static
    static  double salary = 2500;

    //实例变量:从属于对象;
    // 如果不进行自行初始化,这个类型的默认值 0 0.0 u0000 false
    //除了基本类型,其余默认都是null
    String name;
    int age;

    //main方法
    public static void main(String[] args) {

        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //实例变量:变量类型 变量名字 = new BaseClass.Cl8()
        Cl8 cl8 = new Cl8();
        System.out.println(cl8.age);
        System.out.println(cl8.name);

        //类变量 static
        System.out.println(salary);

    }
}

6. 常量

public class Cl9 {
    /*
    常量,一般用大写,关键字final
     */
    //修饰符,不存在先后顺序
     final static double PI =3.14;
    //static final double PI =3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}

/*
变量的命名规范
1.见名知意
2.类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
3.局部变量:首字母小写和驼峰原则
4.常量:大写字母和下划线:MAX_VALUE
5.类名:首字母大写和驼峰原则:Man,GoodMan
6.方法名:首字母小写和驼峰原则:run() runRun()
 */

7. 基本运算符符

public class Cl9 {
   /*
   常量,一般用大写,关键字final
    */
   //修饰符,不存在先后顺序
    final static double PI =3.14;
   //static final double PI =3.14;
   public static void main(String[] args) {
       System.out.println(PI);
   }
}

/*
变量的命名规范
1.见名知意
2.类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
3.局部变量:首字母小写和驼峰原则
4.常量:大写字母和下划线:MAX_VALUE
5.类名:首字母大写和驼峰原则:Man,GoodMan
6.方法名:首字母小写和驼峰原则:run() runRun()
*/
7.1. 基本运算符和类型优先级级
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
   }
}
7.2. 关系运算符
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确/错误 布尔值

        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(c%a);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}
7.3. 一元运算符(自增,自减)
public class Demo04 {
    public static void main(String[] args) {
        // ++自增 --自减 一元运算符
        int a = 3;
        int b = a++;
        System.out.println(a);
        int c = ++a;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
7.4. 逻辑运算符
//逻辑运算符 && ||
    public class Demo05 {
        public static void main(String[] args) {

        boolean a = true;
        boolean b = false;
        System.out.println("a && b "+(a&&b));
        System.out.println("a || b "+(a||b));
        System.out.println("!(a && b) "+(!(a&&b)));

        //短路运算
        int c =5;
        boolean d = (c<4)&&(c++==6);
        System.out.println(d);
        System.out.println(c);

    }
}
7.5. 位运算符
//位运算符
public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001 异或运算
        ~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 0101 5
       0000 0110 6       0000 0111 7
       0000 1000 8
       0001 0000 16

         */
        System.out.println(2<<3);
    }
}
7.6. 三元运算符
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+"");
    }
}
7.7. 条件运算符
public class Demo08 {
    public static void main(String[] args) {
        // 条件运算符
        // x ? y : z
        //如果x==true,则结果为y,否则为z

        int score = 80;

        String type = score <60 ?"不及格":"及格";
        //if
        System.out.println(type);
    }
}

包机制

JavaDoc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值