Java杂记

JavaSE杂记

主要用于记录日常工作和学习中的知识

具体内容

1)Math类:public final class Math extends Object

  • 在Java doc文档中是无法看到Math类的构造函数的,因为Math类的构造方法被声明为私有,所以在外部是无法对Math实例化的。
  • 在Math类中的全部方法都是static,即:所有方法都是通过Math.方法名称即可
  • 使用final来修饰类,说明此类不允许继承(没有子类)

 2)将一个Map中的所有元素copy到另一个Map中:putAll(Map m)

       示例代码:   

[java]  view plain copy
  1. package com.wbf.test;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. public class MapTest {  
  7.     @SuppressWarnings("unchecked")  
  8.     public static void main(String[] args) {  
  9.         Map m = new HashMap();  
  10.         m.put("a""aaa");  
  11.         m.put("b""bbb");  
  12.         m.put("c""ccc");  
  13.         m.put("d""ddd");  
  14.         m.put("e""eee");  
  15.           
  16.         Map m1 = new HashMap();  
  17.         m1.put("f""fff");  
  18.         m1.putAll(m);//将指定的map中的元素都copy到新的map中  
  19.           
  20.         System.out.println(m1.keySet().toString());  
  21.         System.out.println(m1.values().toString());  
  22.           
  23.     }  
  24.   
  25. }  
运行结果:

[f, d, e, b, c, a]
[fff, ddd, eee, bbb, ccc, aaa]


 3)十六进制,二进制,八进制 ---> 十进制

示例代码:

[java]  view plain copy
  1. package com.wbf.test;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5. public class TestDemo {  
  6.     public static void main(String[] args) {  
  7.           
  8.         String str = "0c";//十六进制  
  9.         String str1 = "0101";//二进制  
  10.         String str2 = "14";//八进制  
  11.         /* 
  12.          * BigInteger(String val, int radix) 
  13.          * 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger 
  14.          */  
  15.         BigInteger b = new BigInteger(str, 16);  
  16.         BigInteger b1 = new BigInteger(str1, 2);  
  17.         BigInteger b2 = new BigInteger(str2, 8);  
  18.         System.out.println(b.intValue());  
  19.         System.out.println(b1.intValue());  
  20.         System.out.println(b2.intValue());  
  21.     }  
  22. }  

运行结果:

12
5
12

4)Java短路与(&&)、if...else...

示例代码:

[java]  view plain copy
  1. public class TestDemo {  
  2.     public static void main(String args[]){  
  3.           
  4.         boolean b = true;//情况1  
  5.         String s = null;  
  6.           
  7.         //boolean b = true;//情况2  
  8.         //String s = "not null";  
  9.           
  10.         //boolean b = false;//情况3  
  11.         //String s = null;  
  12.           
  13.         //boolean b = false;//情况4  
  14.         //String s = "not null";  
  15.           
  16.         if (b && s == null)  
  17.         {  
  18.             System.out.println("xxxxx");  
  19.         }  
  20.         else// b == true && s != null 或者 b ==false && s == null 或者  b == false && s != null      
  21.         {  
  22.             System.out.println("yyyyy");  
  23.         }  
  24.     }  
  25. }  
解释:
  1. b && s == null的反面是三种情况,必须思考周全
5)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值