Things we discussed

What's an API

API is a interface/enabler that enables different software components to interact with each other. This could be by shared libraries, this could be through over the network communication.

APIs can be language agnostic or dependent. When APIs are provided as toolkits that can be imported into the development environment as libraies then they are called as SDKs

Since the advent of internet a lot of APIs are offered over the network via internet. here are a few ways APIs exchange data Over The internet 1. HTTP -- Using HTTP data a. SOAP - Using HTTP protocol + XML in the body b. REST - Using HTTP protocol + JSON in the body 2. RPC - Using a binary protocol a. GRPC - using protobuf to transmit messages b. XML - using XML to transmit messages c. JSON - using JSON to transmit messages d. Thrift - using (binary/compact/json) to transmit messages 3. WebSocket - using a binary connection but 2 way data streaming data becomes possible.

Understanding APIs via a stock market exampls

Let's say there's an exchange NSE/BSE aka sensex/nifty. They don't really deal directly with end users they have their affiliates aka stock-brokers who take the request of their clients to the exchanges. Now for this to become possible exchanges should have some component in their technological stack that allows for stockbrokers to know details such as price of the stock. Also they should also provide ability for the brokers to place orders on behalf of their clients

Let's explore different kinds of data that's exposed via the exchange to the broker

  1. Live data a. Stock names and prices.
  2. Static data a. Types of orders possible
  3. Actions a. ability to buy or sell orders

Let's take examples and understand what kind of protocol works best in each scenario

  1. Live data - this needs a common connection between the exchange as the prices keep changing very rapidly so regular HTTP connection won't suffice always creating a new request makes the process of getting data slow. So instead we keep a websocket connection open which allows to feed in live data every second if not faster.
  2. Static data - This can be a HTTP request cached via a CDN or a rpc call served by a service.
  3. Action Data - This can be any one of the HTTP/RPC/WebSocket calls.

Control structures

Loops

Allows for a single statement or a series of statements to be repeated.

While - Keeps executing as long as the condition is true

// print numbers from 1 to 10
int i=1;
while(i<=10) {
    System.out.println(i++);
}

Do While - Run Once if the condition is true even after that keep executing till it is true

// print numbers from 1 to 10
int i=1;
do{
 System.out.println(i++);
} while(i<=10) 

For - in the above loops we are initializing the variable and incrementing the variable in seperate new line, imagine if this gets squashed together that's for. This allows for the programmer to write fewer lines of code <3

A for loop contains 3 parts within the initialization paranthesis seperated by a semi-colon unlike while which only holds the conditional statement.

  1. Initialization - allows to initialize one or more variables. seperate by comma to do more
  2. Condition - allows for mentioning the condition which when true allows for the code to run
  3. change-logic - basically after each loop if you want to perform an action you can toss it in here, seperate statements by comma

in a for loop 1 and 3 can be left empty if desired which makes it equivalent to a while loop

for(int i=0,j=10;i<10&j<20;++i,++j){
    System.out.printf("i=%d, j=%d\n", i, j);
}

Conditional statements

If -Used to conditionally execute a statement aka line of code.

Let's see an example that count odd numbers in range 1 to 10

for(int i=1;i<=10;++i) {
    if(i%2!=0) System.out.println(i);
}

If-Else - If else combination allows us to execute a statement when a condition is true and another statement when the condition is false

for(int i=1;i<=10;++i) {
    if(i%2!=0) System.out.println(i);
    else System.out.println("skipping "+i);
}

Else-If -- If we have more than one condition that has to be checked in a manner that is mutually exclusive then we can use else if or switch statements

for(int i=1;i<=10;++i) {
    if(i%2!=0) System.out.println(i);
    else if(i%3!=0) System.out.println("Not a two multiple but a three multiple "+i);
    else System.out.println("skipping "+i);
}

Break -- exits the loop

for(int i=1;i<=10;++i) {
    if(i>3) break; // exits loop when i==4
    if(i%2!=0) System.out.println(i);
}

Continue -- goes to the end of the loop and continues on with executing the next iteration

for(int i=1;i<=10;++i) {
    if(i==3) continue; // skips i==3
    if(i%2!=0) System.out.println(i);
}

Switch - if we want to check a variable agianst multiple values we can use switch instead of else if

int i=2;
switch(i){
    case 1:
        System.out.println("hmm i is 1");
        break;
    case 2:
        System.out.println("hmm i is 2");
        break;
    case 3:
        System.out.println("hmm i is 3");
        break;
    case 4:
        System.out.println("hmm i is 4");
        break;
    case 5:
        System.out.println("hmm i is 5");
        break;
    default:
        System.out.println("hmm i is not between 1 and 5 (inclusive)");
}

Notice the break statement if that's not there the next case statement will get executed.

Named For -- let's say you have inner and outer loops and you want to exit/skip of the outer loop when in inner named loops make it posible

outer:
    for(int i=0;i<10;++i){
        inner:
            for(int i=0;i<10;++i){
                System.out.printf("%d,%d\n",i,j);
                if(i==1) break outer; // exits after printing till i=1,j=1
            }

    }

Things i got wrong / Mis understood

Sorry ! I Don't remember, please raise it if you do

Things We missed

Sorry ! I Don't remember, please raise it if you do