Java SE学习_Null与""区别
Null 是没有的意思 不占用内存空间
""是空的字符串 它是一个字符串,不过里面没有内容
举例:
""是有一个包装袋没装东西
Null是连包装袋都没有
实例:
String str1 = null; str引用为空
String str2 = ""; str应用一个空串
也就是null没有分配空间,""分配了空间,因此str1还不是一个实例化的对象,儿str2已经实例化。
注意因为null不是对象,""是对象。所以比较的时候必须是
if(str1 == null){.
。。。} 或者
if(str2.equals("")){
}
对象用equals比较,null用等号比较。因此,如果str1=null;下面的写法错误:
if(str1.equals("")||str1==null){
//如果str1没有值,则....
//。。。
}
正确的写法是
if(str1==null||str1.equals("")){
//先判断是不是对象,如果是,再判断是不是空字符串
//。。。
}
相关文档:
result love(boy, girl)
{
if( boy.有房() and boy.有车() )
{
boy.set(nothing);
return girl.嫁给(boy);
&n ......
转 : http://wintys.blog.51cto.com/425414/94051
/**
*名称:BinarySearch
*功能:实现了折半查找(二分查找)的递归和非递归算法.
*说明:
* 1、要求所查找的数组已有序,并且其中元素已实现Comparable<T>接口,如Integer、String等.
* 2、非递归查找使用search( ......
static String string2Unicode(String s) {
try {
StringBuffer out = new StringBuffer("");
byte[] bytes = s.getBytes("unicode");
for (int i = 2; i < bytes.length - 1; i += 2) {
out.append("u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
f ......
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
1>Collection的用法:
import java.util.*;
public class List1
{
public static void main(String []args)
{
String a="A";
String b="B";
String c="C";
Collection<String>list = new ArrayList<String>();
list.add ......