Java语言基础
一、基本数据类型(8个)
整型:byte(1字节)、 short(2字节)、int(4字节)、 long(8字节,使用时一定要在数值后面加上 L) 注 :整型类型的最高位表示数据符号位,因此实际上数据的表示范围为(
−
2
字节数
,
2
字节数
- 2^{字节数},2^{字节数}
− 2 字节数 , 2 字节数 ) 浮点型:float(4字节),double(8字节) 字符型:char(2字节) 布尔型: boolean(1bit)
二、包装类型(Java中的类)
整型:Byte、Short、Integer、Long 浮点型:Float、Double 字符型:Character 布尔型:Boolean 注 :String不是包装类型,是Java中的一个类
三、基本数据类型和包装类型间区别与联系
类型 泛型 存储位置 占用空间 默认值 比较方式 基本数据类型 × 栈、堆、方法区(元空间) 小 不同类型不同默认值 == 包装类型 √ 堆 大 null equals()
类型间转换:
自动装箱 :直接将基本数据类型的变量赋值给包装类对象,这是Java编译器自动处理的,从Java 5开始引入的特性。例如,Integer boxedIntValue = intValue。显式装箱 :使用包装类的静态方法valueOf进行转换,例如,Integer explicitlyBoxedIntValue = Integer.valueOf(intValue)。自动拆箱 :直接将包装类型的对象赋值给基本数据类型变量显式拆箱 :使用包装类的静态方法xxxValue()进行转换,例如,int baseValue = boxedIntValue.intValue()。
基本数据类型 -> 包装类型
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 ) ;
}
}
重写
方法重写:指的是在子类重写父类中的同一个方法,实现不同的逻辑 方法名、参数列表必须相同,子类方法返回值类型应比父类方法返回值类型更小或相等 注 :private/final/static 修饰的方法不能被重写,构造方法不能被重写
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" ) ;
}
}