Explore Scala (8) -- keywords

Source: Internet
Author: User
Tags case statement java keywords

This article compares the keywords of Java and Scala

Java keywords

Java has a total of 50Keywords(Keywords), Two of which areReserved Words, Not used yet:GotoAndConst.True,FalseAndNullIt looks like a keyword, but it's actually justLiteral. This article roughly regards true, false, and null as Java keywords and considers Java as a total of 53 keywords. Below is a list of roughly classified Java keywords:

  • Assert
  • Boolean, byte, short, Char, Int, long, float, double, void
  • Package, import, class, interface, Enum, implements, extends
  • Public, protected, Private, abstract, static, final, volatile, transient, synchronized, strictfp, native
  • Try, catch, finally, throw, throws
  • If, else, do, while, for, switch, Case, default, break, continue, return
  • Super, this
  • New, instanceof
  • Const, Goto
  • True, false, null
Scala keywords

Scala only has 39 keywords:

  • Package, import, class, object, trait, extends, with, type, forsome
  • Private, protected, abstract, sealed, final, implicit, lazy, override
  • Try, catch, finally, throw
  • If, else, match, case, do, while, for, return, yield
  • Def, Val, VAR
  • This, super
  • New
  • True, false, null
Comparison of Java and Scala keywords

To intuitively compare the list of Java and Scala keywords, I drew the following figure:

Keywords shared by Java and Scala

Keywords shared by Java and Scala have the same meanings in the two languages. Only one exception:Case. In Java, case is mainly used in switch-case statements. Scala does not have a switch-case statement. The case keyword is mainly used to define the case class and performPattern Matching.

Keywords used only in Java

A total of 28 Java keywords are not used by Scala, but Scala also needs to run on JVM. So let's take a look at how the meaning of these 28 keywords are implemented in Scala.

Assert

Scala. predef defines the assert () method as follows:

object Predef {    @elidable(ASSERTION)    def assert(assertion: Boolean) {        if (!assertion)            throw new java.lang.AssertionError("assertion failed")    }    @elidable(ASSERTION) @inline    final def assert(assertion: Boolean, message: => Any) {        if (!assertion)            throw new java.lang.AssertionError("assertion failed: "+ message)    }}

Boolean, byte, Char, short, Int, long, float, double

Scala does not have the primitive type and is replaced by the corresponding classes: Boolean, byte, Char, short, Int, long, float, and double. For more information, see this article.

Void

Similar to the primitive type, scala uses a more oo way to express void:UnitClass. For more information, see this article.

Interface and implements

ScalaTraitReplacedInterfaceFor more information, see this article.

Static

Scala does not have the static concept. InsteadSingleton object. For more information, see this article.

Public

In Scala, classes, methods, fields, and so on are public by default, and Scala cannot explicitly set them to public. Therefore, the word "public" has no special meaning, free to use.

Const and goto

Const and goto are no longer reserved words and can be used freely.

Throws

Scala abandonedChecked exceptionAnd the throws keyword.

Native, transient, volatile, and strictfp

The four keywords are replaced by four Annotations: scala. Native, scala. Transient, scala. volatile, and Scala. annotation. strictfp. The following is the sample code:

@transient var t = 1@volatile var v = 1@strictfpdef f() = {}@nativedef n(x: Int): Int;

Synchronized

Scala provides the synchronized method, which is almost the same as the synchronized keyword in Java:

def sync() = {    this.synchronized {        val x = 2    }}
However, there is actually no synchronized method. After decompiling, the scala code above is exactly the same as the following Java code:

public void sync() {    synchronized(this) {        ...    }}

Instanceof

The isinstanceof [] method is replaced by the following code:

def instanceof(arg: Any) = {    if (arg.isInstanceOf[String]) {        val str = arg.asInstanceOf[String]    }}

Enum

In Scala, if you want to defineEnumeration, It should inherit scala. enumeration, such as the following code:

object Color extends Enumeration {    val Red = Value    val Green = Value    val Blue = Value}

Break, continue, default, switch

As mentioned above, scala usesPattern MatchingReplaced switch-case, thus freeing upSwitchAndDefaultKeyword. Scala's for loop is also quite different from Java's for loop. Although Scala has a while and do-while loop, it cannot use break and continue, so these two keywords are also released.

References

Programming in Scala Version 2

Scala for the impatient


Explore Scala (8) -- keywords

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.