简体   繁体   English

Ruby 的等价性:Java 中的符号?

[英]equivalence of Ruby :symbol in Java?

noticed the neat features of ruby symbols here 注意到这里红宝石符号的简洁特征

Does java have anything similar to this? java有没有类似的东西? what would it be called?会叫什么?

D don't think a final string would do all the features. D 不认为最终的字符串可以完成所有功能。 especially the way its stored and it would still need a toString for comparison.特别是它的存储方式,它仍然需要一个 toString 进行比较。

Symbols are actually immutable identifiers.符号实际上是不可变的标识符。 They can't change, are not garbage collected , and have performance advantages over Strings.它们不能改变, 不被垃圾收集 ,并且比字符串具有性能优势。 I assume you already know what a String is.我假设您已经知道 String 是什么。 So the answer would be: Java doesn't have anything like Symbols.所以答案是:Java 没有像 Symbols 这样的东西。

Like @Idan said (and that is pretty clever) enums can be used as symbols except they don't have methods that you can call on .就像@Idan 说的(这很聪明),枚举可以用作符号,除非它们没有可以调用的方法 And symbols in Ruby are not immutable strings. Ruby 中的符号不是不可变的字符串。

Refer to this post if you want to know their core differences.如果您想了解它们的核心区别,请参阅这篇文章

The general answer to the "does Java have this feature?" “Java 有这个特性吗?”的一般答案question is "no!".问题是“不!”。 However, enums can usually solve the same problems symbols solve.但是,枚举通常可以解决符号解决的相同问题。

A Ruby symbol is an immutable string object which gets indexed in a symbol table for singe-reference re-use (hence the very inventive nomenclature!). Ruby 符号是一个不可变的字符串对象,它在符号表中编入索引以供单一引用重用(因此是非常有创意的命名法!)。

In Java, all String objects are immutable, and all of them are indexed in the (inventively named) symbol table for re-use by the JVM (as long as they are referenced and live).在 Java 中,所有String对象都是不可变的,并且所有这些对象都在(创造性命名的)符号表中建立索引,以供 JVM 重用(只要它们被引用并处于活动状态)。

So, in Java, a String ( "my_symbol" ) is the equivalent of a Ruby symbol ( :my_symbol ).因此,在 Java 中, String ( "my_symbol" ) 相当于 Ruby 符号 ( :my_symbol )。

To get something equivalent to a Ruby 'my string' (non-immutable), you'd have to go to more complex Java classes ( new StringBuilder("my string") , etc.).要获得相当于 Ruby 'my string' (非不可变)的东西,您必须使用更复杂的 Java 类( new StringBuilder("my string")等)。 Or, if you have Groovy loaded the JVM, it overlays a similar mutable concept with GString .或者,如果您让 Groovy 加载了 JVM,它会用GString覆盖一个类似的可变概念。

The closest thing you can get to a Ruby Symbol in Java would be calling intern() on a String.最接近 Java 中 Ruby 符号的方法是在字符串上调用intern() While in Java you can have many String objects with the same content, intern ensures that it returns the same object for the same content (sequence of characters), thus it returns a canonical/distinct value.虽然在 Java 中可以有许多具有相同内容的 String 对象,但intern确保它为相同的内容(字符序列)返回相同的对象,因此它返回一个规范/不同的值。 So, while for generic Java Strings you have to call the equals method to check for content equality, for interned Strings you can simply use the == operator.因此,对于通用 Java 字符串,您必须调用equals方法来检查内容是否相等,而对于内部字符串,您可以简单地使用==运算符。

For further details, see: https://en.wikipedia.org/wiki/String_interning有关更多详细信息,请参阅: https : //en.wikipedia.org/wiki/String_interning

A symbol in ruby is just an immutable string. ruby 中的符号只是一个不可变的字符串。 The java equivalent would just be a normal String . java 等价物只是一个普通的String I don't really see too many ruby specific features there, except maybe this memory leak one...我真的没有在那里看到太多 ruby​​ 特定的功能,除了这个内存泄漏......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM