java基础基础超基础

语法

基础

报错

类名和包名必须一致

java运行机制

  • 编译型:全文翻译,速度快

  • 解释型:逐句翻译,会丢失

快捷键

  • psvm:创建main方法
  • sout:输出语句
  • ctrl + D: 复制当前行到下一行

语法

注释

单行注释://

多行注释:/* */

关键字

在这里插入图片描述

标识符

  1. 首字符为字母(大小写), 美元符号($), 下划线(_)组成

  2. 首字符之后为字母, $, _, 数字…字符组成

  3. 不能用关键字作为变量名或者方法名

数据类型

强类型语言:严格规定变量,安全

弱类型语言:vb,js


java类型分为两大类:

基本类型、引用类型

基本类型

在这里插入图片描述

  • long类型定义的数后面要加L
  • float定义的数后面加F
  • String不是关键字,是类

引用类型

  • 接口
  • 数组

整数拓展

进制: 二进制0b 十进制 八进制0 十六进制0x

浮点数拓展

float:有限,离散,舍入误差,大约,接近但不等于

银行业务用BigDecimal数学工具类表示

字符拓展

char c1 = 'a';
System.out.println(c1);//a
System.out.println((int)c1);//97

转义字符

\t:制表符

\n:换行

布尔值扩展

boolean flag = true;
if (flag==true){
    
    }//新手
if (flag){
    
    }//老手

类型转换

表述范围大小:byte-short-char – int -long–float–double

public class demo3 {
    
    
    public static void main(String[] args) {
    
    
        int i = 128;
        byte b = (byte)i;//内存溢出
        //double b = i;//低--高

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

        System.out.println(i);//128
        System.out.println(b);//-128

        /*
        注意:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量时需要强制转换
        4.转换可能出现内存溢出或者精度问题
         */
        System.out.println((int)14.5);//14
        System.out.println((int)23.5f);//23

        char c = 'a';
        int d = c+1;
        System.out.println(d);//98
        System.out.println((char)d);//b
    }
}
public class demo4 {
    
    
    public static void main(String[] args) {
    
    
        //操作比较大的数的时候注意溢出问题
        //JDK7新特性:数字之间可以用下划线分割
        int money = 10_0000_0000;//1000000// 0000
        int years = 20;
        int total = money*years;//-1474836480,计算时溢出
        long total2 = money*years;//-1474836480,long转换的是int之后溢出的结果,所以仍然溢出

        long total3 = money*((long)years);//先把一个数转换为long
        System.out.println(total3);
    }
}

变量

java变量是程序最基本的存储单元,包括变量名,变量类型和作用域

public class demo5 {
    
    

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

    //属性:变量

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

    //mian方法
    public static void main(String[] args) {
    
    
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型 变量名字 = new demo5();
        demo5 demo5 = new demo5();
        System.out.println(demo5.age);
        System.out.println(demo5.name);

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

    }

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

    }
}

命名规则

  1. 见名知意
  2. 类成员变量:首字母小写和驼峰原则(lastname,lastName)
  3. 局部变量:首字母小写和驼峰原则
  4. 常量:大写字母和下划线:MAX_VALUE
  5. 类名:首字母大写和驼峰原则(Man,GoodMan)
  6. 方法名:首字母小写和驼峰原则:run(),runRan()

常量

final 常量名=值;

final double PI = 3.14;

常量名一般使用大写字符

运算符

在这里插入图片描述

package operator;

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        long a = 1845773876562L;
        int b = 344;
        short c = 143;
        byte d = 72;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//short
    }
}
package operator;

public class Demo02 {
    
    
    //关系运算符返回的结果:正确,错误 布尔值
    public static void main(String[] args) {
    
    
        int a = 10;
        int b = 20;
        int c = 14;

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

    }

}

自增自减运算符

package operator;

public class Demo03 {
    
    
    public static void main(String[] args) {
    
    
        //++ --
        int a = 3;
        int b = a++; //a++  a=a+1
        System.out.println(a);//4

        int c = ++a;//++a a = a + 1
        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5

        //幂运算 2^3  2*2*2 = 8
        double pow = Math.pow(3,2);
        System.out.println(pow);

    }
}

逻辑运算符

package operator;

public class Demo04 {
    
    
    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));//逻辑或运算:一个为真就为true
        System.out.println("!(a && b):"+!(a&&b));//非运算:如果为真则变为假,如果为假变为真

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}
/*
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 1000  8
0001 0000  16
 */

字符串连接

package operator;

public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        //字符串连接符  +  ,前面有String类型会变为字符串连接
        int a = 10;
        int b = 20;
        System.out.println(""+a+b);//1020
        System.out.println(a+b+"");//30

    }
}

三元运算符

x ? y : z

package operator;

public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        int score = 50;
        String type = score <60?"不及格":"及格";
        System.out.println(type);
    }
}

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

javaDoc

/**

*/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wnIyT6E4-1642404057753)(D:\Java\cut\doc.png)]

cmd命令:javadoc -encoding UTF-8 -charset UTF-8

生成doc文档

请添加图片描述
请添加图片描述

猜你喜欢

转载自blog.csdn.net/crush_oo/article/details/122540755