aspose file tools*
The moose likes Java in General and the fly likes Return Value Not Captured But Still Runs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Return Value Not Captured But Still Runs" Watch "Return Value Not Captured But Still Runs" New topic
Author

Return Value Not Captured But Still Runs

James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
String str = "A";
str.toLowerCase();
When I compile the above, no warnings/errors given! What's the reason for that as the second line is actually returning a String and its not captured.

Thanks in advance.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Errr... that's because you didn't capture the return value. Try:
String str2 = str.toLowerCase();


"I'm not back." - Bill Harding, Twister
James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
Perhaps I'm not that clear in my 1st posting but actually what I want to know is whether there is any specific reasons as to why Java allow that to happen.
s1.toLowerCase();
The statement above doesn't really has any effect unless we store the return value in a variable or print it out. And btw, where the return value
goes for the statement above (aren't we expecting a value from that)?

Thanks.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Java doesn't care whether or not you use the return value from a method. This is a dubious legacy from C.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Perl also allows this to happen... I suppose it has it's uses in some cases, but I can't think of any good ones.
PCS


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Sometimes you *want* to ignore the return value.
For example, StringBuffer.append is returning the StringBuffer itself, so that you can write statements like:
strBuff.append(x).append(y).append(z);
If the compiler would force you to somehow use the return value, you had to write
strBuff = strBuff.append(x).append(y).append(z);


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
From the compiler's point of view, it can't tell the difference between methods like String's toUpperCase() and StringBuffer's append(). It would take a very sophisticated compiler to identify those methods which had no other side effects and which therefore should only be used by capturing a return value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return Value Not Captured But Still Runs