Thursday, 10 November 2016

Difference between Instance and Object in Java

Difference between class object and class instance in java

               The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Object and Instance in Java
Difference between object and instance in Java

It is obvious to get confused by the words class object and class instance. A class is a template for an object and an object is an instance of a class. To make it clear, we can also say that the instance of a class is object.

There is no need of differentiating the words object and instance as both doesn’t make any difference when is used interchangeably.

For Example:

class Animal

Object of class Animal and instance of class Animal are same. When you create an instance of the class animal, the instance is an object and the type of that object is “class Animal”.

Sunday, 2 October 2016

Javascript Date

The Javascript Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

Simply for displaying dates its like below:
document.getElementById("demo").innerHTML = Date();

The important methods of date object are as follows:

getFullYear()returns the year in 4 digit e.g. 2015. It is a new method and suggested than getYear() which is now deprecated.
getMonth()returns the month in 2 digit from 1 to 31.
getDate()returns the date in 1 or 2 digit from 1 to 31.
getDay()returns the day of week in 1 digit from 0 to 6.
getHours()returns all the elements having the given name value.
getMinutes()returns all the elements having the given class name.
getSeconds()returns all the elements having the given class name.
getMilliseconds()returns all the elements having the given tag name.

Current date example:
var today=new Date();

Current Time example:
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();

Sunday, 12 June 2016

Javascript - String

JavaScript strings are used for storing and manipulating text.It is an object that represents sequence of characters.

A JavaScript string simply stores a series of characters like "John Smith".
A string can be any text inside quotes. You can use single or double quotes.

var name= "Bob";
var name= 'Bob';

String length
Below is the code for find the length of  a string

var name = "John Smith";
var val = name.length;

JavaScript String Methods
Let's see the list of JavaScript string methods with examples.

  • charAt(index)
  • concat(str)
  • indexOf(str)
  • lastIndexOf(str)
  • toLowerCase()
  • toUpperCase()
  • slice(beginIndex, endIndex)
  • trim()
charAt ()

var str="hellowworld";

output: o


var str1="hello";
var str2="world";
var str3=str1.concat(str2);

output: hello world


var str1="helloworld from javascript";
var num=str1.indexOf("from");

output: 11


var str1="hellowworld from javascript";
var num=str1.lastIndexOf("java");

output: 16


var str1="JavaScript toLowerCase Example";
var str2=str1.toLowerCase();

Output: javascript tolowercase example


var str1="JavaScript toUpperCase Example";
var str2=str1.toUpperCase();



var str1="abcdefgh";
var str2=str1.slice(2,5);

output: cde


var str1="     javascript trim example    ";
var str2=str1.trim();

output: javascript trim example

Wednesday, 8 June 2016

Javascript Array

JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
  1. By array literal
  2. By creating instance of Array directly (using new keyword)
  3. By using an Array constructor (using new key

1) JavaScript array literal

Below syntax is creating an array.
var arrayname=[value1,value2.....valueN];
Eg: var cars = ["Hyundai", "Volvo", "BMW"];

2) JavaScript Array directly (new keyword)

The syntax of javascript array using new keyword is as below.
var arrayname=new Array();  
Eg: var cars = new Array("Hyundai", "Volvo", "BMW");

3) By using array constructor

Here, you need to create instance of array by passing arguments in constructor .

var arrayname=new Array("param1","param2","param3")
var emp=new Array("Hyundai","Volvo","BMW")