Your SlideShare is downloading. ×
0
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
1.2 scala basics
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

1.2 scala basics

509

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
509
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Scala basics
  • 2. ;
  • 3. Type definitionsScala Javas: String String si: Int int i / Integer i
  • 4. VariablesScala: Java:val s = “Hello World” public final String s = “Hello World”;var i = 1 public int i = 1;private var j = 3 private int j = 3;
  • 5. MethodsScala: Java:def add(x: Int, y: Int): Int = { public int add(int x, int y) { x+y return x + y;} }def add(x: Int, y: Int) = x + ydef doSomething(text: String) { public void doSometing(String text) {} }
  • 6. Methods (2)Scala: Java:myObject.myMethod(1) myObject.myMethod(1);myObject myMethod(1)myObject myMethod 1myObject.myOtherMethod(1, 2) myObject.myOtherMethod(1, 2);myObject myOtherMethod(1, 2)myObject.myMutatingMethod() myObject.myMutatingMethod()myObject.myMutatingMethodmyObject myMutatingMethod
  • 7. Methods (3)Scala: Java:override def toString = ... @Override public String toString() {...}
  • 8. Classes and constructorsScala: Java:class Person(val name: String) public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
  • 9. Traits (= Interface + Mixin)Scala: Java:trait Shape { interface Shape { def area: Double public double area();} }class Circle extends Object with public class Circle extends Object Shape implements Shape
  • 10. No “static” in ScalaScala: Java:object PersonUtil { public class PersonUtil { val AgeLimit = 18 public static final int AGE_LIMIT = 18; def countPersons(persons: List[Person]) = ... public static int countPersons(List<Person>} persons) { ... } }
  • 11. if-then-elseScala: Java:if (foo) { if (foo) { ... ...} else if (bar) { } else if (bar) { ... ...} else { } else { ... ...} }
  • 12. For-loopsScala: Java:for (i <- 0 to 3) { for (int i = 0; i < 4; i++) { ... ...} }for (s <- args) println(s) for (String s : args) { System.out.println(s); }
  • 13. While-loopsScala: Java:while (true) { while (true) { ... ...} }
  • 14. ExceptionsScala: Java:throw new Exception(“...”) throw new Exception(“...”)try { try {} catch { } catch (IOException e) { case e: IOException => ... ...} finally { } finally {} }
  • 15. Varargsdef foo(values: String*){ } public void foo(String... values){ }foo("bar", "baz") foo("bar", "baz");val arr = Array("bar", "baz") String[] arr = new String[]{"bar", "baz"}foo(arr: _*) foo(arr);
  • 16. (Almost) everything is an expressionval res = if (foo) x else yval res = for (i <- 1 to 10) yield i // List(1, ..., 10)val res = try { x } catch { ...; y } finally { } // x or y
  • 17. Collections – ListScala: Java:val numbers = List(1, 2, 3) List<Integer> numbers = new ArrayList<Integer>();val numbers = 1 :: 2 :: 3 :: Nil numbers.add(1); numbers.add(2); numbers.add(3);numbers(0) numbers.get(0);=> 1 => 1
  • 18. Collections – MapScala: Java:var m = Map(1 -> “apple”) Map<Int, String> m =m += 2 -> “orange” new HashMap<Int, String>(); m.put(1, “apple”); m.put(2, “orange”);m(1) m.get(1);=> “apple” => apple
  • 19. GenericsScala: Java:List[String] List<String>
  • 20. TuplesScala: Java:val tuple: Tuple2[Int, String] = Pair<Integer, String> tuple = new Pair<Integer, String>(1, “apple”) (1, “apple”)val quadruple = ... yeah right... ;-) (2, “orange”, 0.5d, false)
  • 21. PackagesScala: Java:package mypackage package mypackage;... ...
  • 22. ImportsScala: Java:import java.util.{List, ArrayList} import java.util.List import java.util.ArrayListimport java.io._ import java.io.*import java.sql.{Date => SDate} ???
  • 23. Nice to knowScala: Java:Console.println(“Hello”) System.out.println(“Hello”);println(“Hello”)val line = Console.readLine() BufferedReader r = new BufferedReader(newval line = readLine() InputStreamRead(System.in) String line = r.readLine();error(“Bad”) throw new RuntimeException(“Bad”)1+1 new Integer(1).toInt() + new Integer(1).toInt();1 .+(1)1 == new Object new Integer(1).equals(new Object());1 eq new Object new Integer(1) == new Object();"""Asregex""".r java.util.regex.Pattern.compile(“Asregex”);

×