Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #17427 > unrolled thread
| Started by | "dkoleary" <dkoleary@1:261/38.remove-qhs-this> |
|---|---|
| First post | 2012-08-08 19:04 +0000 |
| Last post | 2012-08-10 18:39 +0000 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
why does this work? "dkoleary" <dkoleary@1:261/38.remove-qhs-this> - 2012-08-08 19:04 +0000
Re: why does this work? "Daniel Pitts" <daniel.pitts@1:261/38.remove-qhs-this> - 2012-08-08 19:04 +0000
Re: why does this work? "dkoleary" <dkoleary@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
Re: why does this work? "Lew" <lew@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
Re: why does this work? "Eric Sosman" <eric.sosman@1:261/38.remove-r72-this> - 2012-08-08 20:06 +0000
Re: why does this work? "Roedy Green" <roedy.green@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
Re: why does this work? "glen herrmannsfeldt" <glen.herrmannsfeldt@1:261/38.remove-t9h-this> - 2012-08-10 18:39 +0000
| From | "dkoleary" <dkoleary@1:261/38.remove-qhs-this> |
|---|---|
| Date | 2012-08-08 19:04 +0000 |
| Subject | why does this work? |
| Message-ID | <5022AB86.56377.calajapr@time.synchro.net> |
From: dkoleary <dkoleary@olearycomputers.com>
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?
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.
Can someone provide the missing concept?
Thanks.
Doug O'Leary
--- 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
[toc] | [next] | [standalone]
| From | "Daniel Pitts" <daniel.pitts@1:261/38.remove-qhs-this> |
|---|---|
| Date | 2012-08-08 19:04 +0000 |
| Message-ID | <5022AB86.56379.calajapr@time.synchro.net> |
| In reply to | #17427 |
To: dkoleary
From: Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
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
[toc] | [prev] | [next] | [standalone]
| From | "dkoleary" <dkoleary@1:261/38.remove-k2r-this> |
|---|---|
| Date | 2012-08-09 18:44 +0000 |
| Message-ID | <5023FE2F.56415.calajapr@time.synchro.net> |
| In reply to | #17429 |
To: Daniel Pitts From: dkoleary <dkoleary@olearycomputers.com> On Wednesday, August 8, 2012 12:38:27 PM UTC-5, Daniel Pitts wrote: > 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. > Got it... I do remember reading that somewhere else. main only gets executed once. Thanks for the concept correction. Hopefully, it'll stick now :) Doug O'Leary --- 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
[toc] | [prev] | [next] | [standalone]
| From | "Lew" <lew@1:261/38.remove-k2r-this> |
|---|---|
| Date | 2012-08-09 18:44 +0000 |
| Message-ID | <5023FE3D.56491.calajapr@time.synchro.net> |
| In reply to | #17466 |
To: dkoleary From: Lew <lewbloch@gmail.com> dkoleary wrote: > Got it... I do remember reading that somewhere else. main only gets executed once. That isn't strictly true. The 'main()' method is only called for one class one time by the JVM when it starts, but nothing prevents code in the program from explicitly calling some class's 'main()' at some later time. > Thanks for the concept correction. Hopefully, it'll stick now :) Read the Java tutorials. They explain constructors vs. methods and such concepts. -- Lew --- 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
[toc] | [prev] | [next] | [standalone]
| From | "Eric Sosman" <eric.sosman@1:261/38.remove-r72-this> |
|---|---|
| Date | 2012-08-08 20:06 +0000 |
| Message-ID | <5022BA8F.56386.calajapr@time.synchro.net> |
| In reply to | #17427 |
To: dkoleary
From: Eric Sosman <esosman@ieee-dot-org.invalid>
On 8/8/2012 1:30 PM, dkoleary wrote:
> [...]
> How come that isn't recursive? XCopy.main() instantiates a new XCopy.
Shouldn't that new XCopy instance also instantiate a new XCopy?
See Daniel Pitts' explanation. Another thing you could try to
help you see what's going on is to sprinkle some more println() calls through
the code to help trace through the execution. In this case you're interested
in where the constructor fits with relation to everything else, but there's no
explicit constructor written in the code. As I'm sure you've learned, this
means the compiler will write a simple constructor for you -- but there's no
way to get the compiler to stick println() calls in what it writes, so your
recourse is to write your own explicit constructor. With this in mind, the code
might look like:
class XCopy {
public static void main(String[] args) {
System.out.println("entering main()");
int orig = 42;
System.out.println("main() creates an XCopy");
XCopy x = new XCopy();
System.out.println("main() created an XCopy");
int y = x.go(orig);
System.out.println(orig + " " + y);
System.out.println("main() is finished");
}
int go(int arg) {
System.out.println("executing go(), arg = " + arg);
return arg * 2;
}
// Explicit constructor, just for the println
XCopy() {
System.out.println("constructing an XCopy");
}
}
Run this version, study the output, and see if the sequence of events becomes
clearer.
This technique is sometimes called "printf debugging" (the name
comes from a different programming language). Despite its simplicity, it can
be astonishingly effective, and the overall approach can be used in most
environments and most languages. Indeed, Java's various logging frameworks
(you may learn about them later) are basically just fancied-up versions of
printf debugging: A piece of the program blurts "Look! I'm *here*, and these
are a few interesting values."
--
Eric Sosman
esosman@ieee-dot-org.invalid
--- 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
[toc] | [prev] | [next] | [standalone]
| From | "Roedy Green" <roedy.green@1:261/38.remove-k2r-this> |
|---|---|
| Date | 2012-08-09 18:44 +0000 |
| Message-ID | <5023FE3F.56500.calajapr@time.synchro.net> |
| In reply to | #17427 |
To: dkoleary From: Roedy Green <see_website@mindprod.com.invalid> On Wed, 8 Aug 2012 10:30:02 -0700 (PDT), dkoleary <dkoleary@olearycomputers.com> wrote, quoted or indirectly quoted someone who said : >How come that isn't recursive? new allocates some space and XCopy() runs the initialitzer code in the constructor. There is no new in the constructor, so why would it be recursive? There is no more reason for the constructor to be recursive than any other method. The constructor does not reinvoke the constructor. -- Roedy Green Canadian Mind Products http://mindprod.com A new scientific truth does not triumph by convincing its opponents and making them see the light, but rather because its opponents eventually die, and a new generation grows up that is familiar with it. ~ Max Planck 1858-04-23 1947-10-04 --- 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
[toc] | [prev] | [next] | [standalone]
| From | "glen herrmannsfeldt" <glen.herrmannsfeldt@1:261/38.remove-t9h-this> |
|---|---|
| Date | 2012-08-10 18:39 +0000 |
| Message-ID | <50254C51.56580.calajapr@time.synchro.net> |
| In reply to | #17551 |
To: Roedy Green From: glen herrmannsfeldt <gah@ugcs.caltech.edu> Roedy Green <see_website@mindprod.com.invalid> wrote: (snip) > There is no new in the constructor, so why would it be recursive? > There is no more reason for the constructor to be recursive than any > other method. The constructor does not reinvoke the constructor. But if there IS a new in the constructor... -- glen --- 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web