1. How does Scala declare variables and constants?
var a =0 // variable
val b =0 // constant
2. What are the Scala data types?
basic type:
Byte, Short, Int, Long, Float, Double, Boolean, Char ; the difference between them and java , they are all classes
var variable name : type = value
For example: var a2: Short =1
3. Are String objects mutable or immutable? If you want to create a modifiable string, which class should you use?
( 1 ) String objects are immutable
( 2 ) StringBuilder variable
4. What symbol is used for escape characters?
Start with \ , for example: \t, \n , etc.
5. What is the IF...ELSE syntax?
expr is a boolean expression
if (expr) {
//EVERYTHING
}else {
//TOD
}
6. What are the three types of loop statements, and what are their respective syntaxes? How to get out of the loop?
(1)for, while, do while
for (i <-1.to(3)) {
println(i)
}
var i = 1
while (i <3) {
//EVERYTHING
println(i)
i = i +1
}
var j = 1
do {
//EVERYTHING
println(j)
d = d +1
}while (j <1)
(2) There is no syntax for break and continue in scala , you need to use the Breaks object
breakable {
for (i <-1 to5) {
println(i)
break
}
}
7. What does Unit mean in the function ?
Represents empty, similar to void in java , except that its value is ()
8. How to define a function with no input parameters and no return value in Scala
def test {}
9. How to define a function that takes in parameters and returns a value in Scala
def test(param: Int): Int = {
100
}
10. What is a closure?
Closures are composed of code + non-global variables used
scale> var y = 10
y: Int = 10
scala> val sum= (x:Int) => x+y
sum: Int => Int = <function1>
scala> sum(10)
res6: Int = 20
11.val a = 10 , how to convert a to double type and String type?
a.toDouble和a.toString
E.g:
val a = 10
var b: Double = a.toDouble
var c:String = a.toString
12. In Scala functions, the last line of the method body is used as the return value. Do you need to call return explicitly ?
unnecessary
13. How to define an array of strings? Do subscripts start at 1 ?
(1) var array1 =new Array[String](10) 或者 var array2 =Array("a","b")
(2) The subscript starts from 0
14.1 to 10 ==> 1.to(10) , does 10 include or not?
Contains, to is a [ ] closed interval , and until is left closed and right open [ )
15.Range(1, 10) , does 10 contain or not?
Not included, it is left closed and right open [ )
Supplement: Does the following code contain 10 ?
for( a <- 1 until 10){
println( "Value of a: " + a );
}
It does not include 10 , until is also left closed and right open [ )
16. What is the Scala pattern matching syntax?
xx match {
case value => ...
}
E.g:
var name ="lisi"
namematch {
case "zhangsan" =>println("zhangsan")
case "lisi" =>println("lisi")
case _ =>println("no name match")
}
17. Is abnormal error reporting syntax?
try {
//EVERYTHING
}catch {
case and :Exception => // EVERYTHING
}finally {
//EVERYTHING
}
E.g:
try {
var i = 1/0
}catch {
case e1:NumberFormatException =>print(e1)
case _:Exception =>print("other exception")
}finally {
//EVERYTHING
}
18.Array , ArrayBuffer , who is fixed length? Who is getting longer?
Array fixed length, ArrayBuffer variable length
19. What is an implicit conversion function? In what scenario is it used? How to define it?
( 1 ) A function with a single parameter declared with the implicit keyword
( 2 ) (when a value needs to be converted from one type to another) type conversion
(3) implicit def intToChar(value: Int) = value.toChar
// If the following code comments out the intToChar function, the compilation will report an error
val max: Char = math.max(1,2)
print(max)
Supplement what is implicit conversion and explicit conversion:
Under normal circumstances, the conversion of data types is usually performed automatically by the compilation system without manual intervention, so it is called implicit type conversion. However, if the program requires that data of a certain type must be converted to another type, it can be converted by using the cast operator. This cast process is called explicit conversion. The display type conversion format is:
(mandatory type name) expression
Used to force the value of " expression " to be converted to the data type represented by " type name " . For example, the result of ( int ) 4.2 is 4 ; the purpose of casting is to change the data type of the value of the expression, so that operations between different types of data can continue.
Automatic type conversion, also known as implicit type conversion, refers to the type conversion that is automatically completed by the system without writing code.
The conversion rules are:
From a type with a small storage range to a type with a large storage range.
The specific rules are:
short(char)→int→long→float→double
That is to say, variables of type int can be automatically converted to type long . Example code:
int b = 10;long l = b;
20. Scala object-oriented , what are the three major features? What is the difference?
Encapsulation: Properties, Methods
Inheritance: the relationship between parent and child
Polymorphism: parent class reference points to child class object
21. What should I pay attention to in the basic syntax of Scala?
Like java , use camelCase
( 1 ) Class names - capitalize the first letter of all class names
( 2 ) Method name - the first letter of all method names is lowercase
22. What is the object? What is a class? How to create files in IDEA ?
( 1 ) Class: A collection of objects with the same properties and methods, which is an abstract description
Instance: is a concrete entity
A class is a template for an object, and an object is an instance of a class.
(2)File -> new
23. A series of problems with variable-length array ArrayBuffer
var c = new ArrayBuffer[Int]();
1. Add an element at the end
c+=1
2. Add multiple elements at the end
c+=(2,3)
3. Append collection
c++=Array(4,5)
4. Specify the location to add the element
c.insert(1,9) //1 is the subscript, and the subscript is inserted before the specified subscript
5. Remove trailing n elements
c.trimEnd(3) // Remove the last 3 elements
6. Remove the first n elements
c.trimEnd (n)
7. Remove an element from a position
c.remove(1)
8. Remove count elements starting from (and including ) subscript n
c.remove(n, count)
9. ArrayBuffer 转 Array
toArray
10. Array 转 ArrayBuffer
toBuffer