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
相关文档:
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接。程序可以通过URLConnection实例向该URL发送请求、读取URL引用的资源。
通常创建一个和 URL 的连接,并发送请求、读取此 URL 引用的资源需要如下几个步骤:
(1)通过调用URL对象openConnection()方法来创建URLConnectio ......
Java面试中,最常被人问到的几个问题:
1. java.util.*包的UML结构图。
2. Vector和ArrayList、LinkedList区别 Hashtable 和 HashMap之间的区别
3. String、StringBuffer,StringBuilder之间区别。
--回答--
1.
Collection
|
|_List
| |_LinkedList
| | ......
上一篇文章中介绍了怎样利用MSNP登录MSN,在登录后便可取联系人了,很简单。
参考文章:
http://msnpiki.msnfanatic.com/index.php/MSNP13:Contact_List
http://msnpiki.msnfanatic.com/index.php/MSNP15:TicketTokens
在登录后,可以得到一个contacts.msn.com的ticketToken,这可用于后续的验证
一种方法是加入一个MS ......
Java连接mysql数据库,代码经过运行准确无误。
下面为实例---->
用数据库操纵工具(例:SQLyogEnt)操纵mysql建表,或dos下建,如下:
数据库名:scutcs
表名:student
表内容:
sno char[7] NO NULL Primary Key;
sname varchar[8] NO NULL;
sex char[2] NO NULL; ......
1. 如何得到Java应用程序的可用内存?
答:如下代码实现取得总的内存大小和可用内存大小,并打印到控制台上
public class MemoryExp {
public static void main(String[] args) {
System.out.println("Total Memory"+Runtime.getRuntime().totalMemory());
System.out.println("Free Memory ......