java中string与其他类型之间的互相转换
1.将Int,Float,Double,Long转换为String
String s = ""+i;
String s = String.valueOf(i);
String s = Integer.toString(i);
第一种方法:s = ""+i; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
第三种方法:效率最高?
2.将String转换为Int,Float,Double,Long
int i = Integer.parseInt(s);
int i = Integer.valueOf(s).intValue();
第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
3.将Date转换为String
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String s = format.format(date);
4.将String转换为Date
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(s);
------------------------------------------------------------------------------------------------------------
public class TypeChange {
public TypeChange() {
}
//change the string type to the int type
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
//change int type to the string type
public static String intToString(int value)
{
Integer integer = new Integer(value);
return integer.toString();
}
//change the string type to the float type
public static float stringToFloat(String floatstr)
{
Float floatee;
floatee = Float.valueOf(floatstr);
return floatee.floatValue();
}
//change the float type to the string type
public
相关文档:
java去除字符串中的空格、回车、换行符、制表符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
public static void replaceBlank()
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
String str="I am a, I am Hello ok, \n new line ffdsa!";
System.out.p ......
通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化。一般有两种方案:即优化代码或更改设计方法。我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性能。而一个设计良好的程序能够精简代码,从而提高性能。
下面将提供一些在JAVA程序的设计和编码中,为了能够 ......
------------------------------------------------------------------------------------------------
这几天主要是狂看源程序,在弥补了一些以前知识空白的同时,也学会了不少新的知识(比如 NIO),或者称为新技术吧。
线程池就是其中之一,一提到线程,我们会想到以前《操作系统》的生产者与消费者,信号量,同步控制 ......
学java也将近快两年的时间了,之前学过的东西自己感觉有点模糊,理论掌握的不是很透彻,有些问题解决的也不是很全面,为此在大学毕业前夕,想把知识好好的梳理一下,把自己对技术的疑点和一些研究心得写到csdn博客上。 ......
1. 如何得到Java应用程序的可用内存?
答:如下代码实现取得总的内存大小和可用内存大小,并打印到控制台上
public class MemoryExp {
public static void main(String[] args) {
System.out.println("Total Memory"+Runtime.getRuntime().totalMemory());
System.out.println("Free Memory ......