Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What expressions does Scala have?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what expressions does Scala have". In the operation of actual cases, many people will encounter such a dilemma. Then 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!

I. conditional expression

If else can return a value, if (x > 0) 1 else-1, and the return value of this expression is of type Int,-1 or 1. So you can write val I = if (x > 0) 1 else-1. In fact, I prefer to write if (x > 0), I prefer to use val, the former is more functional and val is used, and the latter is more instructive and must be var.

2. Unit,if (x > 0) 1 if the return value is 1 if x > 0, this is no problem, but what about x < 0? This expression does not return a value, it doesn't matter, there is a Scala.Unit in Scala, write (), which is equivalent to void.

Third, with regard to semicolons, scala can write but not write, but there is a situation that must be written, one line to write multiple statements, in fact, do not know this matter, why do you have to write multiple statements in one line.

IV. Block expression

There is something similar in java, but the difference is that scala also has a return value, the return value is x + y, and the last sentence is x + y, which is incomprehensible in java, but in scala, this is equivalent to the return value of this block expression.

Val res = {val x = 10; val y = 20; x + y}

Fifth, there is no obvious difference between While,scala and java. As far as I understand it, while is a more prescriptive thing and should be avoided as far as possible in scala.

6. For, for (I

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report