Giter Site home page Giter Site logo

learning-scala's Introduction

learning-scala

Starting Scala Basic

Installation

This tutorial is based on windows system.

  1. Step 1 - Verify Java version

    C:\Users\Your_PC_username>java -version

    Output

    java version "1.8.0_111"
    Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
    Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

    If Java is not installed then install latest Java SDK.
    SDK Download

  2. Step 2 - Install Scala
    Scala Download
    Run the downloaded .msi file. Follow below screenshots
    alt text
    Click on the NEXT button
    alt text
    Check the “I Agree Option” and then click NEXT
    alt text
    Click on the INSTALL button.
    alt text
    Click on the FINISH button
    alt text

  3. Step 3 - Test Installation

    C:\Users\Your_PC_username>java -version

    alt text

Declare variable and types

Immutable Variable

Meaning - values which can not be changed
Declaration - val :scala:Type = <some_Literal>
Example - val name:String = 'Scala'
Note - Reassignment is not possible.


Mutable variable

Meaning - value can be changed
Declaration - var :scala:type=<some_literal>
Example - var name:String = 'Scala'


Lazy Initialization
Meaning - Call by need means expression is not evaluated until it is needed
Declaration - lazy val = <some_literal>
Pros

  1. Optimize computation, avoid wasted of CPU
  2. resolve circular dependencies

Cons

  1. Tricky in finding bugs
  2. Goes against conventional approach

Blocks
Meaning - Combine expression by surrounding them {}. Last value will be evaluated in this case.
Declaration - println({ val x =1+23; x+1})


Functions
Meaning - Expression which have parameters, and take arguments
Declaration - val multipleBythree = (x:Int) => x*3


Method
Meaning - Similar to functions but method is always initialized with keyword def and wil always return a value
Declaration - def multipleBythree(x:Int):Int = x*3


Type Hierarchy

alt text

What is Nothing in bottom

It is a subtype of everything of higher types. Commonly used to signal non-termination such as exception thrown, exit program or infinite loop.

Type Casting

alt text


Class
Meaning - A Scala class is defined with keyword class along with name and constructor parameters
Declaration -

class name(firstName:String,lastName:String){
    def fullName(greet:String):Unit =
      println(greet+' '+firstName+' '+lastName)
  }
  val textPrint = new name("Adarsh","Shukla")
  textPrint.fullName("Welcome to Canada!!!!!")
}

learning-scala's People

Contributors

adarshshkl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.