赞
踩
类型 | 泛型 | 存储位置 | 占用空间 | 默认值 | 比较方式 |
---|---|---|---|---|---|
基本数据类型 | × | 栈、堆、方法区(元空间) | 小 | 不同类型不同默认值 | == |
包装类型 | √ | 堆 | 大 | null | equals() |
类型间转换:
// 基本数据类型 int intValue = 5; char charValue = 'a'; boolean booleanValue = true; byte byteValue = 1; short shortValue = 2; long longValue = 100L; float floatValue = 3.14f; double doubleValue = 3.14159; // 自动装箱:将基本数据类型转换为包装类型 Integer boxedIntValue = intValue; Character boxedCharValue = charValue; Boolean boxedBooleanValue = booleanValue; Byte boxedByteValue = byteValue; Short boxedShortValue = shortValue; Long boxedLongValue = longValue; Float boxedFloatValue = floatValue; Double boxedDoubleValue = doubleValue; // 显示装箱:使用包装类的构造函数 Integer explicitlyBoxedIntValue = Integer.valueOf(intValue); Character explicitlyBoxedCharValue = Character.valueOf(charValue); Boolean explicitlyBoxedBooleanValue = Boolean.valueOf(booleanValue); Byte explicitlyBoxedByteValue = Byte.valueOf(byteValue); Short explicitlyBoxedShortValue = Short.valueOf(shortValue); Long explicitlyBoxedLongValue = Long.valueOf(longValue); Float explicitlyBoxedFloatValue = Float.valueOf(floatValue); Double explicitlyBoxedDoubleValue = Double.valueOf(doubleValue);
Integer boxedIntValue = Integer.valueOf(5); Character boxedCharValue = Character.valueOf('a'); Boolean boxedBooleanValue = Boolean.valueOf(true); Byte boxedByteValue = Byte.valueOf((byte) 1); Short boxedShortValue = Short.valueOf((short) 2); Long boxedLongValue = Long.valueOf(100L); Float boxedFloatValue = Float.valueOf(3.14f); Double boxedDoubleValue = Double.valueOf(3.14159); // 自动拆箱:将包装类型转换为基本数据类型 int intValue = boxedIntValue; char charValue = boxedCharValue; boolean booleanValue = boxedBooleanValue; byte byteValue = boxedByteValue; short shortValue = boxedShortValue; long longValue = boxedLongValue; float floatValue = boxedFloatValue; double doubleValue = boxedDoubleValue; // 显示拆箱:使用包装类的对应方法 int explicitlyUnboxedIntValue = boxedIntValue.intValue(); char explicitlyUnboxedCharValue = boxedCharValue.charValue(); boolean explicitlyUnboxedBooleanValue = boxedBooleanValue.booleanValue(); byte explicitlyUnboxedByteValue = boxedByteValue.byteValue(); short explicitlyUnboxedShortValue = boxedShortValue.shortValue(); long explicitlyUnboxedLongValue = boxedLongValue.longValue(); float explicitlyUnboxedFloatValue = boxedFloatValue.floatValue(); double explicitlyUnboxedDoubleValue = boxedDoubleValue.doubleValue();
类型 | 调用方式 | 访问限制 |
---|---|---|
静态方法 | 对象名.方法名()/ 类名.方法名() | 只能访问类的静态变量或静态方法 |
普通方法 | 对象名.方法名() | 无限制 |
注:静态方法是在类加载的时候就分配了内存空间,而类的非静态成员是在创建对象时才分配内存空间,因此静态方法访问非静态成员是非法的。 |
public class methodTest { //整数+整数 public int add(int a,int b){ return a+b; } //整数+浮点数 public double add(double a,int b){ return a+b; } //浮点数+浮点数 public double add(double a,double b){ return a+b; } public static void main(String[] args){ methodTest mt=new methodTest(); int result1=mt.add(1,2); double result2 = mt.add(1.2,5); double result3=mt.add(2.3,5.2); } }
// 父类 class Animal { // 父类中的方法 public void makeSound() { System.out.println("The animal makes a sound"); } // 父类中的另一方法 public void sleep() { System.out.println("The animal is sleeping"); } } // 子类 class Dog extends Animal { // 子类重写父类的方法 @Override public void makeSound() { System.out.println("The dog barks"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。