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)
示例代码:
- package com.wbf.test;
- import java.util.HashMap;
- import java.util.Map;
- public class MapTest {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- Map m = new HashMap();
- m.put("a", "aaa");
- m.put("b", "bbb");
- m.put("c", "ccc");
- m.put("d", "ddd");
- m.put("e", "eee");
- Map m1 = new HashMap();
- m1.put("f", "fff");
- m1.putAll(m);//将指定的map中的元素都copy到新的map中
- System.out.println(m1.keySet().toString());
- System.out.println(m1.values().toString());
- }
- }
[f, d, e, b, c, a]
[fff, ddd, eee, bbb, ccc, aaa]
3)十六进制,二进制,八进制 ---> 十进制
示例代码:
- package com.wbf.test;
- import java.math.BigInteger;
- public class TestDemo {
- public static void main(String[] args) {
- String str = "0c";//十六进制
- String str1 = "0101";//二进制
- String str2 = "14";//八进制
- /*
- * BigInteger(String val, int radix)
- * 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
- */
- BigInteger b = new BigInteger(str, 16);
- BigInteger b1 = new BigInteger(str1, 2);
- BigInteger b2 = new BigInteger(str2, 8);
- System.out.println(b.intValue());
- System.out.println(b1.intValue());
- System.out.println(b2.intValue());
- }
- }
运行结果:
12
5
12
4)Java短路与(&&)、if...else...
示例代码:
- public class TestDemo {
- public static void main(String args[]){
- boolean b = true;//情况1
- String s = null;
- //boolean b = true;//情况2
- //String s = "not null";
- //boolean b = false;//情况3
- //String s = null;
- //boolean b = false;//情况4
- //String s = "not null";
- if (b && s == null)
- {
- System.out.println("xxxxx");
- }
- else// b == true && s != null 或者 b ==false && s == null 或者 b == false && s != null
- {
- System.out.println("yyyyy");
- }
- }
- }
- b && s == null的反面是三种情况,必须思考周全