xxdd123321

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

类型

  • 强类型语言

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

    2. 安全性高

    3. 速度慢

  • 弱类型语言

    1. 可以不符合规定VB,JS

  • Java的数据类型分为两大类

    1. 基本类型(primitive type)

      • 数值类型

        • 整数类型

          1. byte占一个字节范围:-18-127

          2. short占2个字节范围:-32768-32767

          3. int占4个字节范围:-2147483648-2147483647

          4. long占8个字节范围:-9223372036854775808-9223372036854775807

        • 浮点类型

          1. float占4个字节

          2. double占8个字节

        • 字符类型

          1. char占2个字节

      • Boolean类型

    2. 引用类型(reference type)

      • 接口

      • 数组

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

       // 整数
       int num1 = 10;  // 最常用
       byte num2 = 20;
       short num3 = 30;
       long num4 = 30L;    // Long类型要在数字后面加个L

       // 小数:浮点数
       float num5 = 50.1F; // float类型要在数字后面加个F
       double num6 = 3.14159265357964533;

       // 字符
       char name = '中';
       // 字符串,String不是关键字,类
       // String namea = "解迪";

       //布尔值
       boolean flag = true;
       // boolean flag1 = false;
  }
}

变量

  • 变量是什么:可以变化的量

  • Java是一种强类型语言,每个变量都必须声明其类型

  • Java变量事程序中最基本的储存单元,其要素包含变量名,变量类型和作用域

  • 数据类型 变量名 = 值;

  • 注意事项

    1. 每个变量都有类型,类型可以是基本类型,也可以是引用类型

    2. 变量名必须是合法的标识符

    3. 变量声明是一条完整的语句,因此每一个声明都必须以分号结束

public class Demo02 {
   public static void main(String[] args) {
       // int a=1, b=2, c=3;   不提倡多行,程序可读性
       String name = "xd";
       char x = 'x';
       double pi = 3.14;
  }
}
  • 变量作用域

    1. 类变量:写在类里面

      • 加static

        static int a = 0;
    2. 实例变量:写在类中间

    3. 局部变量:写在方法中

public class Demo02 {
   //属性:变量
   //实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
   //布尔值:默认是false
   //除了基本类型,其余默认值都是null
   String name;
   int age;
   //类变量 static
   static double salary = 2500;
   //main方法
   public static void main(String[] args) {
       //局部变量;必须声明和初始化值,只在该方法有效
       int i = 10;
       System.out.println(i);
       //变量类型 变量名字 = new Demo02()
       Demo02 demo02 = new Demo02();
       System.out.println(demo02.age);
       System.out.println(demo02.name);
       System.out.println(salary);
  }
   //其它方法
   public void  add(){
  }
}
  • 变量的命名规范

    • 所有变量,方法,类名:见名知意

    • 类成员变量:首字母小写和驼峰原则:monthSalary

    • 局部变量:首字母小写和驼峰原则

    • 常量:大写字母和下划线:MAX_VALUE

    • 类名:首字母大写和驼峰原则:Man,GoodMan

    • 方法名:首字母小写和驼峰原则:run(),runRun()

常量

  • 常量(constant):初始化后不能再改变值,不会动的值

  • 所谓常量可以理解为一种特殊变量,它的值被设定后,再程序运行过程中不允许被改变

  • final 常量名=常量值;

  • 常量名一般使用大写字符

public class Demo02 {

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

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

字节

  • 位(bit):是计算机 内部数据 存储的最小单位,11001100是一个八位二进制数

  • 字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示

  • 1B = 8bit

  • 字符:是指计算机中使用的字母,数字,字和符号

  • 1bit = 1位

  • 1byte = 1B = 8b

  • 1024B = 1KB

  • 1024KB = 1M

  • 1024M = 1G

  • 1024G = 1TB

数据类型扩展

  • 整数拓展

public class Demo02 {
   public static void main(String[] args) {
       //整数拓展: 进制 二进制0b   十进制 八进制0   十六进制0x

       int i = 10;
       int i2 = 010;   //八进制
       int i3 = 0x10;  //十六进制0x   0-9 A-F

       System.out.println(i);
       System.out.println(i2);
       System.out.println(i3);
       System.out.println("====================================");

       //=======================================================
       //浮点数拓展 银行业务表示
       //=======================================================
       //float 有限 离散   舍入误差   大约 接近但不等于
       //double
       //最好完全不用浮点数进行比较
       //BigDecimal 数学工具类

       float f = 0.1f;
       double d = 1.0/10;
       System.out.println(f==d);
       System.out.println(f);
       System.out.println(d);

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

       float d1 = 32323232f;
       float d2 = d1 + 1;
       System.out.println(d1 == d2);
       System.out.println(d1);
       System.out.println(d2);

       //=======================================================
       //字符拓展
       //=======================================================
       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 2字节 65536 excel 2 16 65536
       char c3 = '\u0061';
       System.out.println(c3); //a
       System.out.println("====================================");

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

       System.out.println("hello\tworld");
       System.out.println("hello\nworld");
       //=======================================================
       //布尔值拓展
       //=======================================================
       boolean flag = true;
       if (flag==true){}
       if (flag){}

  }
}

类型转换

  • 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换

  • 运算中,不同类型的数据先转化为同一类型,然后进行运算

public class Demo02 {
   public static void main(String[] args) {
       //低 --------------------------------高
       //byte,short,char,int,long,float,double
       int i = 128;
       byte b = (byte) i;
       double d = i;
       //强制转换 (类型)变量名 高->低
       //自动转换 低->高
       System.out.println(i);
       System.out.println(b);//128超出127,内存溢出
       System.out.println(d);

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

  }
}


"D:\JAVA\IDEA\IntelliJ IDEA Community Edition 2019.3.5\jbr\bin\java.exe" "-javaagent:D:\JAVA\IDEA\IntelliJ IDEA Community Edition 2019.3.5\lib\idea_rt.jar=51102:D:\JAVA\IDEA\IntelliJ IDEA Community Edition 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\code\JavaSE\out\production\基础语法 Demo02
128
-128
128.0
=========================
23
-32
=========================
98
b

Process finished with exit code 0

注意问题

public class Demo02 {
   public static void main(String[] args) {
       //操作比较大的数的时候,注意溢出问题
       //JDK7新特性,数字之间可以用下划线分割
       int money = 10_0000_0000;
       System.out.println(money);
       int years = 20;
       int total = money * years;  //-1474836480,计算时候溢出
       long total1 = money * years;    //默认是int,转换之前已经存在问题
       System.out.println(total);
       System.out.println(total1);

       long tota123 = money*((long)years);//先把一个数转换成Long
       System.out.println(tota123);
       //l尽量使用大写L

  }
posted on   xxdd123321  阅读(282)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
 
点击右上角即可分享
微信分享提示