Latest Posts

Do you know about Let,Const and Var on javascript?

LET US TRY AND LEARN


 let, const, and var are three keywords for declaring variables in Javascript.

1. let

Scope: 

Block-scoped. 

  • A variable declared with let is only accessible within the block (code enclosed in curly braces {}) where it's declared.
  •  This is crucial for preventing accidental variable reassignment from outer scopes.

Reassignment: 

  • Allowed. You can change the value stored in a let variable after it's declared.

Temporal Dead Zone (TDZ): 

  • Variables declared with let exist in a TDZ before their declaration, meaning you cannot access or modify them until they are fully declared. 

This is in contrast to var and const hoisting behavior.

Example:

if (true) {

  let foo = 10; // Accessible only within this block

  console.log(foo); // Output: 10

}

console.log(foo); // ReferenceError: foo is not defined (TDZ)


2. const:


Scope: 

  • Block-scoped, similar to let.

Reassignment: 

  • Not allowed. Once you assign a value to a const variable, you cannot modify it later. 
  • This helps prevent unintended changes and potential bugs.

Hoisting: 

  • Not hoisted. const declarations are not moved to the top of their scope like var declarations. They must be declared before use.

Example:

const foo = 111;  // Declaring a constant value

foo = 333; // TypeError: Assignment to constant variable


3. var:

Scope: Function-scoped. 

  • A variable declared with var is accessible throughout the function where it's declared, even within nested blocks. 
  • This can be problematic for large codebases as it can lead to unintended side effects and variable naming conflicts.

Reassignment: 

  • Allowed, like let.

Hoisting: 

  • Hoisted. var declarations are moved to the top of their scope (function or global scope) during compilation, making them accessible before their declaration. 
  • This can be confusing and lead to unexpected behavior if not used carefully.

Example:

for (var i = 0; i < 3; i++) {
  console.log(i); // Output: 0, 1, 2
}

console.log(i); // Output: 3 (accessible even outside the loop due to hoisting)

Best Practices:

  • In modern JavaScript development, it's strongly recommended to use const and let instead of var due to their improved scoping behavior and reduced risk of unintended variable reassignment.
  • Prefer const for variables that should remain constant throughout the code, and use let when you need to reassign values. 


Note:


const id=6767676

let name='abcd'

var pwd="886876"

city="Bangalore"


1.id=10 //not allowed

2.name="bgbg"

3.pwd="78787"

4.city="hyd"


Prefer not to use var because there was an issue with the block-scoped and functional scope, we use let instead of var.



What happens when we type python Hello.py in cmd?

LET US TRY AND LEARN

1. Parsing the Command:

  •   1st the command is split into two parts: python and Hello.py.
  •   The Python interpreter recognizes the first part (python) as an executable.
  •  The second part (Hello.py) is interpreted as the name of the Python script to be executed.

2. Finding the Script:

  • The Python interpreter searches for the script file Hello.py in the current working directory (the directory you're currently in at the command prompt).

3. Compilation :

  • When you execute a Python script (e.g., python Hello.py), the Python interpreter first compiles the source code into bytecode. Bytecode is a low-level, platform-independent representation of your Python code.
  • The bytecode generated by the Python compiler is often hidden from the user, and stored in .pyc (Python compiled) files. These files contain the bytecode representation of your Python scripts.

4. Python Virtual Machine (PVM) Execution:

  • The Python interpreter initiates the Python Virtual Machine (PVM).
  • If Hello.py is compiled to bytecode, the PVM fetches bytecode instructions from the file.
  • It decodes and executes them one by one, performing operations like calculations, calling functions, and printing output.
  • Source Code Execution (if not compiled to bytecode):
  • If Hello.py is not compiled to bytecode, the PVM directly parses and converts the source code into bytecode on the fly.
  • This bytecode is then executed similarly to the compiled version.

5. Running the Script:

  • The PVM executes the script's code sequentially, line by line.
  • If the script contains any print statements, the corresponding output will be displayed in the Python prompt.

6. Script Completion and PVM Cleanup:

  • Once the entire script finishes execution, the PVM releases any resources it was using.
  • Control returns to the Python prompt, ready for further commands.

7.__pycache__ Directory: 

  • When you run a Python script, if the interpreter detects that the corresponding .pyc file is missing or outdated, it generates a new bytecode file and stores it in a directory named __pycache__. 
  • This directory helps improve startup time and avoid unnecessary recompilations.


8. Naming Convention of .pyc Files: 

  • The .pyc files follow a naming convention that includes the name of the Python script (hello_python in your example), the Python implementation (cpython for the reference implementation), and the Python version (e.g., 311 for Python 3.11).
  •  This naming convention helps differentiate bytecode files for different scripts and Python versions.



Notes:-

when we type > python Hello.py

1. The Python software/compiler compiles to Byte code. Which is a low-level code and Platform Independent.

2. The byte code is basically hidden most of the time. it is also known as  frozen Binaries

3. when we install Python software we install the Python virtual Machine which is known as Python VM.

4. This Python VM runs the byte code. Byte Code runs faster  

5. when we run the file that has imported files, it generates a folder called __pyche__

and a file named hello_python.cpython-311.pyc

6.hello_python.cpython-311.pyc

hello->file name

cpython->python type

311->version





JavaScript

LET US TRY AND LEARN
What is Javascript?

Javascript is a programming language, which is used to make the webpage interactive with the user.
This scripting language interacts with the different elements of the web pages.


Main Components Of Javascript
 
There are three main components of Javascript

1. ECMAScript
2. Document Object Model (DOM)
3. Browser Object Model(BOM)


What is ECMAScript?

ECMA stands for European Computer Manufacturers Association.

ECMAScript is a general-purpose scripting language that has been built on the standard of ECMA-262.
This has been done to standardize the Javascript.
Javascript is the implementation of the ECMAScript and all web browsers have to adhere to ECMAScript while creating their Javascript Engine and runtime environment.


What is DOM(Document Object Model)?

DOM  Provides interfaces for interacting with elements on web pages.
Because of the DOM Javascript is able to create Dynamic HTML and a developer can manipulate different nodes of the Document. 


Developers can use various methods provided by the DOM to alter the HTML document.

What is BOM(Browser Object Model)?

BOM provides the API for interacting with the web browser.
The BOM can be accessed by using the window object which represents the browser window.

Few Examples of BOM API

Opening and closing of the window.

Resizing of the window.

Accessing the cookies

Getting the history of the browser.

 

History Of Javascript.

Javascript was developed by the Netscape developer named Brendan Eich in 1995.
Firstly it was named as Mocha and later changed to LiveScript.

Later again Livescript name got changed to JavaScript.

So earlier JavaScript was supported by Netscape Navigator. In the meantime, Microsoft has its own scripting language called Jscript and it was supported by Internet Explorer.

There were no standards between Jscript and JavaScript earlier. Later the community decided to standardize the language.


JavaScript is evolving with each passing day. We will learn those in the coming tutorials.










Quarkus Hello World Program

LET US TRY AND LEARN
How To Write HelloWorld Program in Quarkus

We can develop our 1st quarkus application using 2 ways.

1. Using Commandline.
2. Generating through https://code.quarkus.io/

Here we will use the 2nd approach ie using https://code.quarkus.io/




Fill group ,artifact and select the build tool.

Click on more options.

Select Java Version.


Check the checkbox RESTEasy Reactive.

Click on Generate your Application.

Click on Download The ZIP on the popup.

You will get the zip file. Extract it.


Open with any IDE.

I am using Intlej as the IDE here.

Navigate to the GreetingResource Class. This is the default class provided by the generator.



package org.letustryandlearn;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello from RESTEasy Reactive";
    }
}


Run the application. By default, it will run on the 8080 port.


You can see the Congratulations! page.

Click on hello. (http://localhost:8080/hello). You can see the response as "Hello from RESTEasy Reactive"

If you get the error as the port is already used then you can change the port in the application.properties by adding 
quarkus.http.port=9090

Run the application.




http://localhost:9090/hello

Change the code.  

package org.letustryandlearn; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello World"; } }






Run the application .
 Hit the url http://localhost:9090/hello You will get the response as "Hello World".



Spring Boot Hello World Program

LET US TRY AND LEARN

Spring Boot is built on top of the Spring Framework. It makes easier and quicker to develop, setup, and run the Spring Boot applications.

Spring Boot is  the combination of Spring Framework and Embedded Servers.

Spring Boot has no requirement of XML configuration.



We can develop our 1st HelloWorld program using the Spring Boot Initializer.

Follow the below Steps To write  the HelloWorld program using Spring Boot:-


Please visit  https://start.spring.io/  .




Choose Java, Maven, and Spring Boot version.

Provide the Group and Artifact Name.

Select Package JAR

and Java -11

Click on add dependency and search WEB


Select Spring Web

Click on the Generate


A zip file will get downloaded. Extract the file.

Open it with any IDE I am using IntelliJ Idea For this project

and run the application.


Spring Boot Use 8080 port by default. You may have to change the port if it is already been used on your PC.

 let's write a class called HelloWorld.



Run the application again.

Code Details.


package com.letustryandlearn.HelloWorld;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping( path = "/helloworld")
public class HelloWorld {
    @GetMapping(path = "/hello")
    public String getMessage() {
        return "welcome to spring boot";
    }
}

You can hit the URL http://localhost:9090/helloworld/hello
This completes our HelloWorld program using Spring Boot.

Multitasking, Multiprocessing and Multithreading

LET US TRY AND LEARN

 



Multitasking 

Performing multiple tasks at a single time is called multitasking.

example:- A pc we can run various applications simultaneously, like when we are browsing, we can play music also.

It increases performance.

There are two ways to achieve the Multitasking

1. Process-based multitasking (Multiprocessing)

2. Thread-based multitasking (Multithreading)


Multiprocessing:

When a system a connected to multiple CPUs to complete a task is called MultiProcessing.

It is suitable for system-level or Operating System (OS) level


Multithreading :

Multiple small tasks execute simultaneously, So each small task is called a Thread.

Multithreading is suitable at the programming level.

Java provides predefined APIs(Application programming interface) for Multithreading 

Below are the predefined APIs provided by java:

Thread,

 Runnable,

ThreadGroup,

Concurrency,

Threadpool,

etc.