Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!204.153.247.121.MISMATCH!indigo.octanews.net!news-out.octanews.net!mauve.octanews.net!newspump.sol.net!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "Daniel Pitts" Subject: Re: why does this work? Message-ID: <5022AB86.56379.calajapr@time.synchro.net> X-Comment-To: dkoleary Newsgroups: comp.lang.java.programmer In-Reply-To: <5022AB86.56377.calajapr@time.synchro.net> References: <5022AB86.56377.calajapr@time.synchro.net> X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 c848e878 X-FTN-REPLY: 1:261/38 4be438ba Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 56 Date: Wed, 08 Aug 2012 19:04:28 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1344452668 69.21.70.65 (Wed, 08 Aug 2012 14:04:28 CDT) NNTP-Posting-Date: Wed, 08 Aug 2012 14:04:28 CDT Organization: tds.net X-Received-Bytes: 3155 Xref: csiph.com comp.lang.java.programmer:17429 To: dkoleary From: Daniel Pitts On 8/8/12 10:30 AM, dkoleary wrote: > Hi; > > New java programmer. So new, in fact, that I'm still working my way through the O'Reilly Head First Java book. One of the end of chapter questions involves identifying if a sample class will compile and what to do to make it compile if it won't. > > The sample class from chapter 4 is: > > class XCopy > { public static void main(String[] args) > { int orig = 42; > XCopy x = new XCopy(); > int y = x.go(orig); > System.out.println(orig + " " + y); > } > > int go(int arg) > { return arg * 2; } > } > > The book says that it'll compile and run, displaying "42 84" and, sure enough, it does: > > $ javac XCopy.java > $ java XCopy > 42 84 > > How come that isn't recursive? XCopy.main() instantiates a new XCopy. Shouldn't that new XCopy instance also instantiate a new XCopy? new XCopy() creates a new instance of the XCopy class, which executes Constructors. main isn't executed again because of this. main is only automatically executed by the JVM on start-up, once. > > I was figuring this would run until the XCopy.go function tried returning a number that wouldn't fit in int anymore... That's obviously not the case, but I don't know why. int can fit any number in the range [-2^31, 2^31). 42 and 84 are both within that range. It is possible that if you pass in a number with a large enough magnitude, you will end up with an overflow. In Java (and many 2s-compliment integer systems), overflow will simply throw-away the upper bits, and you will have what is called "wrap-around". This actually makes many of the basic operations easier, because signed numbers and unsigned numbers behave the same way. Hopefully this helps. --- BBBS/Li6 v4.10 Dada-1 * Origin: Prism bbs (1:261/38) --- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24