Java基础知识总结

标识符命名规则

 package com.hqyj.javacode.variable;

import java.security.spec.RSAOtherPrimeInfo;

public class TestVariable {
    public static void main(String[] args) {
        int i;//定义了一个变量i
        int b;//定义了一个变量b
        int x, y, z;
        //未经过声明的变量不能使用
        int a;
        //a=0;cannot resolve symbol 'a1'
        //2.不能的命名
        /*

        符合标识符的语法规则
        -必须有字母数字下划线$组成
        -不能以数字开头
        -不能使用关键字
        —不乐观使用保留字
        -不能使用中文
        类名遵循的大驼峰命名---帕斯卡命名法 所有单词首字母大写
        !!!在Java中只要见到大驼峰命名法命名的单词,一定是一个类
        //变量的命名遵循  小驼峰命名法-第一个单词字母 小写其他首字母大写
        //见名知意准则
        */
        //int jkl*;
        //int 3j2kl*;
        //int class;
        //int goto;
        //int 中国;

        int goSchool=2,ih=33;
        int userNamePhone;


        //3.变量的初始化
        //第一次使用变量要先对其进行初始化
        //扩展,对同一条语句声明多个变量时,要对每一个变量赋值
        //int sum=2;
        int sum;
        System.out.println(goSchool);
        System.out.println();

    }
}

方法的定义

package com.hqyj.javacode.methods;

import static java.lang.Integer.sum;

/*
方法(函数)
 Arrays.sort()
 */
public class TestMethod {
    /*
    主方法-main方法-主函数-main函数->程序的入口
     */
    public static void main(String[] args) {
        /*
        1.方法的定义:
        -修饰词:public static
            >.修饰词可以用多个,也可以不用
        - 返回值类型:void
            >.有返回值时,这里写具体的返回值类型、
            >.无返回值时,则用void填充
        -方法名:main
            >.命名方式为小驼峰,见名知意
        - 参数列表:(String[] args)
            >.可以有0个也可以有多个
        - 方法体:{}
            >.有返回值时,需要return 返回值;


            !!!方法里面不能嵌套法方法
            !!!方法的唯一标识为:方法名和参数;列表-->方法的签名
         */
        int sum=sum(1,2);
        System.out.println(sum);

        
    }
    public static int sum(int x,int y){
        return x+y;
    }
}

数据类型

package com.hqyj.javacode.datatype;

public class datatype {
    public static void main(String[] args) {
        //1.自动类型转换
        double d=1;//double=int 小->大
        System.out.println(d);//1.0
        //2.强制类型转换
        int i=(int)1.25;
        System.out.println(i);
        //3.强制类型转换时的精度丢失和溢出
        float a =(float)0.123456789;
        System.out.println(a);//0.123456789 造成了精度丢失
        int x=128;
        byte y=(byte)x;
        System.out.println(y);//-128 造成了溢出
        //!因此写程序时,选择合适的数据类型时解决问题的第一步!
        //4.在多种类型参与运算时,结果会向比较大的类型进行转换
        int  res =(int)(0.25f+1l+0.75+2);
        //float +long+double+int
        System.out.println(res);
        //5.对于byte,short,char,int 参与运算,编译期会将byte,short,char转换为int,再运算
        short m=1;
        byte n=2;
        char c='中';
        int res2=m+n+c;
        System.out.println(res2);
        /*
        总结:
        小转大叫自动转换、大转小叫强制转换(需要转换的类型)
        强制转换时会存在精度丢失和溢出
        在运算时,结果会向较大类型自动转换
        byte ,short int,char参与的运算,会向全部转换为int 再运算!
        * */



    }
}

int数据类型

/*
* int
* */
package com.hqyj.javacode.datatype;

public class TestInt {
    public static void main(String[] args) {
        //1.整数的直接量为int
        int a=100;//100就是直接写出的整数,因此100的默认类型为int
        //byte b=128;//128是直接写出来的整数,默认为int,int赋值给byte就会报出错误。
        byte c=1;//大小问题 byte; -128-127
        System.out.println("psvm");
        //2.整数除法运算的整取
        int count=5/3;//
        System.out.println(count);
        int score=100;
        int error=30;
        int percent =error/score*100;//0
        System.out.println(percent);
        //3.防止溢出的发生
        byte b=127;
        b=(byte)(b+1);
        System.out.println(b);//-128
        int res=2147483647;
        res=res+1;
        System.out.println(res);//-2147483648

        /*
        int的范围为-2147483648~2147483647
        直接写出来的没有超过int范围的默认类型为int
        整数除法运算会取整
        当运算时超过int存储范围则结果不符合逻辑
        * */

    }
}

long数据类型

/*
 * long:超过int范围的整数用long
 * */
package com.hqyj.javacode.datatype;

public class TestLong {
    public static void main(String[] args) {
        //1.long类型的直接量 需要在数值的末尾加l
        long i = 2147483648l; //-2147483648~2147483647
        //疑问:a,b有什么区别?
       /*
       a做了转换:因为int类型的直接量就是直接写出的数字!因此1为int转换为long
       b没有做转换,因此1就是long类型的直接量,所以类型时匹配的
        */


        long a = 1;//int->long;
        long b = 1l;
        //疑问2;为什么long x=3*1000000000;结果会怎出错吗直接量的问题
        long x = 3 * 1000000000;//30y -1294967296 int*int=int
        System.out.println(x);

        long y = 3 * 1000000000l; //int *long=long
        System.out.println(y);


        //2。通过long存储时间毫秒数
        long l = System.currentTimeMillis();//1970年1月1日0.0.0到此时此刻所经历的毫秒
        System.out.println(l);//1647689096888


        //作业:统计程序的运行时间
    }
}

double数据类型

/*
 * long:超过int范围的整数用long
 * */
package com.hqyj.javacode.datatype;

public class TestLong {
    public static void main(String[] args) {
        //1.long类型的直接量 需要在数值的末尾加l
        long i = 2147483648l; //-2147483648~2147483647
        //疑问:a,b有什么区别?
       /*
       a做了转换:因为int类型的直接量就是直接写出的数字!因此1为int转换为long
       b没有做转换,因此1就是long类型的直接量,所以类型时匹配的
        */


        long a = 1;//int->long;
        long b = 1l;
        //疑问2;为什么long x=3*1000000000;结果会怎出错吗直接量的问题
        long x = 3 * 1000000000;//30y -1294967296 int*int=int
        System.out.println(x);

        long y = 3 * 1000000000l; //int *long=long
        System.out.println(y);


        //2。通过long存储时间毫秒数
        long l = System.currentTimeMillis();//1970年1月1日0.0.0到此时此刻所经历的毫秒
        System.out.println(l);//1647689096888


        //作业:统计程序的运行时间
    }
}

char数据类型

/*
* char:
* 本质是16位无符号整数!
* */
package com.hqyj.javacode.datatype;

public class TestChar {
    public static void main(String[] args) {
        //1.char类型赋值
        char a=66;//B
        char b=97;//a
        System.out.println(a);
        System.out.println(b);
        //b.赋值字符--单引号括起来的是字符字符型的直接量
        char c='a';//a
        char d='\u4e2d';//中  'u4e2d'指的是Unicode编码
        //扩展:e1和e2有什么区别
        char e1=1;//[]
        char e2='1';//1
        System.out.println(c);
        System.out.println(d);
        System.out.println(e1);
        System.out.println(e2);

        // c.赋值中文 每个中文占两个字符
        char f='中';
        //char f1='123';//123是3个字符
        int h='中';
        System.out.println(h);//20013
                h=h+1;
        System.out.println(h);//20014

        //经典面试题;为什么会出现错误,怎么改
        char m='a';
       char res1='a' +1;//char res1=(char)(m+1)
       char res2='a'+1;//char +int =int



    }
}

boolean数据类型

/*
* boolean
* !初始化的值只能为true或flase
* */
package com.hqyj.javacode.datatype;

public class TestBoolean {
    public static void main(String[] args) {
        //1.boolean赋值
        boolean flag1=true;
        boolean flag2=false;
        //3.boolean可以进行逻辑运算
        //例子:判断是否成年
        int age =25;
        boolean res= age >=18;
        System.out.println(res);
    }
}

练习

/*

案列-统计程序的运行时间
*/
package com.hqyj.javacode.datatype;


import sun.awt.windows.ThemeReader;

public class HomeWork1 {
    public static void main(String[] args) throws InterruptedException {

        //1.获取运行前的时间
        long startTime = System.currentTimeMillis();
        //2.需要计算运行时间的程序
        Thread.sleep(3000);//程序睡眠三秒
        //3.获取运行后时间
        long endTime = System.currentTimeMillis();

        //4.打印测序的运行时间
        System.out.println(endTime-startTime);
    }
}

流程控制

分支语句

if语句

package com.hqyj.javacode.ifelse;
/*
if结构

* */

public class TestIF {
    public static void main(String[] args) {
        //1.if结构
        int score =80;
//        if (score>60){
//            System.out.println("A");
//        }
//        System.out.println(B);
        //2.if的变形:对于if括号中只有一条语句,我们可以省略花括号!,但不建议
        if(score>70)
            System.out.println("A");
        System.out.println("B");
    }
}

if…else

package com.hqyj.javacode.ifelse;
/*
1.if结构
* */
public class TestIfElse {
    public static void main(String[] args) {
        int score=80;
        if(score>60){
            System.out.println("A");

            }
        else{
                System.out.println("B");

        }

    }
}

package com.hqyj.javacode.ifelse;

/*
else if语句

  • /
    public class TestElseIF {
    public static void main(String[] args) {
    //判断当前成绩的评级
    /

    60以下为D
    60-70为C
    70-80为B
    80-90为A
    90以上为A+
    * */
    int score = 75;
    if (score >= 90) {
    System.out.println(“A+”);

      } else if (score < 90 && score >= 80) {
          System.out.println("A");
      } else if (score >= 70 && score < 80) {
          System.out.println("B");
      } else if (score >= 60 && score < 70) {
          System.out.println("C");
      } else {
          System.out.println("D");
      }
    

    }
    }

else if

package com.hqyj.javacode.ifelse;

/*
else if语句
* */
public class TestElseIF {
    public static void main(String[] args) {
        //判断当前成绩的评级
        /*
        60以下为D
        60-70为C
        70-80为B
        80-90为A
        90以上为A+
        * */
        int score = 75;
        if (score >= 90) {
            System.out.println("A+");

        } else if (score < 90 && score >= 80) {
            System.out.println("A");
        } else if (score >= 70 && score < 80) {
            System.out.println("B");
        } else if (score >= 60 && score < 70) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}



switch语句

package com.hqyj.javacode.ifelse;

/*
switc分支结构
1.整型表达式
2。整型常量值
3.case中有无break的情况
4.case都不匹配是最终选择为default
switch重点:
switch(整型表达式):整型表达式的类型只能为byte short int char string
case 整数常量值:要么是常量,要么是表达式值恒为常量:1/1...3-2等
判断case中是否有break
* */
public class TestSwitch {
    public static void main(String[] args) {
        //1.基本认识:分数的评级
        int i1 = 2;
        byte i2 = 2;
        short i3 = 2;
        long i4 = 2;
        double i5 = 2;
        char i6 = '2';
        String i7 = "2";
        switch (i1) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            case 4:
                System.out.println("4");
                break;
            case 5:
                System.out.println("5");
                break;
            case 6:
                System.out.println("6");
                break;
            case 7:
                System.out.println("7");
                break;
            default:
                System.out.println("0");
                break;
        }

    }

}



一级标题

一级标题

一级标题

一级标题

一级标题

一级标题

一级标题

一级标题

一级标题

一级标题

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值