Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Novice Newsgroups: comp.lang.java.programmer Subject: Re: Curious compiler warning Date: Wed, 11 Jan 2012 04:55:00 +0000 (UTC) Organization: Your Company Lines: 83 Message-ID: References: NNTP-Posting-Host: 5PPmjkvSSHvzZTySRz75XA.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: Xnews/5.04.25 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 120110-1, 2012-01-10), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11214 Lew wrote in news:jeir2s$7v0$3@news.albasani.net: > On 01/10/2012 05:51 PM, Novice wrote: >> I'm getting an odd compiler warning that I don't really understand. I >> wonder if anyone can enlighten me on the meaning of this message. >> Basically, I'm not sure what the compiler's problem is with what I'm >> doing or the best way to make it happy. >> >> I've got a fairly simply method name foo() that takes two parameters, >> ints called start and finish. During this method, I decrement start >> and finish and then calls another method, bar(), passing the >> decremented versions of start and finish. For some reason, the >> compiler objects to the statements where I decrement start and finish >> and says "the parameter should not be assigned". I'm running Eclipse >> 3.7.1 with a 1.6.18 JDK. >> >> So here's what the code looks like: >> >> public static int foo(int start, int finish) { >> >> /* Other stuff */ >> >> start--; >> finish--; >> >> bar(start, finish); >> } >> >> It's the start--; and finish--; lines that are raising the warnings. >> >> What exactly is wrong with doing that? And what is the best way to >> make the compiler happy about that code? > > What's exactly wrong is that the assignment to the parameters is > thrown away, and does you no good. > How so? My original code says - in my opinion - "thank you very much for the value but we need to adjust it a little by subtracting one, aside from that, it's perfect". It's not like we're changing start to an arbitrary value like 0 or 5 billion regardless of the original value. The code in this class basically just does String functions to count the number of occurrences of a string in another string but I'm assuming that the user will consider the initial character of the string being searched as '1' while Java's methods are all 0-based. I'm just subtracting one to bring the user's view of the situation in line with Java's view. I'm just wondering why the compiler is so offended by "start--".... >> I modified the code as follows to get rid of the warnings but is it >> really necessary to create two local variables just to eliminate the >> warnings? Or is there a better way? >> >> public static int foo(int start, int finish) { >> >> /* Other stuff */ >> >> int myStart = start; >> myStart--; >> >> int myFinish = finish; >> myFinish--; >> >> bar(myStart, myFinish); >> } > > Yes, there's a better way; that one is silly. > > public static int foo(int start, int finish) { > > /* Other stuff */ > > bar(start - 1, finish - 1); > } > Yes, I definitely like that better. Strangely enough, writing bar(start--, finish--); gives me the same warning as before, presumably for the same reason. -- Novice