// Java基本类型和引用类型的示例代码
public class TypeExample {
// 基本类型
int intValue = 10; // 整数类型
double doubleValue = 3.14; // 浮点数类型
char charValue = 'A'; // 字符类型
boolean boolValue = true; // 布尔类型
byte byteValue = 127; // 字节类型
short shortValue = 32767; // 短整型
long longValue = 123456789L;// 长整型
float floatValue = 1.23f; // 单精度浮点数
// 引用类型
String strValue = "Hello"; // 字符串类型,是引用类型
Integer intValueObj = 100; // 包装类类型,也是引用类型
Object objValue = new Object(); // 对象类型,引用类型
public static void main(String[] args) {
TypeExample example = new TypeExample();
// 输出基本类型值
System.out.println("int value: " + example.intValue);
System.out.println("double value: " + example.doubleValue);
System.out.println("char value: " + example.charValue);
System.out.println("boolean value: " + example.boolValue);
System.out.println("byte value: " + example.byteValue);
System.out.println("short value: " + example.shortValue);
System.out.println("long value: " + example.longValue);
System.out.println("float value: " + example.floatValue);
// 输出引用类型值
System.out.println("String value: " + example.strValue);
System.out.println("Integer value: " + example.intValueObj);
System.out.println("Object value: " + example.objValue);
}
}
int, double, char, boolean, byte, short, long 和 float。这些类型直接存储数据的值,它们不是对象。String 是一个常见的引用类型,它实际上是一个类。引用类型存储的是对对象的引用(即内存地址),而不是直接存储对象本身。通过这段代码,你可以看到如何声明和使用基本类型和引用类型的变量,并且可以在 main 方法中输出它们的值。
上一篇:java 金额转大写
下一篇:java中switch用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站