Basic Doubt

please clear my doubts 1.why don't we have pointers in java .Whatz itz advantage.2.Whatz the difference between virtual function in c++ and interface in java.Give Me Exact Reason
[207 byte] By [Hartbreaka] at [2007-10-2 7:04:50]
# 1

>1.why don't we have pointers in java .Whatz itz advantage.

Java has no pointers but instead uses references. A reference provides access only to the functionality of the object. The programmer does not have direct access to memory removing the dangers of pointer manipulation. For example:

* Pointers remove the concept of private data since they can provide direct access to that data.

* In some cases, integer values can be converted to pointer addresses which can result in incorrect addresses being formed and hence memory corruption.

* Arrays can be directly accessed by pointers. This can result in the program writing past the end of the array.

Interestingly, the lack of pointers can be a hindrance in specific cases. For example, imagine trying to create a memory manager for an operating system without pointers.

> 2.Whatz the difference between virtual function in c++ and interface in java.

C++ - A virtual function is a function member of a class, declared using the "virtual" keyword. A pointer to a derived class object may be assigned to a base class pointer, and a virtual function called through the pointer. If the function is virtual and occurs both in the base class and in derived classes, then the right function will be picked up based on what the base class pointer "really" points at.

Java Interface - an abstract base class with all pure virtual functions and no instance variables.

Hope this helps...

--

HydieBuddy

hydiebuddya at 2007-7-16 20:35:49 > top of Java-index,Java Essentials,New To Java...
# 2

> please clear my doubts

>

> 1.why don't we have pointers in java .Whatz itz

> z advantage.

>

I like to think of it like this...

They are there but protected so that they always point to objects.

> 2.Whatz the difference between

> difference between virtual function in c++ and

> interface in java.

>

> Give Me Exact Reason

An interface is a contract.

Gargoylea at 2007-7-16 20:35:50 > top of Java-index,Java Essentials,New To Java...
# 3

Please help spread the word: The word "doubt" is inappropriate and confusing for subject lines. I have no idea what you mean by "doubt." Did you mean "confusion"? Even "confusion," though, is too vague. The more meaningful & specific your subject, the more meaningful & specific the replies will be.

Dick_Adamsa at 2007-7-16 20:35:50 > top of Java-index,Java Essentials,New To Java...
# 4

1. the only difference between pointers in c/c++ is that you can't perform arithimetic on them ie *p++

. My take is that if you manage your data structures and alogrithims correctly, then there is no need to do this -

making java a bit simpler than c or c++ (some would say dumber).

If you're doing the sort of low level manipulation that requires this for performance reasons etc then you probably should be using C of C++. Choose the tool.

2. There is no relationship between interfaces in java and c++ virtual functions. A C++ virtual function is equivalent to a java method (and a final java method equivalent to a non-virtual function in C++). A java interface is equivalent to a C++ class consisting only of pure virtual functions (ie definitions only, no implementation).

David_Waddella at 2007-7-16 20:35:50 > top of Java-index,Java Essentials,New To Java...
# 5

The major advantage of not having pointers is that you can't have pointers which look valid but in fact point to never never land. Invalid pointers have been the cause of endless grief to many C/C++ programmers (myself being one).

In Java, the only invalid reference is null, and you can immediately tell it's invalid. You can't have any "silent" errors with a null reference - the NullPointerException will hit you like a ton of bricks. This is not the case in C: A program can seem to execute normally when it is actually reading from or writing to the wrong place in memory.

Dick_Adamsa at 2007-7-16 20:35:50 > top of Java-index,Java Essentials,New To Java...
# 6

> The major advantage of not having pointers is that

> you can't have pointers which look valid but in fact

> point to never never land. Invalid pointers have been

> the cause of endless grief to many C/C++ programmers

> (myself being one).

>

oh yes. been there. good point well made.

David_Waddella at 2007-7-16 20:35:50 > top of Java-index,Java Essentials,New To Java...