Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Type casting

Reply
Thread Tools

Type casting

 
 
programming
Guest
Posts: n/a
 
      12-16-2006
Hi all, i was just wondering with the below program what effect what
type casting have on the final output. I tried compiling the sample
below, but there were to many bugs in the program to get it to work. I
think that type casting would copy the value "cufflinks for Aus" into
T2, and it would be printed out twice. Can somebody verify this with
me.


public class Thingy
{
public TextField value;

public Object clone()
{
Thingy n;

n=new Thingy();
n.value=this.value;

return n;
}
}


------------------------------------------------------------------------------------------------------------------------------------------------
public class Question
{
Thingy t1,t2;

public static void main(String args[])
{

t1= new Thingy();

t1.value.setText("Cufflinks for Aus");

t2=(Thingy) t1.clone();
t2.value.setText("Bah Humbug!");
}


}

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      12-16-2006

programming wrote:
> Hi all, i was just wondering with the below program what effect what
> type casting have on the final output. I tried compiling the sample
> below, but there were to many bugs in the program to get it to work. I
> think that type casting would copy the value "cufflinks for Aus" into
> T2, and it would be printed out twice. Can somebody verify this with
> me.
>
>
> public class Thingy
> {
> public TextField value;
>
> public Object clone()
> {
> Thingy n;
>
> n=new Thingy();
> n.value=this.value;
>
> return n;
> }
> }
>
>
> ------------------------------------------------------------------------------------------------------------------------------------------------
> public class Question
> {
> Thingy t1,t2;
>
> public static void main(String args[])
> {
>
> t1= new Thingy();
>
> t1.value.setText("Cufflinks for Aus");
>
> t2=(Thingy) t1.clone();
> t2.value.setText("Bah Humbug!");
> }
>
>
> }


Hmm, I think its time for you to get a Java book.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      12-17-2006
> programming wrote:
>> public class Thingy
>> {
>> public TextField value;


Most times one should keep member variables private and use accessor methods
to get at them.

>>
>> public Object clone()

Your intent is to override Object.clone(), correct?

>> {
>> Thingy n;
>>
>> n=new Thingy();
>> n.value=this.value;


This causes the clone to share a reference to the same TextField as its
progenitor's.

>>
>> return n;
>> }
>> }
>>
>>
>> ------------------------------------------------------------------------------------------------------------------------------------------------
>> public class Question
>> {
>> Thingy t1,t2;
>>
>> public static void main(String args[])
>> {
>>
>> t1= new Thingy();
>>
>> t1.value.setText("Cufflinks for Aus");
>>
>> t2=(Thingy) t1.clone();
>> t2.value.setText("Bah Humbug!");


This replaces the text attribute in the value field that is pointed to by both
t1 and t2.

>> }
>>
>>
>> }


Not only is the TextField 'value' shared, but it has public access, meaning
that it's subject to change any ol' which where.

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting primitive type to user-defined type works in usage xllx.relient.xllx@gmail.com C++ 2 04-15-2006 05:37 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM
Casting between const type** and type** Kevin L C++ 6 08-11-2003 03:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57