In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the relationship and logical operation method of Scala". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Mathematical operation
You can invoke mathematical methods in any number type through the infix operator, plus sign (+), minus sign (-), multiplication sign (*), division sign (/), and remainder (%). Here are some examples:
Scala > 1.2 + 2.3 res6: Double = 3.5 scala > 3-1 res7: Int = 2 scala >'b' -'a 'res8: Int = 1 scala > 2L * 3L res9: Long = 6 scala > 11 / 4 res10: Int = 2 scala > 11% 4 res11: Int = 3 scala > 11.0f / 4.0f res12: Float = 2.75 scala > 11.0% 4.0 res13: Double = 3.0
When the left and right operands are of integer type (Int,Long,Byte,Short, or Char), the / operator will return the integer part of your quotient, removing the remainder. The% operator indicates its remainder.
The part of the floating point remainder obtained with the% symbol does not follow the definition of the IEEE754 standard. IEEE754 uses rounding division instead of truncated division when calculating the remainder, so the calculation of the remainder is very different from the remainder operation of an integer. If you do want the remainder of IEEE754, you can call IEEEremainder in scala.Math, for example:
Scala > Math.IEEEremainder (11.0,4.0) res14: Double =-1.0
The number type also provides the unary prefix + and-operators (methods unary_+ and unary_-), which allow you to indicate whether the text number is positive or negative, such as-3 or + 4.0. If you do not specify a unary + or -, the text number is interpreted as positive. The unary sign + also exists only to coordinate with the unary symbol -, but it has no effect. Unary symbol-can also be used to make a variable negative. Examples are as follows:
Scala > val neg = 1 +-3 neg: Int =-2 scala > val y = + 3 y: Int = 3 scala >-neg res15: Int = 2
Relationship and logical operation
You can use the relational method: greater than (>), less than (
< ),大于等于(>=) and less than or equal to (
< =)比较数类型,像等号操作符那样,产生一个Boolean结果。另外,你可以使用一元操作符!(unary_!方法)改变Boolean值。以下是一些例子: scala>1 > 2 res16: Boolean = false scala > 1
< 2 res17: Boolean = true scala>1.0
< = 1.0 res18: Boolean = true scala>3.5f > = 3.6f res19: Boolean = false scala >'a'> ='A' res20: Boolean = true scala > val thisIsBoring =! true thisIsBoring: Boolean = false scala >! thisIsBoring res21: Boolean = true
Logical methods, logical and (&) and logical OR (| |), take Boolean operands in an infix manner and produce Boolean results. Such as:
Scala > val toBe = true toBe: Boolean = true scala > val question = toBe | |! toBe question: Boolean = true scala > val paradox = toBe & &! toBe paradox: Boolean = false
As in Java, logic and logic may have a short circuit: the concept of short-circuit: expressions built with these operators evaluate only the parts that at least determine the outcome. In other words, the right-hand part of the logic and the logic or expression is no longer evaluated when the left-hand part can determine the result. For example, if the left-hand side of the logic and expression evaluates to false, then the result of the expression is destined to be false, so the right-hand part is no longer evaluated. Similarly, if the left-hand part of the logic or expression evaluates to true, then the result of the expression must be true, so the right-hand part is no longer evaluated. Here are some examples:
Scala > def salt () = {println ("salt"); false} salt: () Boolean scala > def pepper () = {println ("pepper"); true} pepper: () Boolean scala > pepper () & & salt () pepper salt res22: Boolean = false scala > salt () & & pepper () salt res23: Boolean = false
Of the * expressions, both pepper and salt are called, but in the second, only salt is called. Because salt returns false, there is no need to call pepper.
Be careful
You might want to know how the short-circuit mechanism works if the operators are just methods. Usually, all parameters are evaluated before entering the method, so how can the method choose not to evaluate its second parameter? The answer is that all Scala methods have settings that delay or even cancel the evaluation of their parameters.
Bit operator
Scala allows you to use several bit methods to operate on a single bit of an integer type. There are: bitwise and operational (&), bitwise or operational (|) and bitwise XOR (^). Perform mutually exclusive or: exclusive or operations on its operands by bit XOR method. Consistent bits produce 0. The bits of the difference produce 1. So 0011 ^ 0101 produces 0110. The unary complement operator (~, method unary_~) reverses each bit of its Operand. For example:
Scala > 1 & 2 res24: Int = 0 scala > 1 | 2 res25: Int = 3 scala > 1'3 res26: Int = 2 scala > ~ 1 res27: Int =-2
* expressions, 1 & 2, and compute each bit of 1 (0001) and 2 (0010), and produce 0 (0000). The second expression, 1 | 2, performs or operates on each bit of the same Operand and produces 3 (0011). The third expression, 1 ^ 3, each bit of XOR 1 (0001) and 3 (0011), produces 2 (0010). The expression of * *, ~ 1, converts every bit of 1 (0001), resulting in-2, and the binary looks like 1111 1111 1111 1110.
The Scala integer type also provides three displacement methods: move left (
< < ),右移(>>) and unsigned right shift (> >). When using the infix operator mode, the displacement method moves the integer on the left bit by bit according to the number of integer values specified on the right. Move left and unsigned right fill in zero when you move. Moving to the right fills in the * * bits (symbol bits) of the left integer when you move. Examples are as follows:
Scala >-1 > > 31 res38: Int =-1 scala >-1 > 31 res39: Int = 1 scala > 1
< < 2 res40: Int = 4 二进制的-1是1111 1111 1111 1111 1111 1111 1111 1111。***个例子里,-1 >> 31maxim 1 has been shifted to the right by 31 bits. Since Int includes 32 bits, this operation actually moves the leftmost bit to the far right. The leftmost bit of a numeric type is the symbol bit. If the leftmost bit is 1, the number is negative; if it is 0, the number is positive. Because the > > method is constantly moving to the right, the leftmost bit of 1 is 1, so the result is exactly the same as the original number on the left, 32 bits of 1, or-1. In the second example,-1 > > 31, the leftmost bit keeps moving to the right again to the rightmost position, but this time it is 0. So this time the result is 0000 0000 0000 0001, or 1. In one example, 1 < 2, the left Operand, 1, is moved 2 positions to the left (fill in 0), resulting in a binary 0000 0000 0000 0100, or 4.
This is the end of the content of "what is the relationship and logical operation of Scala". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.