Blog

Learn about Javascript Strings

We will learn how to create Strings, escaping quotes and string concatenation.

Code examples below are also available at Github

Creating a String
Strings are surrounded by quotation marks. It can be single or double quotes.
Just like we declare and initialize numbers we can work with strings.

let message = 'Hello World';
message

Here message is a string literal.

Escaping Characters in a String
Let’s say we need to use the following string – 'I'm at the store right now'. As you can see here we have a single quote in the string itself. This won’t work.
To handle this we can use the \ symbol.
let message = 'I\'m at the store right now'
Using \’ solves the problem.

Joining two strings
To join or concatenate two strings we can use + operator.
let str1 = 'Hello';
let str2 = 'Sam';

str1+' '+str2

To convert a string to a number you can use the Number() function.
To convert a number to a string you can use toString()

Example
let numStr = '922';
typeof(numStr)

let num = Number(numStr);
typeof(num)

let tempStr = num.toString();
typeof(tempStr)

Template Literals or Template Strings
This is a new addition. Using this it’s convenient to concatenate strings or print out a variable value.
We use backtick instead of quotes.
let fName = 'Raj';
let message = Check this out. My name is ${fName};
message

Learn about Numbers and Operators in Javascript

NUMBERS AND OPERATORS

Github Code – https://github.com/ankur-srivastava/javascript-lectures/blob/master/Lecture3/index.js

Usually we deal with three types of numbers –

  1. Integers like 3, 34
  2. Floating Point like 3.12, 93.34
  3. Doubles like 1.3452345

JS has only one data type for numbers called Number.

Let’s declare two numbers

let num1 = 32;
let num2 = 44;

Arithmetic Operators

With JS you have all the mathematical operators available to perform various operations.
Like add, substract etc.

2+3
4/2
4%2

We use operator precedence rules when evaluating a complex expression

let x = 3+2*6/2

It’s good to use parenthesis.

(3+2)*(6/2)

Increment Operators using Postfix operator

let x = 4;
x++
x

Comparison Operators

If you want to check that two values are identical you can use === operator
Similarly to check if they are not equal you can use !==

=== compares the data type also, in addition to it’s value. == does not.

Other frequently used comparison operators include : <, >, <=

Mostly in conditional statements like if-else
if(x > y)

Learn about Variables in Javascript

If you have done any programming you will be at home with variables. If you haven’t then don’t worry it’s quite easy to understand.

Variables are used to store values. Value can be a string, a number or other forms of data.

Code - https://github.com/ankur-srivastava/javascript-lectures/blob/master/Lecture2/index.js

Let’s understand this with an example. You may have seen websites that ask you to enter your first name etc. Now to store that name we need a variable.

let firstName = 'Ankur';

Here firstName is a container that stores Ankur.

Declare a Variable
To use variables you need to declare them. We use keywords like let, var or const to declare variables. const is used to declare variables whose values do not change.

let age = 30;
const color = 'red';

What happens if you just write

let day;
console.log(day)

It gives undefined, since we dint assign any value.

Initialize a Variable
Initialize means assigning a value to a variable.

let day = 'Tuesday';
console.log(day)

Why you should use let instead of var ?

With var you can declare a variable after initializing. This is known as hoisting. And is not good. You cannot do the same thing with let.
Another thing that works with var and not let is that with var you can declare a variable multiple times.

var fName = 'Ankur';
var fName = 'Sid';

For these reasons it’s not advisable to use var.

A variable declared using let or var can be updated.

let name = 'Jack';
name = 'Sid';

Variable Naming Rules

1. Don’t use numbers at the start of variables. This is not allowed.
2. Variable names are case sensitive.
3. Using camelCase is safe and recommended.
4. Variable names should be intuitive.
5. Don’t use the reserved words. That is words that are part of JS syntax. Like let, const, function etc.

Variable Types

You don’t need to specify variable types in JS. It’s a dynamically typed language, quite similar to Python. In other languages like C or Java you may have seen various data types being used like int, long, boolean, float etc. Not needed with JS.

You can store numbers, strings, booleans, arrays and objects.

Let’s see some examples:

let message = 'Hello all';
let age=32;
let flag = false;
let a = [10, 12, 32]

An introduction to Javascript

How Javascript was born ?

In 1993 the first web browser with a GUI was released. It was called Mosaic. Due to it’s GUI it made www quite popular.

It’s lead developers then founded Netscape which released an enhanced version called Netscape Navigator. The pages were static though. They lacked interaction.

To make the pages more interactive Netscape needed a scripting language. To do this they hired Brendan Eich. Same guy who later co-founded Mozilla project. His job was to put this language “in the browser”. The first version was completed in 10 days. It was called Mocha. Then renamed to Livescript and subsequently to Javascript.

What exactly is Javascript ?

Javascript is an interpreted or just in time compiled programming language. What this means is that the program you write is compiled at run time.
Unlike Java where you need to first compile the program and then run the generated byte code.

It is one of the core technologies of the World Wide Web (www) along with HTML and CSS. You can build interactive websites using JS. When you see some animations like page elements fading in and out or resizing then it’s more or less the work of JS.

All major web browsers have a dedicated JS engine which can execute the JS code.

Most of the websites you see use Javascript to handle client side behavior. But with the advent of technologies like Node JS we can now write code in JS which can be executed on Server side.

Why should you learn Javascript ?

To build any modern day website Javascript is required. And now JS can be used to build backend, like APIs. So whether you want to work for a company as a developer or you plan to build your pwn websites you will need JS.

Code – https://github.com/ankur-srivastava/javascript-lectures/tree/master/Lecture1

PPT – Introduction to JavaScript from Ankur Srivastava

What are checked and unchecked Exceptions in Java?

What are Checked Exceptions?

If you write a piece of code that is logically correct but can fail in certain scenarios then you need to handle those scenarios. So for example if you write code which reads a File and suppose at run time the File is not available then it will throw an Exception. So you can handle this by catching a FileNotFoundException.

What are Runtime Exceptions?

Suppose you write a piece of code which is logically not correct. Say you write code that does not use an API correctly then your code is buggy. So no point handling exception here. Let the error be printed in the log so that you can make corrections to the code.

The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.

What are Errors?

When the exception is beyond your application’s scope. Like some hardware issue. Say at runtime some I/O error occurs.

Both Runtime and Error are unchecked. You don’t need to handle them.

Top 5 features added to Java SE 9

Background

Java has been evolving at a good pace ever since it was introduced in early 90’s. With every passing year and each release it has tried to keep pace with emerging market demands.Java has been evolving at a good pace ever since it was introduced in early 90’s. With every passing year and each release it has tried to keep pace with emerging market demands.

Let’s take a look at the top 5 features added to Java 9

1. Modules
The result of Project Jigsaw, modules are the most important addition to Java 9.

  • Java is now modular which means the library packages are now organized as modules.
  • It changes the way we design and build Java Applications.
  • It is different from OSGi.
  • It helps Programmers to be more productive.
  • Managing and evolving modules is easier.

2. JShell : Read-Eval-Print loop for Java
A tool that allows you to develop and test Java code interactively.

Earlier:

class Sample{
	public static void main(String[] a){
		System.out.println("Hello World");
	}
}

And now using JShell you can print Hello without writing other stuff.
You can type JShell at command line to get started.
And write

System.out.println(“Hello !!”);

3. Private Methods in Interfaces

You can now add private methods to your Interfaces. This can help you to create  reusable code.

Example:

public interface Sample {

private void printMyStuff(String abc){

System.out.println(abc);

}

}

4. Collections
Earlier you would write

List<Employee> empList = new ArrayList<>();

empList.add(new Employee(1, “Ankur”));

empList.add(new Employee(2, “Rohan”));

Now you can write

List<Employee> empList = List.of(new Employee(1, “Ankur”), new Employee(2, “Rohan”));

Reason – Thanks to changes in Java 8. Interfaces can now have default methods.

5. Concurrency Updates

Java 8 introduced Streams. Java 9 has gone a step ahead and added

  • Reactive Streams publish-subscribe framework
  • Enhancements to CompletableFuture class which was introduced in Java 8.

Other Notable Changes

HTTP Client API to support HTTP/2 and WebSocketThe new incubator HTTP Client is capable of working with both HTTP/1.1 and HTTP/2

 

3 Key Updates in Java SE 8

What is new in Java 8?

Java has been evolving at a good pace since it was introduced in early 90’s. With every passing year it has tried to keep pace with emerging market demands.

Let’s take a look at 3 key updates introduced in Java 8:
1. Lambda Expression  

Lambda Expression adds functional programming features to Java. It changes the way programming solutions are conceptualized and the way code is written in Java. It reduces the amount of code needed to create certain constructs like Anonymous Classes.

2. Stream API

The Stream API supports pipeline operations on data. Stream API is designed with Lambda Expression in mind.Powerful way to handle data in an efficient way.You can perform very sophisticated operations that search, filter or manipulate data. Plus it can be done in Parallel.

3. Interface Methods

Another Lambda inspired feature is the ability to define a default implementation for a method in the Interface. This feature helps you to update the codebase without impacting the classes that implement an interface.

Other new features

1. New Date and Time API

2. Ability to use parallel processing when sorting an Array.