Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's improve <a href="https://stackoverflow.com/questions/2952732/samples-of-scala-and-java-code-where-scala-code-looks-simpler-has-less-lines/2952776#2952776">stacker's example</a> and use Scala's <a href="http://docs.scala-lang.org/tutorials/tour/case-classes.html" rel="nofollow noreferrer">case classes</a>:</p> <pre><code>case class Person(firstName: String, lastName: String) </code></pre> <p>The above Scala class contains all features of the below Java class, <em>and some more</em> - for example it supports <a href="http://docs.scala-lang.org/tutorials/tour/pattern-matching.html" rel="nofollow noreferrer">pattern matching</a> (which Java doesn't have). Scala 2.8 adds named and default arguments, which are used to generate a <a href="http://www.scala-lang.org/node/2075" rel="nofollow noreferrer">copy method</a> for case classes, which gives the same ability as the with* methods of the following Java class.</p> <pre><code>public class Person implements Serializable { private final String firstName; private final String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Person withFirstName(String firstName) { return new Person(firstName, lastName); } public Person withLastName(String lastName) { return new Person(firstName, lastName); } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Person person = (Person) o; if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) { return false; } if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) { return false; } return true; } public int hashCode() { int result = firstName != null ? firstName.hashCode() : 0; result = 31 * result + (lastName != null ? lastName.hashCode() : 0); return result; } public String toString() { return "Person(" + firstName + "," + lastName + ")"; } } </code></pre> <p>Then, in usage we have (of course):</p> <pre><code>Person mr = new Person("Bob", "Dobbelina"); Person miss = new Person("Roberta", "MacSweeney"); Person mrs = miss.withLastName(mr.getLastName()); </code></pre> <p>Against</p> <pre><code>val mr = Person("Bob", "Dobbelina") val miss = Person("Roberta", "MacSweeney") val mrs = miss copy (lastName = mr.lastName) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload