Monday, 24 October 2016

SCALA BASICS DAY 1 Practice on 24 Oct 2016

Scala Day 1 Practice:
==============================================
orienit@kalyan:~$ scala
Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.8.0_66-internal).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 


scala> val id = 1
id: Int = 1

scala> val id : Int = 1
id: Int = 1

scala> val id : Long = 1
id: Long = 1

scala> val id = 1l
id: Long = 1

scala> val id = 1d
id: Double = 1.0

scala> val id = 1f
id: Float = 1.0

scala> val id : Float = 1
id: Float = 1.0

scala> val id : Double = 1
id: Double = 1.0

-------------------------------------------------

scala> val id = 1
id: Int = 1

scala> id = 2
<console>:11: error: reassignment to val
       id = 2
          ^

scala> var id = 1
id: Int = 1

scala> id = 2
id: Int = 2

-------------------------------------------------

Note: 
1. In scala we don't have any Primitive data types like java
2. In scala every thing is `Object`
3. Operator overloading is avialble in Scala, C++ , but not in Java

-------------------------------------------------

scala> 1
res0: Int = 1

scala> "kalyan"
res1: String = kalyan


scala> 1 + 1
res2: Int = 2

scala> 1.+(1)
res3: Int = 2

scala> 1 + 20.0
res4: Double = 21.0

scala> 1.+(20.0)
res5: Double = 21.0

scala> 10 - 5
res6: Int = 5

scala> 10.-(5)
res7: Int = 5


-------------------------------------------------

scala> val id = 1
id: Int = 1

scala> id.
%   -    >>             isInstanceOf   toFloat   toString   |   
&   /    >>>            toByte         toInt     unary_+        
*   >    ^              toChar         toLong    unary_-        
+   >=   asInstanceOf   toDouble       toShort   unary_~  


-------------------------------------------------

scala> val name = "kalyan"
name: String = kalyan

scala> name.
+                     endsWith             replace        
asInstanceOf          equalsIgnoreCase     replaceAll     
charAt                getBytes             replaceFirst   
chars                 getChars             split          
codePointAt           indexOf              startsWith     
codePointBefore       intern               subSequence    
codePointCount        isEmpty              substring      
codePoints            isInstanceOf         toCharArray    
compareTo             lastIndexOf          toLowerCase    
compareToIgnoreCase   length               toString       
concat                matches              toUpperCase    
contains              offsetByCodePoints   trim           
contentEquals         regionMatches                       

-------------------------------------------------

scala> val id = 1
id: Int = 1

scala> id.toByte
res8: Byte = 1

scala> id.toChar
res9: Char = ?

scala> id.toLong
res10: Long = 1

scala> id.toDouble
res11: Double = 1.0

scala> id.toFloat
res12: Float = 1.0

scala> id.toString
res13: String = 1


-------------------------------------------------
String Interpolation
-------------------------------------------------
val name = "kalyan"
val course = "hadoop"
val location = "hyderabad"
val pincode = 500032
val count = 100


scala> val name = "kalyan"
name: String = kalyan

scala> val course = "hadoop"
course: String = hadoop

scala> val location = "hyderabad"
location: String = hyderabad

scala> val pincode = 500032
pincode: Int = 500032

scala> val count = 100
count: Int = 100


scala> println("Name is " + name + ", Course is " + course)
Name is kalyan, Course is hadoop

scala> println("Name is $name, Course is $course")
Name is $name, Course is $course

scala> println(s"Name is $name, Course is $course")
Name is kalyan, Course is hadoop




scala> println(s"Name is $name, Count is $count")
Name is kalyan, Count is 100

scala> println(f"Name is $name, Count is $count")
Name is kalyan, Count is 100

scala> println(f"Name is $name, Count is $count%.2f")
Name is kalyan, Count is 100.00

scala> println(f"Name is $name, Count is $count%.5f")
Name is kalyan, Count is 100.00000

scala> println(s"Name is $name, Count is $count%.5f")
Name is kalyan, Count is 100%.5f




scala> println(s"Name is $name\nCourse is $course")
Name is kalyan
Course is hadoop

scala> println(raw"Name is $name\nCourse is $course")
Name is kalyan\nCourse is hadoop

-------------------------------------------------
Java:
----------
String[] names = new String[10];

String[] names = {"anil", "raj", "venkat"};



Scala:
----------
val names = new Array[String](10)

val names = Array[String]("anil", "raj", "venkat")

val names = Array("anil", "raj", "venkat")


scala> val names = new Array[String](10)
names: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)

scala> val names = Array[String]("anil", "raj", "venkat")
names: Array[String] = Array(anil, raj, venkat)

scala> val names = Array("anil", "raj", "venkat")
names: Array[String] = Array(anil, raj, venkat)

scala> names(0)
res25: String = anil

scala> names(1)
res26: String = raj

scala> names[1]
<console>:1: error: identifier expected but integer literal found.
names[1]
      ^

-------------------------------------------------



Share this article with your friends.
Related Posts Plugin for WordPress, Blogger...