java数据类型
数值类型
public static void main(String[] args){ int i1 = 10;//十进制 int i2 = 010;//八进制 int i3 = 0x10;//十六进制 System.out.println(i1); System.out.println(i2); System.out.println(i3); //浮点数扩展:使用BigDecimal //float 有限 离散 舍入误差 大约 float f = 0.1f;//0.1 double d = 1.0/10;//0.1 float d1 = 2323232323232323f; float d2 = d1 + 1; System.out.println(f==d); System.out.println(d1==d2); //字符扩展 char c1 = 'a'; char c2 = '中'; System.out.println(c1); System.out.println((int)c1); System.out.println(c2); System.out.println((int)c2); //unicode char c3 = '\u0061'; System.out.println(c3); String sa = new String("haha"); String sb = new String("haha"); System.out.println(sa==sb);//false String sc = "haha"; String sd = "haha"; System.out.println(sc==sd);//true }
类型转换
public static void main(String[] args){ //byte short char int long float double由低到高 //不能对布尔值转换 int i = 128; byte b = (byte)i;//强制类型转换 System.out.println(i);//128 System.out.println(b);//内存溢出-128 System.out.println((int)2.2);//2 System.out.println((int)-2.2f);//-2 //操作大数注意溢出 int money = 10_0000_0000; int years = 20; int total = money * years;//-1474836480 溢出 long total2 = money * years;//默认是int, 转换之前已经出错了 long total3 = money*((long)years);//先把一个转为long }
变量
变量类型
public class Animal { //类变量 static 从属类 static double weight = 2222; static final double K = 22;//修饰符不区分先后顺序 //实例变量:从属于对象;不初始化时为默认值 //除了基本类型默认初始值都是null String name; int age; public static void main(String[] args){ //局部变量:必须声明和初始化值 int i = 1; System.out.println(i); Animal animal = new Animal(); System.out.println(animal.age); //类变量 System.out.println(weight); } }
变量命名规范
类成员变量、局部变量、方法名:首字母小写加驼峰 monthSalary
类名:首字母大写加驼峰 GoodMan
常量:大写字母和下划线MAX_VALUE
基本运算符
public static void main(String[] args){ long a = 111111111111L; int b = 111; short c = 10; byte d = 8; //只要有long,结果就是long类型 System.out.println(a+b+c+d);//long System.out.println(b+c+d);//int System.out.println(c+d);//int //短路运算 int aa = 8; boolean bb = (aa < 7) && (aa++>8); System.out.println(aa); }
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/14514221.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步