1. String
keyword:
-
package import class obejct trait extends with type for
-
private protected abstract sealed final implicit lazy override
-
try catch finlly throw
-
if else match case do while for return yield
-
def var val
-
this super
-
new
-
true false null
-
Keywords that Java does not have: object trait with implicit match yield def val var
character string:
-
Type: String
-
+Number connection
-
*String multiplication, copy a string multiple times
-
printf formatted output
-
String interpolation: s"xxx${varname}" prefix s template string, prefix f format template string, obtain variable value through $,% followed by format string.
-
Original string: raw"rawstringcontents${var}". The format string followed will not be considered.
-
Multiline string: '' ''.
-
Output: print printf println
package com.demo2 /** * @author June * @date 2022/1/25 9:12 */ object Test4 { def main(args: Array[String]): Unit = { //String output //String splicing println()Usage: through:assignment //String splicing: through+link val name = "alice" val age = 33 println(age+"Year old"+name+"I'm learning") //*Used to copy and splice a string multiple times println(name*2) //println(): character string. adopt%Value transmission printf("%d Year old%s I'm learning",age,name) println() //Difference between strings(Template)(Difference value (string) $Get variable value println(s"${age}Year old ${name}I'm learning") val num: Float = 2.33333445f println(s"The num is ${num}") //Format template string s val nums: Double = 333232.33333445 println(f"The num is ${nums}%.2f") //Format template string f println(raw"The num is ${nums}%.2f") //Format template string raw //Three quotation marks represent the string and keep the original format output of multi line string val sql=s""" |select * |from | student |where | name = ${name} |and | age > ${age} |""".stripMargin println(sql) } }
Test results:
2. Input
Input:
-
StdIn.readLine()
-
StdIn.readShort() StdIn.readDouble
-
import scala.io.StdIn
package com.demo2 import scala.io.StdIn /** * @author June * @date 2022/1/25 9:35 */ object Test5 { def main(args: Array[String]): Unit = { //Information input println("Please enter your name:") val name = StdIn.readLine() println("Please enter your age:") val age = StdIn.readInt() //Printout of console println(s"welcome ${age}Year old ${name}") } }
Test results:
3. Read file
package com.demo2 import java.io.{File, PrintWriter} import scala.io.Source /** * @author June * @date 2022/1/25 9:40 */ object Test6 { def main(args: Array[String]): Unit = { //Reading data from a file Source.fromFile("src/main/resources/test.txt").foreach(print) //Write data to file val writer = new PrintWriter(new File("src/main/resources/output1.txt")) writer.write("hello scala from ni") //Close output stream writer.close() Source.fromFile("src/main/resources/output1.txt").foreach(print) } }
Test results:
4. Data type
Data type:
-
java basic type char byte short int long float double boolean.
-
The package type corresponding to the java basic type: charter byte short integer long float double Boolean.
-
java is not purely object-oriented.
-
Scala absorbs this point. All data are objects and subclasses of Any.
-
Any has two subclasses: AnyVal value type, AnyRef reference type.
-
Numeric types are subclasses of AnyVal, which are the same as Java numeric wrapper types. Only integers are Int and characters are Char in scala.
-
StringOps is an enhancement of the String class in java and a subclass of AnyVal.
-
Unit corresponds to void and AnyVal subclasses in java. It is used for the position of the return value of the method. It indicates that the method has no return value. Unit is a type with only one single instance. It is converted into a string and printed as ().
-
Void is not a data type, but a keyword.
-
Null is a type. There is only one singleton object. Null is an empty reference. All reference types are subclasses of AnyRef. This type is mainly used to interoperate with other JVM languages and is hardly used in Scala code.
-
Nothing is a subtype of all types, also known as the bottom type. Its common use is to signal termination, such as throwing an exception, program exit, or infinite loop.
Integer type: all signed integers, represented by standard complement.
-
Byte 1 byte
-
Short 2 bytes
-
Int 4 bytes
-
Long 8 bytes
-
An error is reported when the initial value of an integer exceeds the representation range.
-
Automatic type inference. The default type of integer value is Int, and the long integer value must be represented by an L suffix.
-
Direct down conversion will fail, and forced type conversion (a + 10) is required toByte.
Floating point type:
-
Float IEEE 754 32-bit floating point number
-
Double IEEE 754 64 bit floating point number
-
Literal default Double
Character type:
-
The same as java Character, 2 bytes, UTF-16 encoded characters.
-
Character constant: ''
-
Type Char
-
Escape: \ t \ n \ R \ \ "\ 'etc
Boolean type: true false
Empty type:
-
Unit has no value and only one instance, which is used for the return value of the function.
-
Null has only one instance, null, empty reference.
-
Nothing determines that there is no normal return value. You can use nothing to specify the return value type.
package com.demo2 /** * @author June * @date 2022/1/25 10:10 */ object Test7 { def main(args: Array[String]): Unit = { val a1: Byte = 127 //Wrong definition:Byte Value range -128~127 //val a2: Byte = 128 val a2: Byte = -128 //The definition variable defaults to integer val a3 = 12 //default Int val a4: Long = 123444322222L //Long integer value definition //Wrong definition val b1: Byte = 10 val b2: Byte = (3+29) println(b2) val b3: Byte = (b1+33).toByte println(b3) //Floating point type val f1: Float = 1.2243f val d1:Double = 3.223 //Character type val c1: Char = 'a' println(c1) val c2: Char = '9' println(c2) //Special characters val c3: Char = '\t' //Tab val c4: Char = '\n' //Line feed println("abc" + c3 + "def") println("abc" + c4 + "def") //Escape character val c5 = '\\' //express\ val c6 = '\"' //express" println("abc" + c5 + "def") println("abc" + c6 + "def") val i1: Int = c1 println("i1:" + i1) val i2: Int = c2 println("i2:" + i2) val c7: Char = (i1+1).toChar println(c7) val c8: Char = (i2-1).toChar println(c8) } }
Test results: