1.1 Integer

对int类型的数据的包装。 private final int value; 维护Integer的数据

public final class Integer  extends Number  implements Comparable<Integer>
static int MAX_VALUE  
static int MIN_VALUE      
Integer(int value) 
Integer(String s)   将字符串转整型数据  

 

==装箱: 基本类型 转换成 包装类型的对象 包装类.valueOf()==

==拆箱: 将包装类对象转换成基本类型的数据==

  private static void demo2() {
       int num = 100;
       Integer num1 = 100;
      // Integer num2 = Integer.valueOf(100);
       // 默认是int 基本类型 转换成 包装类型的对象 包装类.valueOf(基本类型 变量)

       //拆箱: 将包装类对象转换成基本类型的数据
       //System.out.println(num == num1.intValue());

       System.out.println(num == num1);//true
       
  }

 

==整数缓存池 (容器:数组)==

 public static Integer valueOf(int i) {//100 200
       if (i >= IntegerCache.low && i <= IntegerCache.high)
           return IntegerCache.cache[i + (-IntegerCache.low)];
       return new Integer(i);
  }
//i low---high 直接获得是数组的指定索引的元素数据   -128--127
private static class IntegerCache {
       static final int low = -128;
       static final int high;
       static final Integer cache[];

       static {
           // high value may be configured by property
           int h = 127;
           String integerCacheHighPropValue =
               sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
           if (integerCacheHighPropValue != null) {
               try {
                   int i = parseInt(integerCacheHighPropValue);
                   i = Math.max(i, 127);
                   // Maximum array size is Integer.MAX_VALUE
                   h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
              } catch( NumberFormatException nfe) {
                   // If the property cannot be parsed into an int, ignore it.
              }
          }
           high = h;

           cache = new Integer[(high - low) + 1];
           int j = low;
           for(int k = 0; k < cache.length; k++)
               cache[k] = new Integer(j++);

           // range [-128, 127] must be interned (JLS7 5.1.7)
           assert IntegerCache.high >= 127;
      }

       private IntegerCache() {}
  }

 

Byte—ByteCache Short—-ShortCache Long—LongCache Character—-CharacterCache(0-127)

 

parseInt

 public static int parseInt(String s, int radix)
               throws NumberFormatException
  {

       if (s == null) {
           throw new NumberFormatException("null");
      }

       if (radix < Character.MIN_RADIX) {
           throw new NumberFormatException("radix " + radix +
                                           " less than Character.MIN_RADIX");
      }

       if (radix > Character.MAX_RADIX) {
           throw new NumberFormatException("radix " + radix +
                                           " greater than Character.MAX_RADIX");
      }

       int result = 0;//最后转换的数值结果
       boolean negative = false;//正数 负整数
       int i = 0, len = s.length();//获得字符串的字符个数
       int limit = -Integer.MAX_VALUE;
       int multmin;
       int digit;

       if (len > 0) {
           char firstChar = s.charAt(0);// charAt(index) 获得字符串指定索引的字符的数据
           if (firstChar < '0') { // Possible leading "+" or "-"
               if (firstChar == '-') {
                   negative = true;
                   limit = Integer.MIN_VALUE;
              } else if (firstChar != '+')
                   throw NumberFormatException.forInputString(s);

               if (len == 1) // Cannot have lone "+" or "-"
                   throw NumberFormatException.forInputString(s);
               i++;
          }
           multmin = limit / radix;
           while (i < len) {      
               digit = Character.digit(s.charAt(i++),radix);//10
               //将一个指定字符内容转换成指定进制内的数字  
               //digit = 1
               //digit = 2
               //digit = 3
               
               if (digit < 0) {
                   throw NumberFormatException.forInputString(s);
              }
               if (result < multmin) {
                   throw NumberFormatException.forInputString(s);
              }
               result *= radix;// result = result*radix;
               if (result < limit + digit) {
                   throw NumberFormatException.forInputString(s);
              }
               result -= digit;// result = result-digit; -123
          }
      } else {
           throw NumberFormatException.forInputString(s);
      }
       return negative ? result : -result;
  }

 

 

String 转 int

 private static int strConvertToInt(String numStr) {

       Objects.requireNonNull(numStr);
       int length = numStr.length();
       if (length == 0) {
           throw new NumberFormatException("字符串为一个空字符串");
      }

       final int radix = 10;
       boolean flag = false;
       int index = 0;
       //第一个字符: + - 汉字/字母 数字 0-9   48-57
       char firstChar = numStr.charAt(index);
       if (firstChar == '-') {
           flag = true;//当前数字为一个负数
           index++;
      } else if (firstChar == '+') {
           index++;
      } else if (firstChar < 48 || firstChar > 57) {
           throw new NumberFormatException("字符串为一个空字符串");
      }
       
       int result = 0;
       while (index < length) {
           char ch = numStr.charAt(index++);//'1'=49
           if (ch < 48 || ch > 57) {
               throw new NumberFormatException("字符串为一个空字符串");
          }
           //肯定获得数字一个字符
           int num = ch - 48;//1 进制值 10
           result = result * radix + num;
      }
       return flag ? -result : result;
  }

 

 

1.2 Character

 public static void main(String[] args) {


       //包装了基本数据类型char数据
       //1. 创建对象
       Character ch1 = new Character('a');
       //装箱 Character.valueOf();
       Character ch2 = Character.valueOf('a');//0-127 ASCII
       //Character ch3 = '\u0000';

       System.out.println(ch1);
       System.out.println(ch2);
       //System.out.println(ch3);

       System.out.println(Character.MIN_RADIX);
       System.out.println(Character.MAX_RADIX);

       //功能方法

       Character character = 'a';
       System.out.println(Character.isDigit(character));//判断一个字符是不是一个数字
       System.out.println(Character.isLetter(character));
       if (Character.isLetter(character)) {
           System.out.println(Character.toUpperCase(character));
      }

       //将指定字符转换成指定进制内数据
      // System.out.println(Character.digit('F', 16));//-1
       //System.out.println(Integer.parseInt("xyz", 36));

  }

 

2. Math

执行算术运算和几何运算。

public static void main(String[] args) {


       System.out.println(Math.abs(-10));
       int random = (int) (Math.random() * 900 + 100);
       System.out.println(random);

       System.out.println(Math.pow(2, 3));
       System.out.println(Math.min(10, 3));
       System.out.println(Math.max(10, 3));

       System.out.println(Math.round(5.678));
}

 

 

3. Object

方法:
   protected Object clone()  克隆(复制)  默认浅克隆---> 数据还是内存地址值?
     
   // 成对出现的 (jvm的规则)  
   boolean equals(Object obj)  
   int hashCode()  获得对象的hashcode
           
   String toString()  将对象的数据转换成字符串操作
   protected void finalize()  提示GC回收对象   不推荐手动调用

   Class<?> getClass() 获得正在运行的类或者接口的Class类对象 ----> 反射的基础
   
   //与线程的通信有关 ----> 监视器对象---->同步  
   void notify()  ---> 在一个线程里面唤醒其它处于等待的线程
   void notifyAll()  
   void wait()  ---> 当前线程等待
   void wait(long timeout)  ----> 时间内等待
   void wait(long timeout, int nanos)  

 

==3.1 比较对象==

如何比较2个对象? 为什么要重写equals和hashcode?

2个对象相等  hashcode肯定相等。
   
1. jvm优先使用hashcode比较   2个hashcode不一致  2个对象肯定不等 (数值运算  比较快)
  底层hash表  不同的2个对象  hashcode是相等的  
       String str1 = "Ma";
       String str2 = "NB";
       System.out.println(str1.hashCode());//2484
       System.out.println(str2.hashCode());

 依然equals()比较    集合框架: Set/Map
     
2. 只是重写equals  true  但是hashcode不一样    违背规则      

 

 public boolean equals(Object obj) {
       return (this == obj);
  }
 //不需要 
   //重写equals方法
   //2个对象的相等的规则是我们自己定义的
   //id name age 都相等的时候 2个对象是相等的
   @Override
   public boolean equals(Object obj) {
       if (!(obj instanceof Student)) {
           return false;
      }
       Student student = (Student) obj;
       //return id.equals(student.id) && name.equals(student.name) && age.equals(student.age);
       return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(age, student.age);
  }

   //必然重写hashcode
   //Set/Map
   //jvm底层: 判断对象 优先使用hashcode 2个对象的hashcode不一样的话 这2个对象不相等
   //不能完全信任: 不同的2个对象的是可能会相等的(底层还是要执行equals)


   @Override
   public int hashCode() {
//       int hash = 0;
//       hash = hash * 31 + id.hashCode();
//       hash = hash * 31 + name.hashCode();
//       hash = hash * 31 + age.hashCode();
       return Objects.hash(id, name, age);
  }

 

 

==3.2 克隆==

什么是克隆? Object.clone() 默认是什么克隆? 深克隆和浅克隆有什么区别?

java.lang.CloneNotSupportedException: com.javasm.object_.UserInfo
at java.lang.Object.clone(Native Method)
at com.javasm.object_.UserInfo.clone(UserInfo.java:32)
at com.javasm.object_.CloneDemo.main(CloneDemo.java:20)
Cloneable:  标识接口  一个类实现Cloneable  代表这个类能够被克隆对象
// Cloneable : 标识接口  一个类实现Cloneable  代表这个类能够被克隆对象
@Setter
@Getter
@ToString
public class UserInfo extends Object implements Cloneable {

   private Integer id;
   private String name;
   private String[] hobby;


   //重写父类的clone的方法


   @Override
   public UserInfo clone() {
       UserInfo cloneUser = null;
       try {
           cloneUser = (UserInfo) super.clone();// 浅克隆
           //遍历式处理属性 深克隆/IO(序列化流)
//           cloneUser.setHobby(Arrays.copyOf(hobby,hobby.length));
           cloneUser.setHobby(hobby.clone());
      } catch (CloneNotSupportedException e) {
           e.printStackTrace();
      }
       return cloneUser;
  }

   public UserInfo(Integer id, String name, String[] hobby) {
       System.out.println("有参创建对象");
       this.id = id;
       this.name = name;
       this.hobby = hobby;
  }

   public UserInfo() {
       System.out.println("无参创建对象");
  }
}
public static void main(String[] args) {

       String[] hobby = {"code", "music", "sleep"};
       UserInfo userInfo = new UserInfo(1, "张三", hobby);
       System.out.println("userinfo:" + userInfo);

       //目前创建对象: new 构造();
       //克隆对象(不执行构造方法)---->jvm自己的规则
       UserInfo clone = userInfo.clone();
       System.out.println("clone:" + clone);
       System.out.println(clone == userInfo);//false 2块内存


       System.out.println("------------修改了--------------");
       userInfo.setId(1001);
       userInfo.setName("张三丰");
       userInfo.getHobby()[0] = "game";

       System.out.println("userinfo:" + userInfo);
       System.out.println("clone:" + clone);

       //数组: 引用类型 ----> 值传递 复制的内存地址值
  }

 

 

==3.3 回收对象==

GC可以回收哪些引用?

1.强引用
   //强引用
   private static void demo1() {
   UserInfo[] userInfos = new UserInfo[1024 * 1000];

   UserInfo userInfo = new UserInfo(1, "jim", null);
   //强引用: 表示对象必须的 有用的对象

   //运行期间 500M 300+300 内存不足 宁愿抛出堆栈内存溢出问题 也不会回收对象
   //-----------------------很多逻辑
   UserInfo[] userInfos1 = new UserInfo[1024 * 1000];
}    

 

2.软引用
private static void demo2() {
       //软引用: 有用非必须对象
       //一直判断
//       if(内存不足了){
//           //回收软引用关联的对象
//       }
       UserInfo userInfo = new UserInfo(1, "jim", null);
       SoftReference<UserInfo> softReference = new SoftReference<>(userInfo);
       //将对象交给SoftReference进行维护
       System.out.println(softReference.get());

       userInfo = null;
       System.gc();
       System.out.println(softReference.get());
  }
   

 

3. 弱引用
private static void demo3() {
       //弱引用: 有用非必须对象
       //将对象从有用变为无用 手动通知GC 立马回收对象
       UserInfo userInfo = new UserInfo(1, "jim", null);
       WeakReference<UserInfo> weakReference = new WeakReference<>(userInfo);
       //ThreadLocal
       System.out.println(weakReference.get());
       userInfo = null;
       System.gc();
       System.out.println(weakReference.get());
  }  
   

 

4. 虚引用
  private static void demo4() {
       //虚引用: 没用的对象
       UserInfo userInfo = new UserInfo(1, "jim", null);
       ReferenceQueue<UserInfo> referenceQueue = new ReferenceQueue<>();
       PhantomReference<UserInfo> phantomReference = new PhantomReference<>(userInfo, referenceQueue);
       System.out.println(phantomReference.get());
  }    
   

 

 

 

4. Class

3种方式获得Class类对象:

  public static void main(String[] args) {
      /* UserInfo userInfo = new UserInfo();

       //1.调用getClass方法(很少使用 必须先有对象 )
       Class infoClass = userInfo.getClass();//等同于获得了UserInfo.class
       System.out.println(infoClass.toString());
       System.out.println(infoClass.getSimpleName());//类名
       System.out.println(infoClass.getName());//类的全限定名称   包名+类名*/


    /*   //2. 调用静态属性 类名.class属性   推荐 (获得本项目内的类/接口的Class对象)
       Class infoClass = UserInfo.class;
       System.out.println(infoClass);
       System.out.println(infoClass.getSimpleName());
       System.out.println(infoClass.getName());*/

       //3. 使用Class.forName("类的全限定名称") 推荐 框架 加载第三方资源类库里面类或者接口
       try {
           Class infoClass = Class.forName("com.javasm.object_.UserInfo");
           System.out.println(infoClass);
           System.out.println(infoClass.getSimpleName());
           System.out.println(infoClass.getName());
      } catch (ClassNotFoundException e) {
           e.printStackTrace();
      }

  }
 

原文地址:http://www.cnblogs.com/wang1999an/p/16799129.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性