انجمن‌های فارسی اوبونتو

لطفاً به انجمن‌ها وارد شده و یا جهت ورود ثبت‌نام نمائید

لطفاً جهت ورود نام کاربری و رمز عبورتان را وارد نمائید

نویسنده موضوع: تفاوت مرجع و اشاره گر - چرا در زبان جاوا از اشاره گر استفاده نمیشود  (دفعات بازدید: 4178 بار)

0 کاربر و 2 مهمان درحال مشاهده موضوع.

آفلاین !

  • High Sr. Member
  • *
  • ارسال: 586
  • جنسیت : پسر
  • Don't Panic!
    • مانیتورینگ سایت
خب جالب شد به بحث خوبی برسیم که یکی از انسانهای خوب انجمن اومدن و با ادعای مهندسی نرم افزار باعث شدند بخوام توضیح بدم که جاوا چرا اشاره گر نداره :


Does Java have pointers?

No, Java does not have pointers. This was an intentional decision by the creators of Java, because most people would agree that having pointers creates a lot of potential for bugs in the code – pointers can be quite confusing, especially to new programmers. Because arrays and strings are provided as class types in Java, there is no need for pointers to those constructs. By not allowing pointers, Java provides effectively provides another level of abstraction to the programmer.

But, what Java does have is references, which are different from pointers. Here are some of the differences between references in Java and pointers in C++:


ترجمه :
نه جاوا اشاره گر ندارد.این یک تصمیم عمدی بود که توسط سازندگان جاوا گرفته شد چرا که اشاره گر ها دارای پتانسیل بالایی برای  رخ دادن اشتباهات برنامه نوسیسی هستند.اشاره گر ها میتوانند بسیار گیج کننده باشند بخصوص برای افراد تازه کار.به دلیل اینکه آرایه ها و سترینگ ها به صورت کلاس در جاوا طراحی شده اند نیازی به اشاره گر به آن ساختار ها نیست.با عدم دسترسی به اشاره گرها، جاوا بصورت موثری سطوح انتزاعی بیشتری از برنامه نویسی را به برنامه نویسان ارایه داد.
در عوض جاوا دارای  مرجع است کهبا اشاره گر ها تفاوت دارند.تفاوت های  مرجع با اشاره گرها:


با تشکر از احسان عزیز برای ترجمه :)
« آخرین ویرایش: 21 شهریور 1392، 02:08 ق‌ظ توسط داریوش ! »
I just felt like running

Altern AI Directory --- GitHub

آفلاین !

  • High Sr. Member
  • *
  • ارسال: 586
  • جنسیت : پسر
  • Don't Panic!
    • مانیتورینگ سایت
یکسری پرسش و پاسخ ها در stackoverflow :

q :

نقل‌قول
If Java does not have pointers then what does the the new keyword do in Java?

I am confused, please explain.

answers

As pointed out, Java has references. How are these different ?

    you can't perform arithmetic or other such operations on these
    they do not point to the memory containing the object (i.e. they are not pointers by another name). The JVM is at liberty to move objects around within the VM memory, and most likely will do during garbage collection. The references however still point to that object, despite its movement within memory.

So they're not like C++ references (pointing directly to an object). Perhaps a better name would be handle


----------

Java doesn't have pointers; Java has references.

It's a fine point, but a pointer has extra operations that you may (or may not) typically use; a reference lacks these operations because the operations may be unsafe.

For example, if you use a pointer to index the first element of an array like so:

int primes[] = {2, 3, 5, 7, 11, 13, 17, 19};
int* intPointer = primes;

you may dereference the pointer and get the value "2", but you may also:

intPointer++

and after you do that, when you dereference the pointer you will get the value "3". This is because the ++ operation moves the pointer one "unit" ahead in memory.

The issue comes from the weaknesses in the C / C++ typechecking system (C++ must maintain compatibilty with C, so it allows the same issues). The pointer stores an address in memory and the ++ operation adds the appropriate number of bytes to the address. On many systems ++ing an int adds four bytes, but if the pointer was a char pointer ++ing it should only add one byte. Note that since the underlying data type of a pointer is an address in memory, the following is legal (but not recommended):

char* charPointer = primes;
charPointer++;

void* voidPointer = primes;
voidPointer++;

Since pointers are addresses in memory, they might represent (correctly) any bit of memory in the computer, but they are only properly dereferenced when the underlying data maches the type and alignment of the pointer. For pointers that aren't managed by lots of code to make them safe, this means you might stray off the data type (or alignment) of the desired information and a dereference might end in disaster. Attempting to fix this issue with custom code tends to slow down one pointers badly enough that you notice performance issues, and it opens the doors for adding errors in the custom "pointer management" code.

Java side steps all of these issues by returning a reference. A reference does not refer to any location in memory; Java maintains an internal "reference to pointer" table. This table takes the reference and returns the data associated with it, wherever that data may reside in memory. This slows down code execution, because two lookups are done for each "dereferencing", one lookup in the reference table, one in the machine's memory.

A big advantage of Java using references is that the memory can be moved around without breaking the would-be pointer addresses. In a C program, if you move data into a new memory location, it is very difficult to know whether some other part of the program has a pointer to the data. Should a stale pointer be dereferenced after the memory is moved, the program will be accessing corrupt data, and typically a crash will be shortcoming.

Ability to move the memory around in a running program allows programs to easily recycle memory. Any program which doesn't need chunks of memory can release the unused memory, but this creates memory holes of unused memory in between chunks of used memory. Internally computers use pages of memory, which are quite large. If a sparsely used page of memory could have the few used bits moved into another page, then a page of memory can be freed. This increases the density of data to memory, improving cache performance. Sometimes this translates into performance improvements that can be quite dramatic.

Java's Garbage Collector takes advantage of the use of references by temporarily blocking access to the data for a set of references. During that blockage of access, it moves the data around (to compact it). After the blockage, the reference to address table has the new memory addresses. Since the "functional" layer of the code never knew the addresses in the first place, this operation will not break a running Java program.

مرجع : http://stackoverflow.com/questions/2629357/does-java-have-pointers

و چنتای دیگه :

http://stackoverflow.com/questions/8080617/why-doesnt-java-have-pointers

I just felt like running

Altern AI Directory --- GitHub

آفلاین mohammadgolfami

  • Full Member
  • *
  • ارسال: 213
  • جنسیت : پسر
ممنون. خیلی خوب بود.
محدودیت تنها در ذهن ماست!