1

noticed the neat features of ruby symbols here

Does java have anything similar to this? what would it be called?

D don't think a final string would do all the features. especially the way its stored and it would still need a toString for comparison.

Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71
  • Just to clarify: Ruby strings behaves like mutable but actually immutable like symbols. It internally creates new strings and replace the reference pointing to the new one – SwiftMango Apr 08 '15 at 22:46
  • 1
    To specifically address storage and comparison, an `intern`ed `String` would provide analogous behavior (which string literals will normally be). – Matt Whipple Jan 22 '17 at 08:29

5 Answers5

5

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. So the answer would be: Java doesn't have anything like 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. And symbols in Ruby are not immutable strings.

Refer to this post if you want to know their core differences.

Gorille
  • 170
  • 12
sargas
  • 5,820
  • 7
  • 50
  • 69
2

The general answer to the "does Java have this feature?" question is "no!". However, enums can usually solve the same problems symbols solve.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
1

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!).

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).

So, in Java, a String ("my_symbol") is the equivalent of a Ruby symbol (: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.). Or, if you have Groovy loaded the JVM, it overlays a similar mutable concept with GString.

Jake Gage
  • 9
  • 2
  • The note about reuse is misleading. Literals are interned and reused but strings created at runtime are generally not (as evidenced by most heap dumps). – Matt Whipple Jan 22 '17 at 08:33
  • GStrings are also a different animal and aren't designed for the shared binding type behavior associated with mutable objects. I'd think if your design calls for aliasing like that it would be best expressed by a basic mutable bean with a string property and descriptive name...Or even an `AtomicReference` – Matt Whipple Jan 22 '17 at 09:37
  • Or cut out the String wrapper and just use an Array or List of chars – Matt Whipple Jan 22 '17 at 09:43
0

The closest thing you can get to a Ruby Symbol in Java would be calling intern() on a String. 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. 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.

For further details, see: https://en.wikipedia.org/wiki/String_interning

Vajk Hermecz
  • 5,413
  • 2
  • 34
  • 25
-4

A symbol in ruby is just an immutable string. The java equivalent would just be a normal String. I don't really see too many ruby specific features there, except maybe this memory leak one...

mango
  • 5,577
  • 4
  • 29
  • 41