JAVA基础

java基础

注释

  1. 单行注释(只能注释一行文字)     // 注释内容
    
  2. 多行注释(可以注释一段文字)     /* 注释内容 */    
    
  3. JavaDoc: 文档注释    /**  注释内容   */   
    

关键字

12345
abstractassertbooleanbreakbyte
casecatchcharclassconst
continuedefaultdodoubleelse
enumextendsfiinalfinallyfloat
forgotoifimpiementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnstrictfpshortstaticsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile

**Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。

标识符

  1. 所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线( _ ) 开始。_
  2. 首字母之后可以是字母(A-Z或者a-z),美元符($),下划线(_)或数字的任何字符组成。
  3. 不能使用关键字作为变量名或方法
  4. !!!!标识符是大小写敏感的。
  • 合法标识符举例:age 、 $salary 、 _value 、 _1_value
  • 非法标识符举例:123abc、-salary、#abc
  • **可以使用中文命名,但是一般不建议这样去使用,也不建议用拼音,很low.
例子:
public class Demo01 {
    public static void main(String[] args ){
        //所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线(_)开始。
        String Ahello="paofu";
        String hello="paofu";
        String $hello="paofu";
        String _hello="paofu";
        //String 1hello="paofu";(错误)
        //String #hello="paofu";(错误)
        //String *hello="paofu";(错误)

        // 首字母之后可以是字母(A-Z或者a-z),美元符($),下划线(_)或数字的任何字符组成。
        String $hello123="paofu";

        //不能使用关键字作为变量名或方法
        //String class="paofu";(错误)

        //标识符是大小写敏感的。
        String MAN="paofu";
        String man="paofu";


        System.out.println("");
    }
}

数据类型

  • 强类型预压

    要求变量的使用要严格符合规则,所有变量都必须先定义后才能使用

  • 弱类型语言
  • JAVA的数据类型分为两大类
    1. 基本类型(primitive type)
    2. 引用类型(reference type)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EAkXj3NY-1668840155421)(file:///E:\软件\QQ\QQ聊天记录\3411515893\Image\C2C\AD01957BC0944A43FAF47D4C8963FA75.png)]

例子
public class Demo02 {
    public static void main(String[] args){
        //八大基本数据类型

        //整数
        int num1=10;      //(常用)
        byte num2=20;    //(范围:-128——127)
        short num3=30;   //(范围:-32768——327677)
        long num4=30L;   //(定义long类型,要在数字后面加L)

        //小数:浮点数
        float num5=50.1F;    //(定义float类型,要在数字后面加F)
        double num6=3.14;

        //字符 (是代表一个字)
        char name='国';
        //char name='国家';(报错)

        /*字符串 (在字符串中,String不是关键词,是类)
        String name1="小七"; */

        //布尔值:是非
        boolean flag=true;
        //boolean flag=false;



    }
}
数据类型的扩展
public class Demo03 {
    public static void main(String[] args) {
        //整数扩展: 进制     二进制 0b    十进制     八进制 0     十六进制0x

        int i = 10;
        int i2 = 0b10;   //二进制 0b
        int i3 = 010;    //八进制 0
        int i4 = 0x10;   //十六进制 0x

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);


        System.out.println("==============================================================");
        //====================================================================
        // 浮点数扩展:    银行业务怎么表示?钱
        //BigDecimal   数学工具类
        //====================================================================

        //float   有限  离散  含误差   大约   接近但不等于
        //double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较


        float f = 0.1F;  //0.1
        double d = 1.0 / 10;  //0.1

        System.out.println(f == d);  //false

        float d1 = 23434325423542f;
        float d2 = d1 + 1;

        System.out.println(d1 == d2);  //true

        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 表:(a=97,A=65),2字节 0——65536

        //转义字符
        // \t  制表符
        // \n  换行
        // 。。。。。

        System.out.println("Hello\nworld");

        System.out.println("=====================================================");
        
        //布尔值扩展:
        boolean flag = true;
        if(flag==true){}  //新手
        if(flag){}  //老手
        //less is more! 代码要精简易读

    }
}

类型转换

  • 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换。-
---------------------------------------------------------------byte,short,char-> int ->long ->float ->double
  • 运算中,不同类型的数据先转换为同一类型,然后进行运算。
  • 强制类型转换
  • 自动类型转换
例子
public class Demo04 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte)i;
        double c = i;

        //强制转换  (类型)变量名   高--低
        //自动转换   低--高

        System.out.println(i);
        System.out.println(b);
        System.out.println(c);


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

        System.out.println("============================");
        char e = 'a';
        int d = e+1;
        System.out.println(d);
        System.out.println((char)d);

    }

}
类型转换常见问题
例子
public class Demo05 {
    public static void main(String[] args) {
        // 操作比较大的数的时候,注意溢出问题
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//-1474836480, 计算的时候溢出
        System.out.println(total);

        long total2 = money*years; //默认是int, 转换之前已经存在问题了
        System.out.println(total2);

        long total3 = money*((long)years);
        System.out.println(total3);
    }
}

变量

  • 变量是什么:就是可以变化的量
  • Java是一种强类型语言,每个变量都必须声明其类型
  • Java变量是程序中最基本的存储单位,其要素包含变量名,变量类型和作用域
type varName [=value] [{,varName[=value]}];
//数据类型  变量名 = 值; 可以使用逗号隔开来声明多个同类型变量。
  • 注意事项:
    1. 每个变量都有类型,类型可以是基本类型,也可以是引用类型
    2. 变量名必须是合法的标识符
    3. 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
public class Demo06 {
    public static void main(String[] args) {
        //int a,b c;(不建议这样定义)  //程序可读性
        int a = 1;
        int b = 2;
        int c = 3;
        String name ="xiaoqi";
        char x = 'x';
        double f = 3.123;
    }
}

常量

  1. 常量:初始化后不能再改变值!不会变动的值。
  2. 所谓常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
  3. 常量名一般使用大写字符。
final  常量名 = 值;
final double PI = 3.14;
public class Demo08 {

    //修饰符,不存在先后顺序
    static final double PI = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}

作用域

  1. 类变量
  2. 实例变量
  3. 局部变量
public class Demo07 {

    //类变量  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 Demo07();
        Demo07 demo07 = new Demo07();
        System.out.println(demo07.age);
        System.out.println(demo07.name);

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


    }

    //其他方法
    public void add(){

    }



}

变量的命名规范

  • 所有变量、方法、类名:见名知意
  • 类成功变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线:MAX_VALUE
  • 类名:首字母大写和驼峰原则:Man,GoodMan
  • 方法名:首字母小写和驼峰原则:run(),runRun()

运算符

java语言支持如下运算符:
  • 算术运算符:+ , — , * , / , % , ++ , ——
  • 赋值运算符: =
  • 关系运算符:> , < , >= , <= , == , != , instanceof
  • 逻辑运算符:&& , || , !
  • 位运算符:& , | , ^ , ~ , >> , << , >>>
  • 条件运算符:? , :
  • 扩展赋值运算符: += , -= , *= , /=
例子
  1. 加减乘除的运算
public class Demo01 {
    public static void main(String[] args){
        //二元运算
        //ctrl + D : 复制当前行到下一行

        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(float)b);
    }

}
  1. 类型运算
public class Demo02 {
    public static void main(String[] args){
        // 有 long 类型,执行 long 类型。
        // 有 double 类型,执行 double 类型。
        //默认 int 类型
        long a = 12123214232138L;
        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
    }
}
  1. 关系运算符的运算
public class Demo03 {

    public static void main(String[] args){
        //关系运算符返回的结果:正确,错误。(布尔值)

        int a = 10;
        int b = 20;
        int c = 22;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);    //   c/a = 22/20 = 1....2


    }
}

  1. 自增、自减的运算 Math工具类
public class Demo04 {
    public static void main(String[] args) {
        // ++  --  自增  自减  一元运算符
        int a = 3;
        int b = a++; //执行完这行代码后,先给b赋值,再自增;
        //a = a+1;
        System.out.println(a);
        //a = a+1;
        int c = ++a; //执行完这行代码前,先自增,再给c赋值;
        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);
    }
}
  1. 逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        // 与(&&)  或(||) 非( !(&&) )
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+ (a&&b)); //逻辑与运算:两个变量都为真,结果才为true.

        System.out.println("a || b:"+ (a||b)); //逻辑或运算:两个变量有一个为真,则结果才为true.

        System.out.println("!(a && b):"+ !(a&&b));//逻辑非运算:如果是真,则变为假。如果是假,则变为真。
    }
}

  1. 位运算符
public class Demo06 {
    public static void main(String[] args) {
       /*
       A = 0011 1100
       B = 0000 1101
       ----------------
       A&B 0000 1100  (A与B比较 相同的话,数字照抄,不同的话,就写0)
       A|B 0011 1101  (A与B比较 相同的话,数字照抄,不同的话,有1就写1)
       A^B 0011 0001  (A与B比较 两个数相同为0,不同为1)
       ~B  1111 0010  (与B相反)

       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);
    }
}

  1. 三元运算及小结
public class Demo07 {
    public static void main(String[] args){
        int a = 10;
        int b = 20;
        int c = 30;

        a+=b; //a = a+b
        c-=b; //c = c-b

        System.out.println(a);
        System.out.println(c);

        //字符串连接符  +   , String
        System.out.println(""+a+b);//字符串后面的数是拼接
        System.out.println(a*b+"");//字符串前面的数照样计算
    }
}

  1. 三元运算
public class Demo08 {
    public static void main(String[] args) {
        //  x  ?  y   :   z
        // 如果 x==ture,则结果为y,否则结果为z;
        int a = 60;
        String c = a<60 ? "不及格":"及格"; //必须掌握
        System.out.println(c);

    }
}

包机制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值