Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 4:34 PM   #1
cipicip
Newbie
 
Join Date: May 2006
Posts: 3
Rep Power: 0 cipicip is on a distinguished road
Unhappy why in C are used pointers and not in Java?

Hello

This question might seem foolish, but I've been asked at an interview this and didn't know what to say I told them the part with the garbage collector but I didn't actually answer the question.

So does anyone knows the answer?

Thanks
Ciprian
cipicip is offline   Reply With Quote
Old May 25th, 2006, 4:42 PM   #2
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
If I had to guess, it might be because they are different languages... wouldnt need any new languages if they were all the same would we?
NSchnarr is offline   Reply With Quote
Old May 25th, 2006, 4:53 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
The answer that I would provide is that Java encourages safety in operation. It will not let you do damage to the system. The use of pointers in C allows the programmer to get right down to the memory of computer, and therefore, if used incorrectly, the programmer might accidently cause problems (for example, calling free() on a previously free()ed pointer. )

Though, I think that would be an educated guess :p.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 25th, 2006, 4:55 PM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
I would have also said the garbage collecting.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old May 25th, 2006, 5:22 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Garbage collecting can be done in C. You can make your own GC and have it manage at least deletion, if not allocation. It's just not built into the language, nor is it guaranteed on the system you run on. Java, on the other hand, runs through the JVM, so I'm assuming that the GC takes advantage of that.
Jimbo is offline   Reply With Quote
Old May 25th, 2006, 5:48 PM   #6
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
It's a common misconception that Java has no pointers. It's not exactly true. Java has pointers, they're just not explicit. Every object is a reference to a location making it a pointer... you just can't manipulate the pointers directly.

Which approach is better.. explicit or not? Well, that very much depends on the context and scope of the application. I'm sure everyone who ever went through learning C will however agree that non-explicit pointers are sure as hell easier to comprehend for someone who is just learning programming.

Here's why it's a pointer.. say you have two Strings s and t

String message = "hi"; String text = "hello";

These are two separate objects in two different locations.

text = message;

Now text and message both reference the same location hence referencing the same object. That is a behavior of a pointer.
Toro is offline   Reply With Quote
Old May 25th, 2006, 6:56 PM   #7
cipicip
Newbie
 
Join Date: May 2006
Posts: 3
Rep Power: 0 cipicip is on a distinguished road
Thanks for your answers, guys. That's what I've told them myself, that Java was design to be safer and to manage the resources by itself. Like this you don't have to worry about erasing pointers, freeing the memory, about the so much dreaded memory leaks, so on and so forth. Not to mention the buffer overflow exploits. But apparently it's not enough. They were expecting another answer . An obvious answer, as they said. Any idea what the hell they were talking about? Since I came, I keep thinking about their question
cipicip is offline   Reply With Quote
Old May 25th, 2006, 7:04 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Well, the memory management shouldn't be part of the answer. Memory management can be done in C. What Toro said might be part of it. Objects in Java have some pointer-ish behavior. The "erasing pointers, freeing the memory, about the so much dreaded memory leaks, so on and so forth" sounds like saying the same thing over and over. I don't know what they would have been expecting, though.
Jimbo is offline   Reply With Quote
Old May 25th, 2006, 7:15 PM   #9
cipicip
Newbie
 
Join Date: May 2006
Posts: 3
Rep Power: 0 cipicip is on a distinguished road
Ok, again thanks for your answers.
cipicip is offline   Reply With Quote
Old May 25th, 2006, 9:12 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It's not really about what you can do with C, but what you can't do with Java. In Java, you can't, for example, increment a reference or change the memory address it's pointing to - it points to one thing, and that's it. Again, this is to do with safety - if the pointer's always pointing to something, you can't screw up.

Hopefully. :p
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC