java真的有指针吗? [英] Does java really have pointers or not?

查看:114
本文介绍了java真的有指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了google的答案,但我不满意。



我的逻辑



Java 使用内存位置,它仅在您无法查看或访问它的场景之后(据我所知,可能有访问它们的方法


$ b

我的混淆/问题:不使用像Java这样的编程语言中的指针的目的是为了让互联网专门用于任何系统,而不是像使用指针的c ++这样的编程语言?



编辑






许多人都在说保持简单。如果是这样,那么为什么流行的编程语言,如 c ++ ,使用指针呢?

解决方案

简单的答案是,这是一个设计决策。稍长一点的答案是C ++中的指针只是内存操作所必需的,这不是一个适用于Java的概念(见下文)。



,面向对象的编程模型和所有基于类的类型本质上总是通过指针处理,因此这个事实根本不会暴露给用户。



C ++中的原始指针在高质量,系统和惯用的编程中不是非常必要。什么原始指针允许你做的(即指针算术)通常被认为是不安全的,因此它完全不在Java中。



许多人试图用C和C ++中的指针做的事情实际上是未定义的行为。使用指针正确地留下了一个相当有限的选项集,其中大部分可以在惯用的C ++中做得更好。



关于唯一的使用指针是直接内存操作。因为Java不希望你这样做(事实上它的垃圾回收内存管理会积极干扰和手动内存操作打破),没有必要显式指针。



更新后:最后一段是(在我看来)最引人注目的解释:在C ++中,你需要能够编写自己的内存管理代码(参见allocators)。并且通过指针处理存储器 。所以我的严格的答案是,当你实现内存管理,而不是其他方式,你需要C指针。


I have looked on google for answers but I am not satisfied.

My Logic:

Java uses memory locations, it's just behind the scenes where you can't see or access it (to my knowledge, probably there are ways of accessing them that I don't know).

My Confusion / Question :

What is the purpose of not having pointers in a programming language like Java, designed specifically for the internet to be used on any system, vs a programming language like c++, which does use pointers?

Edit


Many of you are saying "To keep it simple". If this is the case, then why does a popular programming language, like c++, use pointers anyway?

解决方案

The simple answer is that it is a design decision. The slightly longer answer is that pointers in C++ are only necessary for memory manipulation, which is not a concept that applies to Java (see below).

Java envisions a fairly systematic, object-oriented programming model, and all class-based types are essentially always handled through a pointer, and so this fact isn't exposed to the user at all.

Raw pointers in C++ aren't very necessary in high-quality, systematic and idiomatic programming, either. What raw pointers do allow you to do (namely pointer arithmetic) is generally considered "unsafe", and so it is simply left out of Java altogether.

Note by the way that many things people attempt to do with pointers in C and C++ is actually undefined behaviour. Using pointers correctly leaves you with a fairly restricted set of options, most of which can be done better in idiomatic C++.

About the only real use for pointers is direct memory manipulation. Since Java doesn't want you to do that (and in fact its garbage-collected memory management would actively interfere with and be broken by manual memory manipulation), there's no need for explicit pointers.

After your update: The last paragraph is (in my opinion) the most striking explanation: In C++ you need to be able to write your own memory managing code (cf. "allocators"). And memory has to be handled via pointers. So my strict answer is that you need pointers in C++ when you're implementing the memory management, and never otherwise.

这篇关于java真的有指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆