基本类型 | 大小(字节) | 默认值 | 封装类 |
byte | 1 | (byte)0 | Byte |
short | 2 | (short)0 | Short |
int | 4 | 0 | Integer |
long | 8 | 0L | Long |
float | 4 | 0.0f | Float |
double | 8 | 0.0d | Double |
boolean | - | false | Boolean |
char | 2 | \u0000(null) | Character |
boolean result = obj instanceof Class
int i = 0; System.out.println(i instanceof Integer);//编译不通过 i必须是引用类型,不能是基本类型 System.out.println(i instanceof Object);//编译不通过
Integer integer = new Integer(1); System.out.println(integer instanceof Integer);//true
//false ,在 JavaSE规范 中对 instanceof 运算符的规定就是:如果 obj 为 null,那么将返回 false。 System.out.println(null instanceof Object);
Integer i = new Integer(10);
Integer i = 10;
public class Main { public static void main(String[] args) { Integer Integer Integer Integer i1 = 100; i2 = 100; i3 = 200; i4 = 200; System.out.println(i1==i2); System.out.println(i3==i4); } }
public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i 128]; else return new Integer(i); }
其中IntegerCache类的实现为: private static class IntegerCache { static final int high; static final Integer cache[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; cache = new Integer[(high - low) 1]; int j = low; for(int k = 0; k < cache.length; k ) cache[k] = new Integer(j ); } private IntegerCache() {} }
public class Main { public static void main(String[] args) { Double Double Double Double i1 = 100.0; i2 = 100.0; i3 = 200.0; i4 = 200.0; System.out.println(i1==i2); System.out.println(i3==i4); } }
public class Father { public static void main(String[] args) { // TODO Auto-generated method stub Son s = new Son(); s.sayHello(); } public void sayHello() { System.out.println("Hello"); } } class Son extends Father{ @override public void sayHello() { System.out.println("Hello"); } }
public class Father { public static void main(String[] args) { // TODO Auto-generated method stub Father s = new Father(); s.sayHello(); s.sayHello("wintershii"); } public void sayHello() { System.out.println("Hello"); } public void sayHello(String name) { System.out.println("Hello" " " name); } }
private final char value[];
/** * The value is used for character storage. */ char[] value;
int[] a = new int[4];//推介使用int[] 这种方式初始化 int c[] ={23,43,56,78};//长度:4,索引范围:[0,3]
String str = new String("str"); System.out.println(str);
// 注意:wrf这个引用也是强引用,它是指向SoftReference这个对象的, // 这里的软引用指的是指向new String("str")的引用,也就是SoftReference类中T SoftReference<String> wrf = new SoftReference<String>(new String("str"));
WeakReference<String> wrf = new WeakReference<String>(str);
PhantomReference<String> prf = new PhantomReference<String>(new String("str"), new ReferenceQueue<>());
List<Integer> iniData = new ArrayList<>()
public calss PreCache{ static{ //执行相关操作 } }
import static java.lang.Math.*; public class Test{ public static void main(String[] args){ //System.out.println(Math.sin(20));传统做法 System.out.println(sin(20)); } }
byte a = 127; byte b = 127; b = a b; // 报编译错误:cannot convert from int to byte b = a;
short s1= 1; s1 = s1 1;
short s1= 1; s1 = 1;
Class.forName('com.mysql.jdbc.Driver.class');//加载MySQL的驱动类
Class Object is the root of the class hierarchy.Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. 大致意思:Object 是所有类的根,是所有类的父类,所有对象包括数组都实现了 Object 的方法。
User user=new User();
User user=User.class.newInstance(); Object object=(Object)Class.forName("java.lang.Object").newInstance()
User user=new User(); //clazz就是一个User的类对象 Class<?> clazz=user.getClass();
//clazz就是一个User的类对象 Class<?> clazz=User.class;
Class<?> clazz = Class.forName("com.tian.User");
Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable . If a thread-safe highly- concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.