2026/2/16 11:09:55
网站建设
项目流程
广州 济南网站建设公司 网络服务,网站建设站,网站建设整体设计思路,广东东莞桥头1例新冠状变量就是申请内存来存储值。也就是说#xff0c;当创建变量的时候#xff0c;需要在内存中申请空间。
内存管理系统根据变量的类型为变量分配存储空间#xff0c;分配的空间只能用来储存该类型数据。 因此#xff0c;通过定义不同类型的变量#xff0c;可以在内存中储存整…变量就是申请内存来存储值。也就是说当创建变量的时候需要在内存中申请空间。内存管理系统根据变量的类型为变量分配存储空间分配的空间只能用来储存该类型数据。因此通过定义不同类型的变量可以在内存中储存整数、小数或者字符。Java 的两大数据类型:内置数据类型引用数据类型内置数据类型Java语言提供了八种基本类型。六种数字类型四个整数型两个浮点型一种字符类型还有一种布尔型。bytebyte 数据类型是8位、有符号的以二进制补码表示的整数最小值是 -128-2^7最大值是 1272^7-1默认值是 0byte 类型用在大型数组中节约空间主要代替整数因为 byte 变量占用的空间只有 int 类型的四分之一例子byte a 100byte b -50。shortshort 数据类型是 16 位、有符号的以二进制补码表示的整数最小值是 -32768-2^15最大值是 327672^15 - 1Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一默认值是 0例子short s 1000short r -20000。intint 数据类型是32位、有符号的以二进制补码表示的整数最小值是 -2,147,483,648-2^31最大值是 2,147,483,6472^31 - 1一般地整型变量默认为 int 类型默认值是 0 例子int a 100000, int b -200000。longlong 数据类型是 64 位、有符号的以二进制补码表示的整数最小值是 -9,223,372,036,854,775,808-2^63最大值是 9,223,372,036,854,775,8072^63 -1这种类型主要使用在需要比较大整数的系统上默认值是 0L例子 long a 100000Llong b -200000L。L理论上不分大小写但是若写成l容易与数字1混淆不容易分辩。所以最好大写。floatfloat 数据类型是单精度、32位、符合IEEE 754标准的浮点数float 在储存大型浮点数组的时候可节省内存空间默认值是 0.0f浮点数不能用来表示精确的值如货币例子float f1 234.5f。doubledouble 数据类型是双精度、64 位、符合 IEEE 754 标准的浮点数浮点数的默认类型为 double 类型double类型同样不能表示精确的值如货币默认值是 0.0d例子double d1 7D ; double d2 7.; double d3 8.0; double d4 8.D; double d5 12.9867;7 是一个 int 字面量而 7D7. 和 8.0 是 double 字面量。booleanboolean数据类型表示一位的信息只有两个取值true 和 false这种类型只作为一种标志来记录 true/false 情况默认值是 false例子boolean one true。charchar 类型是一个单一的 16 位 Unicode 字符最小值是 \u0000十进制等效值为 0最大值是 \uffff即为 65535char 数据类型可以储存任何字符例子char letter A;。实例对于数值类型的基本类型的取值范围我们无需强制去记忆因为它们的值都已经以常量的形式定义在对应的包装类中了。请看下面的例子实例public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.println(基本类型byte 二进制位数 Byte.SIZE); System.out.println(包装类java.lang.Byte); System.out.println(最小值Byte.MIN_VALUE Byte.MIN_VALUE); System.out.println(最大值Byte.MAX_VALUE Byte.MAX_VALUE); System.out.println(); // short System.out.println(基本类型short 二进制位数 Short.SIZE); System.out.println(包装类java.lang.Short); System.out.println(最小值Short.MIN_VALUE Short.MIN_VALUE); System.out.println(最大值Short.MAX_VALUE Short.MAX_VALUE); System.out.println(); // int System.out.println(基本类型int 二进制位数 Integer.SIZE); System.out.println(包装类java.lang.Integer); System.out.println(最小值Integer.MIN_VALUE Integer.MIN_VALUE); System.out.println(最大值Integer.MAX_VALUE Integer.MAX_VALUE); System.out.println(); // long System.out.println(基本类型long 二进制位数 Long.SIZE); System.out.println(包装类java.lang.Long); System.out.println(最小值Long.MIN_VALUE Long.MIN_VALUE); System.out.println(最大值Long.MAX_VALUE Long.MAX_VALUE); System.out.println(); // float System.out.println(基本类型float 二进制位数 Float.SIZE); System.out.println(包装类java.lang.Float); System.out.println(最小值Float.MIN_VALUE Float.MIN_VALUE); System.out.println(最大值Float.MAX_VALUE Float.MAX_VALUE); System.out.println(); // double System.out.println(基本类型double 二进制位数 Double.SIZE); System.out.println(包装类java.lang.Double); System.out.println(最小值Double.MIN_VALUE Double.MIN_VALUE); System.out.println(最大值Double.MAX_VALUE Double.MAX_VALUE); System.out.println(); // char System.out.println(基本类型char 二进制位数 Character.SIZE); System.out.println(包装类java.lang.Character); // 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台 System.out.println(最小值Character.MIN_VALUE (int) Character.MIN_VALUE); // 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台 System.out.println(最大值Character.MAX_VALUE (int) Character.MAX_VALUE); } }运行实例 »编译以上代码输出结果如下所示基本类型byte 二进制位数8 包装类java.lang.Byte 最小值Byte.MIN_VALUE-128 最大值Byte.MAX_VALUE127 基本类型short 二进制位数16 包装类java.lang.Short 最小值Short.MIN_VALUE-32768 最大值Short.MAX_VALUE32767 基本类型int 二进制位数32 包装类java.lang.Integer 最小值Integer.MIN_VALUE-2147483648 最大值Integer.MAX_VALUE2147483647 基本类型long 二进制位数64 包装类java.lang.Long 最小值Long.MIN_VALUE-9223372036854775808 最大值Long.MAX_VALUE9223372036854775807 基本类型float 二进制位数32 包装类java.lang.Float 最小值Float.MIN_VALUE1.4E-45 最大值Float.MAX_VALUE3.4028235E38 基本类型double 二进制位数64 包装类java.lang.Double 最小值Double.MIN_VALUE4.9E-324 最大值Double.MAX_VALUE1.7976931348623157E308 基本类型char 二进制位数16 包装类java.lang.Character 最小值Character.MIN_VALUE0 最大值Character.MAX_VALUE65535Float和Double的最小值和最大值都是以科学记数法的形式输出的结尾的E数字表示E之前的数字要乘以10的多少次方。比如3.14E3就是3.14 × 103 31403.14E-3 就是 3.14 x 10-3 0.00314。https://avg.163.com/topic/detail/10050544https://avg.163.com/topic/detail/10050543https://avg.163.com/topic/detail/10050541https://avg.163.com/topic/detail/10050540https://avg.163.com/topic/detail/10050539https://avg.163.com/topic/detail/10050538https://avg.163.com/topic/detail/10050535https://avg.163.com/topic/detail/10050537https://avg.163.com/topic/detail/10050536https://avg.163.com/topic/detail/10050532https://avg.163.com/topic/detail/10050531https://avg.163.com/topic/detail/10050530https://avg.163.com/topic/detail/10050529https://avg.163.com/topic/detail/10050528https://avg.163.com/topic/detail/10050526https://avg.163.com/topic/detail/10050525https://avg.163.com/topic/detail/10050522https://avg.163.com/topic/detail/10050520https://avg.163.com/topic/detail/10050519https://avg.163.com/topic/detail/10050518https://avg.163.com/topic/detail/10050516https://avg.163.com/topic/detail/10050515https://avg.163.com/topic/detail/10050514https://avg.163.com/topic/detail/10050513https://avg.163.com/topic/detail/10050512https://avg.163.com/topic/detail/10050510https://avg.163.com/topic/detail/10050507https://avg.163.com/topic/detail/10050505https://avg.163.com/topic/detail/10050504https://avg.163.com/topic/detail/10050503https://avg.163.com/topic/detail/10050502https://avg.163.com/topic/detail/10050501https://avg.163.com/topic/detail/10050499https://avg.163.com/topic/detail/10050500https://avg.163.com/topic/detail/10050498https://avg.163.com/topic/detail/10050496https://avg.163.com/topic/detail/10050493https://avg.163.com/topic/detail/10050492https://avg.163.com/topic/detail/10050491https://avg.163.com/topic/detail/10050490https://avg.163.com/topic/detail/10050489https://avg.163.com/topic/detail/10050487https://avg.163.com/topic/detail/10050485https://avg.163.com/topic/detail/10050483https://avg.163.com/topic/detail/10050481https://avg.163.com/topic/detail/10050480https://avg.163.com/topic/detail/10050478https://avg.163.com/topic/detail/10050476https://avg.163.com/topic/detail/10050475https://avg.163.com/topic/detail/10050474https://avg.163.com/topic/detail/10050473https://avg.163.com/topic/detail/10050472https://avg.163.com/topic/detail/10050469https://avg.163.com/topic/detail/10050468https://avg.163.com/topic/detail/10050467https://avg.163.com/topic/detail/10050466https://avg.163.com/topic/detail/10050464https://avg.163.com/topic/detail/10050465https://avg.163.com/topic/detail/10050463https://avg.163.com/topic/detail/10050462https://avg.163.com/topic/detail/10050459https://avg.163.com/topic/detail/10050455https://avg.163.com/topic/detail/10050454https://avg.163.com/topic/detail/10050453https://avg.163.com/topic/detail/10050452https://avg.163.com/topic/detail/10050451https://avg.163.com/topic/detail/10050450https://avg.163.com/topic/detail/10050448https://avg.163.com/topic/detail/10050449https://avg.163.com/topic/detail/10050447https://avg.163.com/topic/detail/10050440https://avg.163.com/topic/detail/10050441https://avg.163.com/topic/detail/10050439https://avg.163.com/topic/detail/10050438https://avg.163.com/topic/detail/10050437https://avg.163.com/topic/detail/10050436https://avg.163.com/topic/detail/10050434https://avg.163.com/topic/detail/10050435https://avg.163.com/topic/detail/10050433https://avg.163.com/topic/detail/10050430https://avg.163.com/topic/detail/10050429https://avg.163.com/topic/detail/10050428https://avg.163.com/topic/detail/10050427https://avg.163.com/topic/detail/10050426https://avg.163.com/topic/detail/10050425https://avg.163.com/topic/detail/10050424https://avg.163.com/topic/detail/10050423https://avg.163.com/topic/detail/10050422https://avg.163.com/topic/detail/10050420https://avg.163.com/topic/detail/10050418https://avg.163.com/topic/detail/10050417https://avg.163.com/topic/detail/10050415https://avg.163.com/topic/detail/10050412https://avg.163.com/topic/detail/10050411https://avg.163.com/topic/detail/10050410https://avg.163.com/topic/detail/10050409https://avg.163.com/topic/detail/10050404https://avg.163.com/topic/detail/10050403https://avg.163.com/topic/detail/10050401https://avg.163.com/topic/detail/10050400https://avg.163.com/topic/detail/10050399https://avg.163.com/topic/detail/10050397https://avg.163.com/topic/detail/10050398https://avg.163.com/topic/detail/10050396https://avg.163.com/topic/detail/10050394https://avg.163.com/topic/detail/10050384https://avg.163.com/topic/detail/10050383https://avg.163.com/topic/detail/10050382https://avg.163.com/topic/detail/10050381https://avg.163.com/topic/detail/10050380https://avg.163.com/topic/detail/10050379https://avg.163.com/topic/detail/10050378https://avg.163.com/topic/detail/10050377https://avg.163.com/topic/detail/10050376https://avg.163.com/topic/detail/10050373https://avg.163.com/topic/detail/10050372https://avg.163.com/topic/detail/10050371https://avg.163.com/topic/detail/10050370https://avg.163.com/topic/detail/10050369https://avg.163.com/topic/detail/10050367https://avg.163.com/topic/detail/10050368https://avg.163.com/topic/detail/10050366https://avg.163.com/topic/detail/10050365https://avg.163.com/topic/detail/10050363https://avg.163.com/topic/detail/10050362https://avg.163.com/topic/detail/10050360https://avg.163.com/topic/detail/10050361https://avg.163.com/topic/detail/10050358https://avg.163.com/topic/detail/10050357https://avg.163.com/topic/detail/10050356https://avg.163.com/topic/detail/10050355https://avg.163.com/topic/detail/10050354https://avg.163.com/topic/detail/10050352https://avg.163.com/topic/detail/10050351https://avg.163.com/topic/detail/10050350https://avg.163.com/topic/detail/10050349https://avg.163.com/topic/detail/10050347https://avg.163.com/topic/detail/10050346https://avg.163.com/topic/detail/10050345https://avg.163.com/topic/detail/10050344https://avg.163.com/topic/detail/10050343https://avg.163.com/topic/detail/10050342https://avg.163.com/topic/detail/10050341https://avg.163.com/topic/detail/10050339https://avg.163.com/topic/detail/10050338https://avg.163.com/topic/detail/10050337https://avg.163.com/topic/detail/10050335https://avg.163.com/topic/detail/10050336https://avg.163.com/topic/detail/10050334https://avg.163.com/topic/detail/10050333https://avg.163.com/topic/detail/10050327https://avg.163.com/topic/detail/10050326https://avg.163.com/topic/detail/10050324https://avg.163.com/topic/detail/10050323https://avg.163.com/topic/detail/10050322https://avg.163.com/topic/detail/10050321https://avg.163.com/topic/detail/10050320https://avg.163.com/topic/detail/10050319https://avg.163.com/topic/detail/10050318https://avg.163.com/topic/detail/10050314https://avg.163.com/topic/detail/10050313https://avg.163.com/topic/detail/10050312https://avg.163.com/topic/detail/10050311https://avg.163.com/topic/detail/10050304https://avg.163.com/topic/detail/10050308https://avg.163.com/topic/detail/10050307https://avg.163.com/topic/detail/10050306https://avg.163.com/topic/detail/10050305https://avg.163.com/topic/detail/10050303https://avg.163.com/topic/detail/10050301https://avg.163.com/topic/detail/10050294https://avg.163.com/topic/detail/10050271https://avg.163.com/topic/detail/10050267https://avg.163.com/topic/detail/10050266https://avg.163.com/topic/detail/10050264https://avg.163.com/topic/detail/10050265https://avg.163.com/topic/detail/10050262https://avg.163.com/topic/detail/10050258https://avg.163.com/topic/detail/10050197https://avg.163.com/topic/detail/10050184https://avg.163.com/topic/detail/10050179https://avg.163.com/topic/detail/10050166https://avg.163.com/topic/detail/10050159https://avg.163.com/topic/detail/10050158https://avg.163.com/topic/detail/10050152https://avg.163.com/topic/detail/10050146https://avg.163.com/topic/detail/10050147https://avg.163.com/topic/detail/10050086https://avg.163.com/topic/detail/10050083https://avg.163.com/topic/detail/10050081https://avg.163.com/topic/detail/10050079https://avg.163.com/topic/detail/10050077https://avg.163.com/topic/detail/10050072https://avg.163.com/topic/detail/10050067https://avg.163.com/topic/detail/10050066https://avg.163.com/topic/detail/10050003https://avg.163.com/topic/detail/10049988https://avg.163.com/topic/detail/10049978https://avg.163.com/topic/detail/10049961https://avg.163.com/topic/detail/10049953https://avg.163.com/topic/detail/10049950https://avg.163.com/topic/detail/10049951https://avg.163.com/topic/detail/10049949https://avg.163.com/topic/detail/10049948https://avg.163.com/topic/detail/10049918https://avg.163.com/topic/detail/10049899https://avg.163.com/topic/detail/10049835https://avg.163.com/topic/detail/10049833https://avg.163.com/topic/detail/10049832https://avg.163.com/topic/detail/10049818https://avg.163.com/topic/detail/10049808https://avg.163.com/topic/detail/10049803https://avg.163.com/topic/detail/10049805https://avg.163.com/topic/detail/10049802https://avg.163.com/topic/detail/10049718https://avg.163.com/topic/detail/10049708https://avg.163.com/topic/detail/10049695https://avg.163.com/topic/detail/10049693https://avg.163.com/topic/detail/10049679https://avg.163.com/topic/detail/10049669https://avg.163.com/topic/detail/10049658https://avg.163.com/topic/detail/10049647https://avg.163.com/topic/detail/10049643https://avg.163.com/topic/detail/10049595https://avg.163.com/topic/detail/10049592https://avg.163.com/topic/detail/10049580https://avg.163.com/topic/detail/10049561https://avg.163.com/topic/detail/10049552https://avg.163.com/topic/detail/10049555https://avg.163.com/topic/detail/10049553https://avg.163.com/topic/detail/10049473https://avg.163.com/topic/detail/10049454https://avg.163.com/topic/detail/10049453https://avg.163.com/topic/detail/10049446https://avg.163.com/topic/detail/10049438https://avg.163.com/topic/detail/10049422https://avg.163.com/topic/detail/10049405https://avg.163.com/topic/detail/10049409https://avg.163.com/topic/detail/10049402https://avg.163.com/topic/detail/10049348https://avg.163.com/topic/detail/10049344https://avg.163.com/topic/detail/10049326https://avg.163.com/topic/detail/10049321https://avg.163.com/topic/detail/10049312https://avg.163.com/topic/detail/10049309https://avg.163.com/topic/detail/10049307https://avg.163.com/topic/detail/10049303https://avg.163.com/topic/detail/10049300https://avg.163.com/topic/detail/10049248https://avg.163.com/topic/detail/10049239https://avg.163.com/topic/detail/10049225https://avg.163.com/topic/detail/10049219https://avg.163.com/topic/detail/10049197https://avg.163.com/topic/detail/10049194https://avg.163.com/topic/detail/10049193https://avg.163.com/topic/detail/10049166https://avg.163.com/topic/detail/10049149https://avg.163.com/topic/detail/10049111https://avg.163.com/topic/detail/10049101https://avg.163.com/topic/detail/10049096https://avg.163.com/topic/detail/10049094https://avg.163.com/topic/detail/10049091https://avg.163.com/topic/detail/10049088https://avg.163.com/topic/detail/10049090https://avg.163.com/topic/detail/10049087https://avg.163.com/topic/detail/10048667https://avg.163.com/topic/detail/10048483实际上JAVA中还存在另外一种基本类型 void它也有对应的包装类 java.lang.Void不过我们无法直接对它们进行操作。类型默认值下表列出了 Java 各个类型的默认值数据类型默认值int0long0Lshort0char\u0000byte0float0.0fdouble0.0dbooleanfalse引用类型类、接口、数组null说明int,short,long,byte的默认值是0。char的默认值是\u0000空字符。float的默认值是0.0f。double的默认值是0.0d。boolean的默认值是false。引用类型类、接口、数组的默认值是null。实例public class Test { static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[] args) { System.out.println(Bool : bool); System.out.println(Byte : by); System.out.println(Character: ch); System.out.println(Double : d); System.out.println(Float : f); System.out.println(Integer : i); System.out.println(Long : l); System.out.println(Short : sh); System.out.println(String : str); } }实例输出结果为Bool :false Byte :0 Character: Double :0.0 Float :0.0 Integer :0 Long :0 Short :0 String :null引用类型在Java中引用类型的变量非常类似于C/C的指针。引用类型指向一个对象指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型比如 Employee、Puppy 等。变量一旦声明后类型就不能被改变了。对象、数组都是引用数据类型。所有引用类型的默认值都是null。一个引用变量可以用来引用任何与之兼容的类型。例子Site site new Site(Runoob)。Java 常量常量在程序运行时是不能被修改的。在 Java 中使用 final 关键字来修饰常量声明方式和变量类似final double PI 3.1415927;虽然常量名也可以用小写但为了便于识别通常使用大写字母表示常量。字面量可以赋给任何内置类型的变量。例如byte a 68; char a Abyte、int、long、和short都可以用十进制、16进制以及8进制的方式来表示。当使用字面量的时候前缀 0 表示 8 进制而前缀 0x 代表 16 进制, 例如int decimal 100; int octal 0144; int hexa 0x64;和其他语言一样Java的字符串常量也是包含在两个引号之间的字符序列。下面是字符串型字面量的例子Hello World two\nlines \This is in quotes\字符串常量和字符变量都可以包含任何 Unicode 字符。例如char a \u0001; String a \u0001;Java语言支持一些特殊的转义字符序列。符号字符含义\n换行 (0x0a)\r回车 (0x0d)\f换页符(0x0c)\b退格 (0x08)\0空字符 (0x0)\s空格 (0x20)\t制表符\双引号\单引号\\反斜杠\ddd八进制字符 (ddd)\uxxxx16进制Unicode字符 (xxxx)自动类型转换整型、实型常量、字符型数据可以混合运算。运算中不同类型的数据先转化为同一类型然后进行运算。转换从低级到高级。低 ------------------------------------ 高 byte,short,char— int — long— float — double数据类型转换必须满足如下规则1. 不能对boolean类型进行类型转换。2. 不能把对象类型转换成不相关类的对象。3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。4. 转换过程中可能导致溢出或损失精度例如int i 128; byte b (byte)i;因为 byte 类型是 8 位最大值为127所以当 int 强制转换为 byte 类型时值 128 时候就会导致溢出。5. 浮点数到整数的转换是通过舍弃小数得到而不是四舍五入例如(int)23.7 23; (int)-45.89f -45自动类型转换必须满足转换前的数据类型的位数要低于转换后的数据类型例如: short数据类型的位数为16位就可以自动转换位数为32的int类型同样float数据类型的位数为32可以自动转换为64位的double类型。实例public class ZiDongLeiZhuan{ public static void main(String[] args){ char c1a;//定义一个char类型 int i1 c1;//char自动类型转换为int System.out.println(char自动类型转换为int后的值等于i1); char c2 A;//定义一个char类型 int i2 c21;//char 类型和 int 类型计算 System.out.println(char类型和int计算后的值等于i2); } }运行结果为:char自动类型转换为int后的值等于97 char类型和int计算后的值等于66解析c1 的值为字符a,查 ASCII 码表可知对应的 int 类型值为 97 A 对应值为 65所以 i265166。强制类型转换1. 条件是转换的数据类型必须是兼容的。2. 格式(type)value type是要强制类型转换后的数据类型 实例实例public class ForceTransform { public static void main(String[] args){ int i1 123; byte b (byte)i1;//强制类型转换为byte System.out.println(int强制类型转换为byte后的值等于b); } }运行结果int强制类型转换为byte后的值等于123隐含强制类型转换1、 整数的默认类型是 int。2、 小数默认是 double 类型浮点型在定义 float 类型时必须在数字后面跟上 F 或者 f。这一节讲解了 Java 的基本数据类型。下一节将探讨不同的变量类型以及它们的用法。