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


Groups > comp.lang.java.help > #474 > unrolled thread

Casts and special things of the Java language

Started byMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
First post2011-04-02 22:04 +0200
Last post2011-04-06 12:32 -0400
Articles 10 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  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

#474 — Casts and special things of the Java language

FromMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Date2011-04-02 22:04 +0200
SubjectCasts and special things of the Java language
Message-ID<874o6gqv2k.fsf@merciadriluca-station.MERCIADRILUCA>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Let us consider the following program.

==
class A
{
 public int v;
 public int m () { return 0; }
}

class B extends A
{
 public int v;
 public int m () { return 1; }
}

public class Test
{
 public static void main (String[] args)
 {
  B b = new B ();
  A a = b;
  a.v = 0;
  b.v = 1;
  System.out.println(a.m()); // ?
  System.out.println(b.m()); // 1
  System.out.println(a.v); // 0
  System.out.println(b.v); // 1
  System.out.prinln(((A) b).m()); // ?
  System.out.println(((A) b).v); // ?
 }
}
==

I'm wondering what will be displayed on the console.

I've commented what I expect to be printed, and wrote `?' for the
values that I cannot find.

Thanks.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Don't have too many irons in the fire.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2XgTMACgkQM0LLzLt8MhxqjwCfUBQonZd/qvZO2MSxO8AMR6eM
25cAn28sBAvn19MCQ9KIw4Uxjd3Hn3pR
=Uq8t
-----END PGP SIGNATURE-----

[toc] | [next] | [standalone]


#479

FromLew <noone@lewscanon.com>
Date2011-04-02 17:22 -0400
Message-ID<in8429$ekt$2@news.albasani.net>
In reply to#474
Merciadri Luca wrote:
> Let us consider the following program.
>
> ==
[blah, blah, blah]
> ==
>
> I'm wondering what will be displayed on the console.

Compile and run it yourself, then please tell us what it did.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#485

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-04-02 21:31 -0700
Message-ID<lmtfp6d63kcrqortkn60q7maipr5cg36qn@4ax.com>
In reply to#474
On Sat, 02 Apr 2011 22:04:03 +0200, Merciadri Luca
<Luca.Merciadri@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.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
Doing what the user expects with respect to navigation is absurdly important for user satisfaction.
~ anonymous Google Android developer

[toc] | [prev] | [next] | [standalone]


#491

FromMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Date2011-04-03 11:16 +0200
Message-ID<87tyefbsol.fsf@merciadriluca-station.MERCIADRILUCA>
In reply to#485
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Roedy Green <see_website@mindprod.com.invalid> writes:

> On Sat, 02 Apr 2011 22:04:03 +0200, Merciadri Luca
> <Luca.Merciadri@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
See http://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-----

[toc] | [prev] | [next] | [standalone]


#506

FromMerciadri Luca <merciadriluca@gmail.com>
Date2011-04-03 09:01 -0700
Message-ID<4f3453aa-5824-4aa1-81f0-6bfabcff3c7b@hd10g2000vbb.googlegroups.com>
In reply to#491
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.

[toc] | [prev] | [next] | [standalone]


#514

Frommarkspace <-@.>
Date2011-04-03 10:53 -0700
Message-ID<inac6q$6ns$1@dont-email.me>
In reply to#506
On 4/3/2011 9:01 AM, Merciadri Luca wrote:

>
> 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,


I realize that there may be English language issues here, but I'd hope 
that an advanced student in OOP would know that "dynamic links" is 
called "polymorphism."


> * output 1 is 1 because a becomes b,

Oddly stated, but ok.

> * output 5: ((A) b).m() is equivalent to b.m(), that is, output 2,
> which is 1,

Why?  There's a technical word that belongs here instead of "equivalent."

> * output 6: ((A) b).v is not b.v, because variables are accessed using
> static links.

Too poorly stated to pass.  Again, there is some technical language that 
describes "static links" properly that a OOP student should know about 
from previous studies.  What are those technical words?


[toc] | [prev] | [next] | [standalone]


#518

FromMerciadri Luca <merciadriluca@gmail.com>
Date2011-04-03 11:28 -0700
Message-ID<548b9e2c-28a4-4ebf-87ba-a20c9c54c287@k30g2000yqb.googlegroups.com>
In reply to#514
On Apr 3, 7:53 pm, markspace <-@.> wrote:
> On 4/3/2011 9:01 AM, Merciadri Luca wrote:
>
>
>
> > 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,
>
> I realize that there may be English language issues here,
Tell me where it is, please, then. I learn English every day. :)

> but I'd hope
> that an advanced student in OOP would know that "dynamic links" is
> called "polymorphism."
Thanks for making links. This is fresh material for me, you know!

I don't remember where I posted that I was attending an advanced
course on OOP, but this is wrong: I'm following an *introductory*
course in OOP.
>
> > * output 1 is 1 because a becomes b,
>
> Oddly stated, but ok.
>
> > * output 5: ((A) b).m() is equivalent to b.m(), that is, output 2,
> > which is 1,
>
> Why?
Because methods are accessed using dynamic links, as I said earlier.
Casting to another type which is a subclass of a class does not modify
methods, but only variables!

> There's a technical word that belongs here instead of "equivalent."
I don't know what is this word.
>
> > * output 6: ((A) b).v is not b.v, because variables are accessed using
> > static links.
>
> Too poorly stated to pass.  Again, there is some technical language that
> describes "static links" properly that a OOP student should know about
> from previous studies.  What are those technical words?
Exactly the same as before: casting to another type which is a
subclass of a class does not modify methods, but only variables. Here,
((A) b).v is a.v.

[toc] | [prev] | [next] | [standalone]


#528

Frommarkspace <-@.>
Date2011-04-03 13:35 -0700
Message-ID<inalm7$f2f$1@dont-email.me>
In reply to#518
On 4/3/2011 11:28 AM, Merciadri Luca wrote:

>>   There's a technical word that belongs here instead of "equivalent."
> I don't know what is this word.


Well I'm rushing out the door at the moment, but now that you know one 
word you should be able to find others:

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

[toc] | [prev] | [next] | [standalone]


#531

FromMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Date2011-04-03 22:58 +0200
Message-ID<87tyefxd9z.fsf@merciadriluca-station.MERCIADRILUCA>
In reply to#528
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

markspace <-@.> writes:

> On 4/3/2011 11:28 AM, Merciadri Luca wrote:
>
>>>   There's a technical word that belongs here instead of "equivalent."
>> I don't know what is this word.
>
> Well I'm rushing out the door at the moment, but now that you know one
> word you should be able to find others:
>
> http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Thanks. I did not know you were speaking about polymorphism. I know
this. I check the link right now.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

If you fall off a cliff, you might as well try to fly. After all, you got nothing to lose.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2Y34YACgkQM0LLzLt8MhzXPACgidu6h2KVdOlJkwGhu5wcIZ6t
KMIAniStqMIqnsORJ6qRMPJVmj8aYFLO
=wEJI
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#555

FromLew <noone@lewscanon.com>
Date2011-04-06 12:32 -0400
Message-ID<ini4j7$77b$1@news.albasani.net>
In reply to#518
Merciadri Luca wrote:
> Because methods are accessed using dynamic links, as I said earlier.
> Casting to another type which is a subclass of a class does not modify
> methods, but only variables!

"Static" has a particular meaning in Java.

These aren't usually called "links".

Subtypes do *not* modify variables in their supertypes.  They do override 
supertype methods, potentially, but they don't "modify" methods, either.  This 
is rather the opposite of what you suppose.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web