Under Southern Skies

迎接新变化

Java语言String的解惑

公司要求转换到JAVA平台上来,所以开始研究JAVA语言。最近在看到String时,发现有很多不明白的地方。
问题1:String str2 = str1这种语法的使用,在书写明确写明了是引用的传递。那么为什么我在修改Str2的值时,Str1的内容没有跟着一起变呢?而StringBuffer就没有这些问题。

public class Test
{
    public static void main(String[] args)
    {
        String str1 = "12345";
        //两者指向相同的地址
        String str2 = str1;
        //两者相等
        System.out.println("str1 == str2 : " + (str1 == str2));
        //给str2重新赋值
        str2 = "4567";
        //为什么str1的值没有变
        System.out.println("str1 = " + str1);
        System.out.println("str2 = " + str2);
    }
}

后在无数次尝试后得到的结论是:对于String类型,赋值的意思应该是,赋值语句右边的字符串生成一个新串,并把这个新串的引用传递给赋值语句左边String变量。所以变更str2的值后,实际上str2已不再指向str1。
网上的答案与我的猜测类似:string类型是不可改变的,也就是说,当你想改变一个string对象的时候,比如name= “madding ”
那么虚拟机不会改变原来的对象,而是生成一个新的string对象,然后让name去指向它,如果原来的那个 “tom “没有任何对象去引用它,虚拟机的垃圾回收机制将接收它。

问题2:String str1 = “123456″;和String str1 = new String(“123456″)的区别在哪里?
尝试后的结论是:不用new的话,会把字符串放入java的pool中缓存起来,下一次如果有另一个变量用到这个字符串时,会直接把这个字符串的引用给它。而new的是直接生成一个新串,不会检查pool中的缓存。测试的示例如下:

public class Test
{
    public static void main(String[] args)
    {
        String str1 = "12345";
        //两者指向相同的地址
        String str2 = "12345";
        //两者相等
        System.out.println("str1 == str2 : " + (str1 == str2));
        //str3使用new方式生成。
        String str3 = new String("12345");
        //str3不会与str2相等
        System.out.println("str3 == str2 : " + (str3 == str2));
        str3 = "12345";
        //str3与str2和str1相等了
        System.out.println("str3 == str1 : " + (str3 == str1));
        System.out.println("str3 == str2 : " + (str3 == str2));
    }
}

歌曲推荐环节:
《nothing else matters》,来自于Metallica在1991年发行的同名黑色封面专辑《Metallica》。曲子旋律至柔至美,是《Metallica》中唯一的情歌,适合夜深人静时,一个人静静地听。当然心情不要太坏,不然听了太忧郁。此曲创作的背景是:metallica极具天赋的贝司手cliff burton技艺非凡,是乐队的核心人物。1986年他在车祸中丧生,《nothing else matters》就是为纪念他而作,让这支很man的乐队多了份柔情,多了份伤感。优美的前奏,演唱,和声,干净的间奏,JAMES弹的SOLO,整曲中各个乐器的相互配合…不知道谁喜欢摇滚,如果你喜欢摇滚的话,就不得不听听METALLICA的《nothing else matters》(什么都无所谓)。不得不说说这个乐队,听METALLICA的歌曲,会发现他们的主音吉他非常的厉害,几乎每首歌曲中的大段吉他SOLO都具有非常的难度且极为耐听,最为难得的是主唱James Hetfield的超绝嗓音和唱功,James Hetfield的嗓音高亢时如钛金一般铿锵,却也有其柔和的一面,听听这首歌吧。你会喜欢的。(百度百科


So close, no matter how far 如此靠近,不管有多远
Couldn’t be much more from the heart 不可能比心更遥远
Forever trusting who we are 永远相信我们自己
And nothing else matters 一切都无关紧要
Never opened myself this way 从来不曾如此敞开胸怀
Life is ours, we live it our way 人生属於我们,我们过着自己的生活
All these words I don’t just say 这些话我不会说出口
And nothing else matters 一切都无关紧要
Trust I seek and I find in you 我信任我寻觅到的你
Every day for us something new 对我俩而言,每天都是新的开始
Open mind for a different view 敞开心门,接收不同的视野
And nothing else matters 一切都无关紧要
Never cared for what they do 绝不在乎别人做了什麽
Never cared for what they know 绝不在乎别人知道什麽
But I know 但我明白
So close, no matter how far 如此靠近,不管有多远
Couldn’t be much more from the heart 不可能比心更遥远
Forever trusting who we are 永远相信我们自己
And nothing else matters 一切都无关紧要
Never cared for what they do 绝不在乎别人做了什麽
Never cared for what they know 绝不在乎别人知道什麽
But I know 但我明白
Never opened myself this way 从来不曾如此敞开胸怀
Life is ours, we live it our way 人生属於我们,我们过着自己的生活
All these words I don’t just say 这些话我不会说出口
And nothing else matters 一切都无关紧要
Trust I seek and I find in you 我信任我寻觅到的你
Every day for us, something new 对我俩而言,每天都是新的开始
Open mind for a different view 敞开心门,接收不同的视野
And nothing else matters 一切都无关紧要
Never cared for what they say绝不在乎别人说了什么
Never cared for games they play绝不在乎别人玩什么游戏
Never cared for what they do 绝不在乎别人做了什麽
Never cared for what they know 绝不在乎别人知道什麽
But I know 但我明白
So close, no matter how far 如此靠近,不管有多远
Couldn’t be much more from the heart 不可能比心更遥远
Forever trusting who we are 永远相信我们自己
No, nothing else matters 一切都无关紧要

IXMLDocument的使用

  最近要和其它厂商的系统互相交换数据。提到要通过XML实现。花了点时间学习了一下,把一些要点记下来,免得浪费今天的劳动成果~有些函数还是没搞明白,但实现目前的需求应该是没问题了。在此感谢令狐虫和猛禽提供滴无私帮助。

1、IXMLDocument包含哪个头文件?
#include<XMLDoc.hpp>

2、如何创建一个XML对象?

//生成一个XMLDocument对象。
//对象生成后不用自己释放。

_di_IXMLDocument X = NewXMLDocument();
AnsiString rootpath = “c:\\Temp”;
X->Active = true;

//添加一个NodeName叫”dir”

_di_IXMLNode ix = X->AddChild(“dir”);
//添加Node下的一字Attributes为”id”
//Value为”1001″
ix->Attributes["id"] = “1001″;
ix->Attributes["name"] = “file1.txt”;
ix->Attributes["path"] = “c:\\temp”;
//保存为XML文件。
X->SaveToFile(“c:\\temp\\files.xml”);

3、如何创建树状节结?
//在上面代码的基础上添加。
//在Node下面添加一个子节点
_di_IXMLNode ix = node->AddChild(“file”);
ix->Attributes["id"] = “id”;
ix->Attributes["name"] = name;
ix->Attributes["path"] = Path;
ix->Attributes["type"] = “type”;

4、如何读取XML?
//取得XML对象
// 同样不用管释放
_di_IXMLDocument xml =LoadXMLDocument(“c:\\temp\\files.xml”);
//取得XML中的元素。
//这个root是啥我也不知道。
//我理解为root是整个XML的根
_di_IXMLNode root = xml->GetDocumentElement();
//得到root的子节点
_di_IXMLNodeList nodelist = root->ChildNodes;
//_di_IXMLNodeList有一个Count属性
for(int i = 0; i < nodelist->Count; i ++)
{
 AnsiString str;
 //遍历Node
 _di_IXMLNode node = nodelist->Nodes[i];
 //从属性中取得Attribute
 WideString wpath = node->GetAttribute(WideString(“path”));
 WideString wfile = node->GetAttribute(WideString(“name”));
 str = wpath + “\\” + wfile;
 Memo1->Lines->Add(str);
 //下面这段一样
 //只是演试一下如何得到一下层

 _di_IXMLNodeList childlist = node->GetChildNodes();

 for(int j = 0; j <childlist->Count; j++)
 {
  AnsiString str;
  _di_IXMLNode node = childlist->Nodes[j];
  WideString wpath = node->GetAttribute(WideString(“path”));
  WideString wfile = node->GetAttribute(WideString(“name”));

  str = ” “ + wpath + “\\” + wfile;
  Memo1->Lines->Add(str);
 }
}

5、其它有用的方法
_di_IXMLNode有一个NodeTpye表明这个Node的类型。但不知道在何时使用它。
_di_IXMLNode有一个NodeValue,我猜是用来表明这个NodeValue的类型。但不知道在何时使用它。也不知道怎么用,总是报错。

附一个例子:
TForm1 *Form1;

void AddPath(AnsiString Path, _di_IXMLNode node)
{
 AnsiString FilePath=Path+“\\*.*”;
 TSearchRec sr;
 sr.Name=FilePath;
 int done;
 done = FindFirst(FilePath,faAnyFile,sr);
 AnsiString FileName;
 while (!done) {
  FileName=Path+“\\”+sr.Name;
  FileSetAttr(FileName,0);
  if(sr.Attr & faDirectory&&sr.Name[1]!=‘.’) {
   _di_IXMLNode ix = node->AddChild(“dir”);
   ix->Attributes["id"] = “id”;
   ix->Attributes["name"] = sr.Name;
   ix->Attributes["path"] = Path;
   AddPath(FileName, ix);
  }
  else {
   _di_IXMLNode ix = node->AddChild(“file”);
   ix->Attributes["id"] = “id”;
   ix->Attributes["name"] = sr.Name;
   ix->Attributes["path"] = Path;
   ix->Attributes["type"] = “type”;
  }
  done = FindNext(sr);
 }
 FindClose(sr);
}
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 _di_IXMLDocument X = NewXMLDocument();
 AnsiString rootpath = “c:\\Temp”;
 X->Active = true;
 _di_IXMLNode ix = X->AddChild(“dir”);
 ix->Attributes["id"] = “”;
 ix->Attributes["name"] = ExtractFileName(rootpath);
 ix->Attributes["path"] = ExtractFilePath(rootpath);
 AddPath(rootpath, ix);
 X->SaveToFile(“c:\\temp\\files.xml”);
}
//—————————————————————————
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 _di_IXMLDocument xml =LoadXMLDocument(“c:\\temp\\files.xml”);
 _di_IXMLNode root = xml->GetDocumentElement();

 _di_IXMLNodeList nodelist = root->ChildNodes;
 for(int i = 0; i <nodelist->>Count; i++)
 {
  AnsiString str;
  _di_IXMLNode node = nodelist->Nodes[i];
  WideString wpath = node->GetAttribute(WideString(“path”));
  WideString wfile = node->GetAttribute(WideString(“name”));
  str = wpath + “\\” + wfile;
  Memo1->Lines->Add(str);
  _di_IXMLNodeList childlist = node->GetChildNodes();
  for(int j = 0; j <childlist->Count; j++)
  {
   AnsiString str;
   _di_IXMLNode node = childlist->Nodes[j];
   WideString wpath = node->GetAttribute(WideString(“path”));
   WideString wfile = node->GetAttribute(WideString(“name”));
   str = ” “ + wpath + “\\” + wfile;
   Memo1->Lines->Add(str);
  }
 }
}
//—————————————————————————

ADO异步执行

  看了猛禽令狐的贴子才知道原来google今天死掉乐,偶还以为就偶一个人登不了Gmail呢。现在放心乐,不是偶的RPWT。
  上次问了令狐关于数据库如何在做大数据量或执行很慢的操作时不会让界面死掉。记得令狐当时是说数据库本身就是支持这个功能的,然后就说了一堆以偶的IQ比较难以理解的话。好在在GOOGLE的帮助下还是找到了方法,和令狐说得也差不多,果然ADO就带有这个功能。只要设置ADOQuery或ADODataSet中的ExectueOption属性的eoAsyncFetchNonBlocking为true就可以乐。然后在OnFetchProgress和OnFetchComplete事件中作处理就行乐:)而在处理中想中止的话,只要简单的Close就行乐。

BTW 1:才说到偶和空姐失去联系了,没想到话刚说完的第二天就在网上遇到了她。看来考试后就没再回嘉兴,本来还真想跑回去见她一面,整整五年没见过她乐:(空姐现在对BLOG很感兴趣,AirHostess’ Blog看来就快要开张乐。

BTW 2:偶的Filckr也开张乐,可惜目前没有图片(至少没有公开的图片)。等有相机的时候请大家去参观吧。(Filckr + Blogspot果然是无敌的)

BCB疑难问题

今天在修改同事的代码时发生的怪问题,原来的Cut、Paste、Copy等功能都失灵了,断点后发现ActiveControl总是得到NULL值。(偶是把窗体嵌入到另一个窗体中)。最后还是猛禽解释了原因。

(2005-07-12 17:01:48) 猛禽 这个问题在ActiveControl上 (2005-07-12 17:02:17) 猛禽 当frm是独立窗体是,ActiveControl是它上面的控件 (2005-07-12 17:02:50) 猛禽 当你放到另一个窗体上时,这个子窗体本身变成了ActiveControl
(2005-07-12 17:03:14) 空中猎手 但这时通过断点得到的ActiveControl是空值
(2005-07-12 17:03:18) 猛禽 因为它是Form不是Memo,所以dynamic_cast会返回0
(2005-07-12 17:03:44) 空中猎手 在dynamic_cast操作之前就是NULL
(2005-07-12 17:04:08) 猛禽 那就是因为这个窗体本身变成control了,所以它就没有ActiveControl了 (2005-07-12 17:04:24) 猛禽 你必须取它的Parent的ActiveCtrol才可以

又学到一手,好久都没聊技术了,啥都不熟了:)

BTW:
  1、今天找到很多不错的诗,大家多多参观:《青春》、《索价》、《在非洲
  2、空姐生日快到乐,想送空姐一份生日礼物,但空姐就是不收,郁闷。现在不知道地址,没办法送乐,有钱用不掉指得多半就是现在这种情况@_@

设置打印端口终于解决鸟~~

网上找到一篇《Command line Printer Control in Windows 2000/XP》,里面介绍了通过命令控制打印机,测试也成功乐,这下不能再去理会SetPrinter了(虽然说得迎难而上,不过遇上这种RPWT我也没办法鸟)。

一条命令就解决我的问题,哈哈哈
RUNDLL32 PRINTUI.DLL,PrintUIEntry /Xs /n "printer" PortName "port:"

具体帮助可以在命令行下输入以下语句得到:
RUNDLL32 PRINTUI.DLL,PrintUIEntry /?

今天整理GMAIL时才发现我刚到上海时在网上发的找房消息竟然有人回复鸟,只是被GMAIL误认为是“垃圾邮件”给放到Spam里了,2室1厅1卫,离公司近到只用走路上班就行了,而且合住的还是位MM。现在到好了,啥都木有鸟。唉~~~怪偶过去一直没注意唉~~~