A preliminary study of Scala (I.)

Source: Internet
Author: User

Note: The Scala version I used is 2.11.8

First, the Hello World code, as follows:

Object helloscala{
  def main (args:array[string]): Unit = {
    println ("Hello World ...")}
}

Here is a brief description of some of Scala's knowledge Points: first, Scala data types

   Scala's data types include: Byte, Char, short, Int, Long, Float, Double, Boolean
    Note: Scala will infer the type of the variable itself

Each base type has a corresponding rich wrapper class, such as int has a Richint class, String has a richstring class, and these classes are in package scala.runtime.
When an object of a basic data type is called the method provided by its rich wrapper class, Scala automatically converts the object to the corresponding rich wrapper type by an implicit conversion, and then calls the appropriate method. For example: val a = 1 to 10, according to the source found int does not have to method, but its Richint class has.

Operator: In Scala, you can use the plus (+), minus (-), multiply (*), except (/), remainder (%) operators, and these operators are methods. For example, 5 + 3 and (5). + (3) is equivalent, look at the source code found here The + is the method name, is a method in the Int class. second, the definition of the variable

var defines mutable variable
val: Defines an immutable variable, like final Val in Java, which
indicates that the reference is immutable, not that the value he modifies is not mutable
scala> val str = "Hello Scala"
str:string = Hello Scala

scala> val str1:string = "Hello Scala"
str1:st Ring = Hello Scala

scala> val num = 1
num:int = 1

scala> val num1:double = 1
num1:double = 1.0
  
   scala> num1=10.11
<console>:12:error:reassignment to Val
       num1=10.11
           ^

scala> var num2 =
Num2:int = Ten

scala> num2 = 111
num2:int = 111

scala> Print (num2)
111
scal A>
  
iii. definition of functions

Give the two simple functions as follows:

Def functiontest (): Unit = {
    var a = 1
    a = 3
    val b = 1
    println (A + "+" + B + "=" + (A + B))//return: 3+1= 4
    println (A + "+" + B + "=" + A + B)//return string: 3+1=31
  }
The above functions are explained:
def: The identifier of the function,
FunctionTest: function name,
FunctionTest (): A function variable in parentheses,
Unit: The return type of the function, if the function has no return value, can be returned as Unit, this Java-like void
The main difference is that you can have a unit type value in Scala, which is (), but there is no void type value in Java. In addition to this, unit and void are equivalent;
def funtest (X:int, y:int): String = {
    if (x > Y)
      return (x + y) + ""    //return can not write
    else
      (y-x) + " "
  }
As the above: function can have a return value, this is a bit different from the function in Java
x, y: The name of the parameter passed, the String after the colon: the call to return a value type
function is like Java, and is directly introduced where it needs to be called.

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.