Monday, July 9, 2007

Print any text without using semicolon in the program

Here is the question.

Print any text without using semicolon in your program.

In C/C++, it is wasy to answer, you will say:

int main(){
if (printf("Printed the text without using semicolon")){}
}


But in Java, you cannot do directly like that because System.out.println() returns nothing and the println()'s return type is void.

So, how will you do it in Java?
We can do the same in Java using Reflection classes(java.lang.reflect)

class semicolon{
public static void main(String args[]){
try{
if (System.out.getClass().getDeclaredMethod("println", Class.forName("java.lang.String")).invoke(System.out, "This text is printed by a program without a semicolon") == null){}
}catch(Exception e){
}
}
}


Just try it out yourselves.

43 comments:

GayanNR said...

class semicolon
{
public static void main(String args[])
{
if(System.out.printf("hellow world")==System.out.printf(""))
{
//nothing in here

}
}
}

Antony Vincent Pandian.S. said...

Hi Gayannr,
Your answer is correct when the java version is greater than 1.5

The printf method is not present in versions before 1.5

ElroyTLanphear said...

ut聊天室色情小說美女影片情色貼圖ut聊天室85cc成人片聊天室ut一葉情貼圖片區成人圖片0204貼圖區八國聯軍成人交友戀愛小站咆哮成人繁體080視訊聊天室0204貼圖區成人視訊85cc成人片觀看av情色交友網一葉晴貼圖片區

投癢癢 said...

If you can not be kind, at least have the decency to be vague. ....................................................

cool said...

秋去冬來~~大家要注意身體,別感冒囉!........................................

曉豪 said...

如此動感的blog!!!..................................................

雅惠 said...

謝謝分享好文章 .........................................

郭君 said...

淫美成人論壇淫蕩女孩自拍淫蕩人妻,日本美女短片免費觀看淫娃免費視訊淫娃免費視訊聊天室淫淫美女視訊交友一網情深聊天室一葉情貼影片區一葉晴語音聊天網av127上班族酒趴網影音視訊fm358視訊美女聊天室免費性影片成人短片sexaa免費看影片拓網交友高雄援交妹彩虹視訊交友網免費聊天66k正妹視訊elove交友520sex視訊影音交友aio 交友愛情館台北美女視訊新竹援交自拍av短片-免費a片一夜情台中援交妹視訊go2av成人聊天室情人視訊網免費視訊美女自拍線上av免費影片1007視訊a片免費看視訊交友ing168 視訊聊天室一對一 show666vl netbbs論壇本土自拍免費視訊聊天 msn亞洲限制級成人影片區辣妹視訊自拍美女聊天室

明宏明宏 said...

cool blog,期待更新........................................

TammySteffes2217 said...

I like your blog.

BlancaMcleroy1230 said...

sex女,ut,貼影,av,高潮,女優,做愛,手機成人影片,色遊戲,成人動漫,百分百貼圖區,85cc影片,成人影音,av色情影片,A片線上,a片,sex,777,三級線上看,美女的照片,視訊做愛,78論壇,打飛機,免費視訊,成人影院,辣妹視訊,視訊聊天,影片網,kiss911,a片,交友,聊天,做愛,免費影片,性交,線上成人,網路色情,聊天,美女自慰,免費a分享,免費短片,裸照,一夜情,女優,85cc成人片,美女寫真,偷拍a,情人視訊聊天室

BokHaile8854 said...

如果你不思考未來,你便不會有未來........................................

Ashley凱倫 said...

想要推動天下,先要發動自己。.............................................

WilmerD_Sho玉鳳 said...

上來打聲招呼,祝你一切平安!.............................................

佩璇 said...

偶爾上來逛逛,下次不知是否還有緣再進來,先祝您平安順利!!! ........................................

韋成 said...

人是受想像力所支配的。......................................................

嘉剛 said...

良言一句三冬暖,惡語傷人六月寒。..................................................

歐英傑 said...

生命的意義,是在於活的充實;而不是在於活得長久。 ..................................................

韻枝 said...

you have a good taste!............................................................

季玉 said...

婚姻對男人來說是賭他的自由,對女人而言卻是賭她的幸福。..................................................................

張瑋劭 said...

一棵樹除非在春天開了花,否則難望在秋天結果。....................................................................

銘木 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

家賢 said...

成熟,就是有能力適應生活中的模糊。.................................................................

ToryO_Viss鈺雯er0316 said...

快樂,是享受工作過程的結果............................................................

JasonBirk佳琪 said...

唯有穿鞋的人,才知道鞋的哪一處擠腳............................................................

PearleY建佑 said...

來打聲招呼-大家好!!!..................................................................

吳婷婷 said...

A friend in need is a friend indeed...................................................................

JuanHer珮均 said...

人並不是生來要吃敗仗的。人可被毀滅,但不可被擊倒。.................................................................

江美娟江美娟 said...

.有價值的東西,都要付出代價。-................................................

姿柯瑩柯dgdd憶曾g智曾 said...

很喜歡你的部落格,來給你加油,幫你推一下喔~期待你的下一個更新,謝謝............................................................

洪瑋婷洪瑋婷 said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。..................................................

RicoLisi0802志竹 said...

感謝您費心的分享............................................................

懿馮綺懿馮綺懿馮綺懿馮綺懿馮綺 said...

好的blog需要我們一起努力!............................................................

建依萍彰 said...

愛情是一種發明,需要不斷改良。只是,這種發明和其他發明不一樣,它沒有專利權,隨時會被人搶走。.................................................................

GusF_Finkbeiner1蕙帆 said...

所有的資產,在不被諒解時,都成了負債..................................................................

亦奈美妮 said...

All roads lead to Rome. 堅持自己所選!............................................................

健林林豪 said...

一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤..................................................

怡屏 said...

如此活躍的文字, 妳好棒哦! ..................................................................

于庭吳 said...

人生有些波折,才能有些成長,所以不論順逆,凡是成長、成功的助緣,都應該心存感激。.................................................

木詹詹詹詹詹行詹詹 said...

Poverty tries friends...................................................................

佳張張張張燕張張張張張 said...

一棵樹除非在春天開了花,否則難望在秋天結果。............................................................

麗王王珠 said...

你的部落格很棒,我期待更新喔............................................................

建枫 said...

初次拜訪,祝你人氣一百分