As the title says. I wanted to do Scala, so I summarized it. Scheduled to be updated each time during learning. By the way, my background is C # 4 years and Java 1 year.
It looks good as Java. Camel case
--No need for a trailing semicolon --Comments are the same // and / * * /
Type inference works with declarations! (From 10 for Java)
val hoge = 1 //immutable (variable with final)
var hoge = 1 //mutable (variable)
With type specification,
val i:Int = 1
val str:String = "Hi!"
To embed a variable in a string, prefix the literal with s and prefix the variable with $.
val firstName = j
val familyName = ichikawa
val str = s"my name is $firstName - $familyName"
You can also embed code inside literals
val str = s"My weight is ${weight + 3}"
Writing is the same Can be written as an expression that returns a value
val age = 30
val result =
if (age >= 30) "over 30"
else "under 30"
println(result)// over 30
It's basically the same, but it seems that there is no ternary operator. Since the value can be returned with if, it can be substituted
val result = if (age >= 30) "over 30" else "under 30"
switch It seems to say match
val team = "So"
val result = team match {
case "B" | "seagull" => "Bッテ" //or condition
case other => s"undefined: ${other}" // default
}
println(result)
// undefined:So
while is the same, but different from for It seems that the condition is called a generator
for (i <- 0 to 3) println(i) //The condition can be 0 until 4 and can be 0 or more and less than 4.
// 0
// 1
// 2
// 3
for (i <- 0 to 3 if i != 2) println(i) //Conditional
// 0
// 1
// 3
var result = for (i <- 0 to 3) yield i //Get results
for (i <- result) println(i)
for (i <- 0 to 3; j <- 0 to 3) println(s"$i, $j") //for nesting
With java
public void sayHello() {
System.out.println("hello");
}
public String sayHelloStr() {
return "hello";
}
public String getHelloMessage(String firstName, String familyName) {
return "Hello " + firstName + " " + familyName;
}
But,
def sayHello: Unit = {
println("hello")
}
def getHelloStr: String = {
"Hello" //return Optional
}
def getHelloMessage(firstName: String, familyName: String): String = {
s"Hello $firstName $familyName"
// firstName: String = "taro"You can set the default value like this. C#Is the same as
}
class Human(val name: String) { //Become a constructor with parameter definition
def sayHello() = println("hello") //Lambda-like method definition
}
//Instantiation
val human = new Human("j-ichikawa")
val name = human.name;
human.sayHello()
Inheritance is
class PerfectHuman(name: String) extends Human(name) {
override def sayHello() = println("perfect hello")
}
Declaration is the same
package aaa.bbb.ccc
The call is
import aaa.bbb.ccc.ddd //Individual
import aaa.bbb.ccc.ddd.{ddd, eee} //Multiple
import aaa.bbb.ccc.ddd._ //collect
public, protected, private The way to attach and see to the members of the attached class is the same If nothing is attached, it will be public by default (private is the default in C #, isn't it? final The function when attached to a class or member is the same as Java. Declare with val to make the variable immutable
object It seems to be a keyword, not a type Add object to the class with the same name
//It's like a companion object
object Human {
def sayHello() = println("hello") //Can be called without instantiation like a class method
def apply(name: String) = new Human(name) //Become a factory method definition
}
//This is a companion class
class Human (val name: String) {
def sayHello() = println("hello")
}
//How to use companion objects
Human.sayHello()
val taro = Human("taro") //Generate taro instance
println seems to be a method of an object called Predef
A new concept. ..
Yeah, no resistance
abstract class Animal {
def cry()
}
class Cat extends Animal {
def cry() = println("Nyaaaa")
}
It seems to be a trait
trait Runnable {
def run() = println("Dadada. .. ..")
}
class Player extends Runnable //Same syntax as implementation inheritance C#Same as
class SuperPlayer extends Player with Runnable //If inherited, implement with with
Updating. .. ..
Recommended Posts