String
StringTest.java
http://blog.csdn.net/RichardSundusky/archive/2007/02/12/1508028.aspx
hashCode()的返回值和equals()的关系如下:
如果x.equals(y)返回“true”,那么x和y的hashCode()必须相等。
如果x.equals(y)返回“false”,那么x和y的hashCode()有可能相等,也有可能不等。
http://zhidao.baidu.com/question/47831363.html
1.如果是基本变量,没有hashcode和equals方法,基本变量的比较方式就只有==;
2.如果是变量,由于在java中所有变量定义都是一个指向实际存储的一个句柄(你可以理解为c++中的指针),在这里==是比较句柄的地址(你可以理解为指针的存储地址),而不是句柄指向的实际内存中的内容,如果要比较实际内存中的内容,那就要用equals方法,但是!!!
如果是你自己定义的一个类,比较自定义类用equals和==是一样的,都是比较句柄地址,因为自定义的类是继承于object,而object中的equals就是用==来实现的,你可以看源码。
那为什么我们用的String等等类型equals是比较实际内容呢,是因为String等常用类已经重写了object中的equals方法,让equals来比较实际内容,你也可以看源码。
3. hashcode
在一般的应用中你不需要了解hashcode的用法,但当你用到hashmap,hashset等集合类时要注意下hashcode。
你想通过一个object的key来拿hashmap的value,hashmap的工作方法是,通过你传入的object的hashcode在内存中找地址,当找到这个地址后再通过equals方法来比较这个地址中的内容是否和你原来放进去的一样,一样就取出value。
所以这里要匹配2部分,hashcode和equals
但假如说你new一个object作为key去拿value是永远得不到结果的,因为每次new一个object,这个object的hashcode是永远不同的,所以我们要重写hashcode,你可以令你的hashcode是object中的一个恒量,这样永远可以通过你的object的hashcode来找到key的地址,然后你要重写你的equals方法,使内存中的内容也相等。。。
package com.javaeye.lindows.lang; public class StringTest { /** * @param args */ private void StringLengthMethod() { // 看看128位算法 String string = "488ab41232d6b6c63da35752033b2e9cc424e008dca66331e43845bf9ba962a5de4363bf955033c5634bb34b2b6f4c49e6842e257c0ffa86942aaf78abeb70a8"; System.out.println(string.length()); } public static void main(String[] args) { StringTest sTest = new StringTest(); sTest.StringLengthMethod();// long date = System.currentTimeMillis(); String s = "abc"; String s1 = "abc"; System.out.println(s); // 等效于 // char data[] = { 'a', 'b', 'c' }; // String str = new String(data); System.out.println(s == s1); // true System.out.println(s.equals(s1));// true // String new对象时的变化 String s2 = new String("abc"); String s3 = new String("abc"); System.out.println(s2 == s3); // false new出的地址不等 System.out.println(s2.equals(s3)); // true 但内容相同 // 场景:表单的用户名忽略空白和大小写 String s4 = new String(" abc"); String s5 = "ABC "; // s4.trim();//去掉前后空白 System.out.println(s4.trim().equalsIgnoreCase(s5.trim()));// true if (s4.equalsIgnoreCase(s5)) { System.out.println("yes"); // do something } else { System.out.println("no"); // do something } } }
MessFormatTest.java
package com.javaeye.lindows.lang; /** * @author Lindows */ public class MessageFormatTest { public MessageFormatTest() { } public static void main(String[] args) { // jdk1.4不能用String.format(),得用另一种方法格式化字符串 String helloString = "hello {0},you are {1}"; helloString = java.text.MessageFormat.format(helloString, new Object[] { "AA", "GREAT" }); System.out.println(helloString); // 结果: hello AA,you are GREAT // %d标识数字 %s标识字符 // http://www.iteye.com/topic/313394 String sqlString = "select * from dept d where d.id=%d and d.name=%s"; System.out.println(String.format(sqlString, 100, "lindows")); /* * %2$d标识 第2个类型为整数的参数 * %1$s标识 第1个类型为字符串的参数 */ String sqlString2 = "select * from dept d where d.id=%2$d and d.name=%1$s"; System.out.println(String.format(sqlString2, "lindows", 100)); } }
http://www.iteye.com/topic/252767
package com.javaeye.lindows.lang; public class Int2String { /** * int类型转化成String类型的三种方式 * (1) String.valueOf(i) * (2) Integer.toString(i) * (3) i+"" */ public static void main(String[] args) { // 数组下标从0开始 int[] intArr = new int[100000]; //为了公平分别定义三个数组 String[] strArr1 = new String[100000]; String[] strArr2 = new String[100000]; String[] strArr3 = new String[100000]; // 赋值 Long long1 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { intArr[i] = i + 1; } // String.valueOf(); // String的valueOf(int型参数)方法转换成 int Long long2 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { strArr1[i] = String.valueOf(intArr[i]); } // Integer.toString(); // Integer的toString(int 型参数)方法转换成 String Long long3 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { strArr2[i] = Integer.toString(intArr[i]); } // i + ""; //加空字符串自动转换 String Long long4 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { strArr3[i] = intArr[i] + ""; } Long long5 = System.currentTimeMillis(); System.out.println(long1); System.out.println(long2); System.out.println("赋值: "+(long2 - long1)); System.out.println("String.valueOf(): "+(long3 - long2)); System.out.println("Integer.toString() :"+(long4 - long3)); System.out.println("i+ \"\" :" + (long5 - long4)); /* 1234719707265 1234719707281 赋值: 16 String.valueOf(): 140 Integer.toString() :94 i+ "" :141 */ } }
String,int,Integer,char 几个类型常见转换
http://derpvail.iteye.com/blog/261015
http://antonyup-2006.iteye.com/blog/284651
http://www.blogjava.net/algz/articles/227937.html
如何将字串 String 转换成整数 int?
int i = Integer.valueOf(my_str).intValue();
int i=Integer.parseInt(str);
如何将字串 String 转换成Integer ?
Integer integer=Integer.valueOf(str);
如何将整数 int 转换成字串 String ?
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
如何将整数 int 转换成Integer ?
Integer integer=new Integer(i);
如何将Integer 转换成字串 String ?
Integer integer=String
如何将Integer 转换成 int ?
int num=Integer.intValue();
如何将String转换成 BigDecimal ?
BigDecimal d_id = new BigDecimal(str);
由于BigDecimal精度问题导致退货订单创建失败 / 中台--肖宇14050302 / 18705179907
http://wiki.cns*****.com/pages/viewpage.action?pageId=19369086
转至元数据起始 问题现象: OMSD创建完退货订单后,调用I-OMSD-OMS-003接口给交易创建逆向订单,请求和响应报文如下:
http://dl2.iteye.com/upload/attachment/0103/6436/9f9e61eb-a543-3192-89c6-17a63274802a.jpg
BigDecimal_request
http://dl2.iteye.com/upload/attachment/0103/6440/50fa930a-087c-336a-a9dc-70d1c3549316.jpg
BigDecimal_response
本意是想传0.2过去,但是却传了一大串数字:0.200000000000000011102230246251565404236316680908203125
请求报文中refundAmt字段赋值代码:input.setRefundAmt(df.getRefundAmt().toString());
原因分析:
df.getRefundAmt()的返回类型是BigDecimal类型,导致输出的时候精度出现问题,测试类如下:
public class TestMath {
public static void main(String[] args) {
Double a = 0.2;
BigDecimal b = new BigDecimal(a);
System.out.println(a);
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(String.valueOf(b.doubleValue()));
}
}
运行结果:
0.2
0.2
0.200000000000000011102230246251565404236316680908203125
0.2
结论:
BigDecimal精度问题导致传值错误。
解决方法:
转成double值输出:input.setRefundAmt(String.valueOf(df.getRefundAmt().doubleValue()));
拓展:
public class TestMath {
public static void main(String[] args) {
System.out.println(0.05+0.01);
System.out.println(1.0 - 0.42);
System.out.println(4.015 * 100);
System.out.println(123.3 / 100);
}
}
运行结果:
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
double类型运算依然出现精度问题,所以代码中需要用到BigDecimalUtil类的一系列运算方法
加法:BigDecimalUtil.add(double v1, double v2)
减法:BigDecimalUtil.subtract(double v1, double v2)
乘法:BigDecimalUtil.multiply(double v1, double v2)
除法:BigDecimalUtil.divide(double v1, double v2)
如何将 String 转换成 char ?
char[] ca="123".toCharArray();
如何将char转换成String?
String s=ca.toString(); //任何类型都可以采用toString()转换成String类型
//-----------------日期-------------------------
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DATE);
获取今天的日期字符串
String today=java.text.DateFormat.getDateInstance().format(new java.util.Date());
获取今天的日期
new java.sql.Date(System.currentTimeMillis())
java.lang.annotation
http://yikaicheng-happy.iteye.com/blog/226674
sptring 注解入门
http://www.iteye.com/topic/295348
Annotation 入门教程
http://hi.baidu.com/banseon/blog/item/13a53df4f4d95169ddc474b7.html
SayHello.java
package com.javaeye.lindows.test; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author Lindows * * @ElementType.TYPE 类、接口(包括注释类型)或枚举声明 * * @RetentionPolicy.RUNTIME 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。 * * @documented 指示某一类型的注释将通过 javadoc 和类似的默认工具进行文档化。 * 应使用此类型来注释这些类型的声明:其注释会影响由其客户端注释的元素的使用。 * 如果类型声明是用 Documented来注释的,则其注释将成为注释元素的公共 API 的一部分。 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented() public @interface SayHello { String value() default "HELLO WORLD"; }
HelloWorld.java
package com.javaeye.lindows.test; @SayHello() public class HelloWorld { public void show() { System.out.println("+++++++++++++++++"); } }
TestHelloWorld.java
package com.javaeye.lindows.test; public class TestHelloWorld { public static void main(String[] args) throws ClassNotFoundException { @SuppressWarnings("unchecked") Class clazz = Class.forName("com.javaeye.lindows.test.HelloWorld"); boolean bool = clazz.isAnnotationPresent(SayHello.class); // clazz.isAnnotationPresent // 如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。此方法主要是为了便于访问标记注释而设计的。 if (bool) { SayHello hello = (SayHello) clazz.getAnnotation(SayHello.class); // getAnnotation 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。 System.out.println("打招呼"); System.out.println(hello.value()); System.out.println("完毕"); } } }
MyException 自定义异常
SimpleException.java
package com.javaeye.lindows.lang; //Thinking in java4 P251 public class SimpleException extends Exception { private static final long serialVersionUID = 3958279279547826523L; public void sVoid() { System.out.println("this is SimpleException"); } }
InheritException.java
package com.javaeye.lindows.lang; public class InheritException { public void iVoid() throws SimpleException { throw new SimpleException(); } public static void main(String[] args) { InheritException iException = new InheritException(); try { iException.iVoid(); } catch (SimpleException e) { e.sVoid(); } } }
end
相关推荐
解决mongo数据插入时 报错问题 mogodb插入数据时报错Can't find a codec for class java.math.BigDecimal
当 BigDecimal 值大于 Integer.MAX_VALUE 时,intValue() 方法将抛出 java.lang.ArithmeticException 异常。例如,在上面的示例代码中,若输入的 BigDecimal 值为 2147483648 时,intValue() 方法将抛出“Out of ...
* @return java.lang.String * @author xm * @create 2018/6/7 12:03 **/ public static String format2point(Number value) { return df.format(value); } public static Double add(Number value1, ...
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
BigDecimal.class,BigDecimal[].class, BigInteger.class,BigInteger[].class, Date.class,Date[].class 通过对以上基本元数据类型的模拟实现,本框架可以轻松模拟由以上元数据结构组成的BEAN、LIST、SET、...
amp-java AMP(异步消息协议)的 Java 实现,包括一些扭曲的 Python 功能,如React器和延迟。 有关 AMP 的更多信息,请... AMP 十进制 = java.math.BigDecimal AMP 日期时间 = java.util.Calendar AMP ListOf = java
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
Error:(16, 39) java: no suitable method found for convert(java.lang.Class,java.lang.String,java.math.BigDecimal) method InvalidType.<ENTITY>convert(java.lang.Class,java.lang.String,java.lang.Object)...
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio 定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 java.nio....
rules.put("java.math.BigDecimal", "DECIMAL"); rules.put("java.util.Date", "TIMESTAMP"); 在SqlMapAutoGen类的构造函数中,可以自行修改 3)预先定义了模板 temple.xml (请放在D:/test 目录下) temple.xml定义了...