Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #506

Re: Casts and special things of the Java language

From Merciadri Luca <merciadriluca@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Casts and special things of the Java language
Date 2011-04-03 09:01 -0700
Organization http://groups.google.com
Message-ID <4f3453aa-5824-4aa1-81f0-6bfabcff3c7b@hd10g2000vbb.googlegroups.com> (permalink)
References <874o6gqv2k.fsf@merciadriluca-station.MERCIADRILUCA> <lmtfp6d63kcrqortkn60q7maipr5cg36qn@4ax.com> <87tyefbsol.fsf@merciadriluca-station.MERCIADRILUCA>

Show all headers | View raw


On Apr 3, 11:16 am, Merciadri Luca <Luca.Mercia...@student.ulg.ac.be>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
> Roedy Green <see_webs...@mindprod.com.invalid> writes:
> > On Sat, 02 Apr 2011 22:04:03 +0200, Merciadri Luca
> > <Luca.Mercia...@student.ulg.ac.be> wrote, quoted or indirectly quoted
> > someone who said :
>
> >>I'm wondering what will be displayed on the console.
>
> > Seriously, what are you afraid will happen if you RUN the program?
>
> > Why would you trust strangers on the Internet to tell you more than
> > your own eyes?
>
> > Surely you have managed to get a HelloWorld compiled and executing by
> > now.  Your questions show you are well beyond the rank beginner stage.
>
> > The compiler is well debugged.  Unless you are writing a compiler and
> > need the equivalent of supreme court rulings on the meaning of the
> > JLS, experiments on how Javac.exe/java.exe behaves won't mislead you.
> > In any case, they are more accurate than off the hip answers you may
> > get here.
>
> > Further, the practical knowledge of how the compiler actually behaves
> > is what you need to write programs, not how it should behave.
>
> I know that when I run the bytecode, I get
>
> ==
> 1
> 1
> 0
> 1
> 1
> 0
> ==
> (which shows my guesses are correct).
>
> What I need to understand is WHY these values are displayed (and not
> WHAT is displayed). That is, I need to understand the intrinsical
> mechanism of this.
>
> For example, numbering System.out.println console outputs from 1 to 6,
> I can justify that
>
> * output 2 is 1 (because of the return method);
> * output 3 is 0 (because of the a.v = 0 instruction);
> * output 4 is 1 (because of the b.v = 1 instruction).
>
> However, I don't really understand outputs 1, 5 and 6. I would say:
>
> * output 1 is 1 because b.m() is called thanks to the A a = b
>   instruction (because a gets the methods of b, which is a B);
> * output 5 is 1 because casting b (of type B) to an A does not change
>   its (those of b) methods;
> * output 6 is 0 because casting b (of type B) to an A leads to the
>   following equality: ((A) b).v == a.v.
>
> I would appreciate to know if I'm right (I might detail my
> explanations if necessary), and, if not, I would like to learn what
> really happens in these instructions. (Do not hesitate to give me
> pointers.)
>
> Thanks.
>
> - --
> Merciadri Luca
> Seehttp://www.student.montefiore.ulg.ac.be/~merciadri/
> - --
>
> Effort is important, but knowing where to make an effort makes all
>   the difference!
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
>
> iEYEARECAAYFAk2YOwoACgkQM0LLzLt8MhwkAgCeMI4PmEE7Ljx5+FETXD/saTd/
> V84An1ja3tVs4sW9PJzMCnJii4H0XFQN
> =5goy
> -----END PGP SIGNATURE-----

After still some reflexion, I can reasonably deduce that outputs 1 and
5 are the same (in this case, 1), because casting A a to an A changes
nothing.

I just re-thought about the fact that in Java, variables are accessed
using `static links,' when an object methods are accessed using a
`dynamic link.' As a result,
* output 1 is 1 because a becomes b,
* outputs 2, 3 and 4 are trivial according to the four affectations,
* output 5: ((A) b).m() is equivalent to b.m(), that is, output 2,
which is 1,
* output 6: ((A) b).v is not b.v, because variables are accessed using
static links.

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Casts and special things of the Java language Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-02 22:04 +0200
  Re: Casts and special things of the Java language Lew <noone@lewscanon.com> - 2011-04-02 17:22 -0400
  Re: Casts and special things of the Java language Roedy Green <see_website@mindprod.com.invalid> - 2011-04-02 21:31 -0700
    Re: Casts and special things of the Java language Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 11:16 +0200
      Re: Casts and special things of the Java language Merciadri Luca <merciadriluca@gmail.com> - 2011-04-03 09:01 -0700
        Re: Casts and special things of the Java language markspace <-@.> - 2011-04-03 10:53 -0700
          Re: Casts and special things of the Java language Merciadri Luca <merciadriluca@gmail.com> - 2011-04-03 11:28 -0700
            Re: Casts and special things of the Java language markspace <-@.> - 2011-04-03 13:35 -0700
              Re: Casts and special things of the Java language Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 22:58 +0200
            Re: Casts and special things of the Java language Lew <noone@lewscanon.com> - 2011-04-06 12:32 -0400

csiph-web