Samstag, 3. Oktober 2009

trying Scala ...

install scala, and type scala in console ;)


scala> 4/3.0
res5: Double = 1.3333333333333333

scala> 4/0
java.lang.ArithmeticException: / by zero
at .(:5)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAcce...

scala> var a=3
a: Int = 3

scala> a
res7: Int = 3

scala> a=5
a: Int = 5


scala> val b=3
b: Int = 3

scala> b
res10: Int = 3

scala> b=5
<console>:5: error: reassignment to val
b=5


var and val define variables, val is immutable

For printing is here
Ascala> println(b)
3


operators and control sturctures (if, else, ...) are the same as in java.


scala> val isBook = 6>=3
isBook: Boolean = true

scala> val price=16
price: Int = 16

scala> val vol=10
vol: Int = 10

scala> val sale=if (isBook)price*vol else price/vol
sale: Int = 160

scala> sale
res13: Int = 160

Keine Kommentare:

Kommentar veröffentlichen