Monday, 27 July 2015

new operator in java

The 'new' operator in java is responsible for the creation of new object or we can say instance of a class.
Actually, it dynamically allocates memory in the heap with the reference we define pointed from the stack.
The dynamically allocation is just means that the memory is allocated at the run time of the program. The advantage of this approach is that your program can create as many or as few objects as it needs during the execution of your program.

For example :
Before creating a new object of the class we create the 'reference' variable of the class as

Demo obj;
(Where Demo is the class whose object we want to create)

Now this statement only creates a reference variable of a class or the "Demo" class data type.
There are data types such as integers, characters etc. but we don't need to create these with the help of
new operator because they are not implemented as objects in java rather they are implemented as normal variables because of the efficiency. Since all objects have some properties and behavior and require java to treat them differently than it treats the simple types.
Object version of these data types is also present with the help of wrapper classes which concept is not needed yet.
All the reference variables lies in the stack of the memory and hence 'obj' is .
Currently the reference "obj" is pointed to the null or it is not pointing to any object.

If we use the new operator than the actual or physical creation of the object is occurred.
The statement is :
obj = new Demo();

This statement does two things :
1. It creates the actual or physical object in the heap with the variable 'obj' pointing to the object from the stack.
2. It calls the constructor of the "Demo" class for the initialization of the object.


Basically, the "Demo()" in the statement is the default constructor.
A constructor defines what occurs when an object of the class is created.
Most real-world classes explicitly defines their own constructors within their class definition and if no explicit constructor is specified in the class then java will automatically supply a default constructor.

Also the two statements can be combined into one statement as :
Demo obj = new Demo();

Now at this time the 'obj' which has points to the actual object of the Demo class can call any not restricted methods.
Consider the case, when we declare another reference of the class and equating that variable with the previous reference i.e, obj
Demo obj1;
obj1 = obj;

It is to be noted that the above statement cannot create new object in the memory but the 'obj1' reference variable now points to the same object which is pointed by the 'obj'



Also at any time if we point 'obj' object to null than it does not mean than the 'obj1' object is destroyed.

obj = null;

This means that the 'obj1' object will points to the same object but the 'obj' will not points to any object.


So, this is all about the 'new' operator.

Thursday, 23 July 2015

moving car program

import java.awt.*;
import java.applet.*;
import java.net.*;
/*<applet code="MovingCar" width="1340" height="580"></applet>*/
public class MovingCar extends Applet
{
int x=15,y=15;
int r1=20,r2=115;
int r3=161,r4=115;
Image img;
URL u1;
public void init()
{
setBackground(Color.BLUE);
setForeground(Color.BLACK);
}
public void paint(Graphics g1)
{
u1=getCodeBase();
img=getImage(u1,"3.png");
while(true)
{
g1.drawRect(x,y,200,100);
g1.fillOval(r1,r2,50,50);
g1.fillOval(r3,r4,50,50);
g1.drawImage(img,x,y+400,this);
x++;r1++;r3++;
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.getMessage();
}
if(x>getSize().width)
{
x=15;
y=15;
r1=20;
r3=161;
r2=115;
r4=115;
}
g1.clearRect(0,0,getSize().width,getSize().height);
}
}
}

download this image to see image moving with car





Adding a Google Map To Your Website

<!DOCTYPE html>
<html>
  <head>
    <style>
      #map-canvas {
        width: 500px;
        height: 400px;
      }
    </style>

//Load the Google Maps API 
//this script download the codes required  to dispaly map on your page
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script>
//this piece of code create the map and runs when the page is loaded
 function initialize() {
        var mapCanvas = document.getElementById('map-canvas');
        var mapOptions = {
          center: new google.maps.LatLng(44.5403, -78.5463),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
//the map object constructor takes two arguments
// a reference to the div that the map will be loaded into
//options for the map,such as center,zoom and the map type
//center is a google map LatLng object that tells the api where to center the map
//zoom is a number between 0 to 22
//mapTypeId is used to specify what  type of map to use 
        var map = new google.maps.Map(mapCanvas, mapOptions)
      }
//add an eventlistener to the window object that will call the initalize function once the page has been loaded    


google.maps.event.addDomListener(window, 'load', initialize);
  </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>




Wednesday, 22 July 2015

add a social plugin to your website

If you have a Facebook fan page for your website, you can add an additional Like button to your theme or content to let your visitors Like your brand on Facebook directly from your website.

generate your button code: Use the Facebook Developer resources (https://developers.facebook.com)to get the code to add a Like button/share button  to your store.


add the facebook javascript sdk to your theme:add a snippet of code to your theme

to add a like button to your website follow these steps:

1. Go to the like button for the webpage on the facebook Developer resources site(https://developers.facebook.com/docs/plugins/like-button)

2. Set the URL to like as your Facebook Page URL


3.Customize the look of your button

4.Uncheck include share button.

5.Click Get Code

6.You will receive a Javascript SDK code snippet and a like button snippet






1. From the Facebook browser Tab ,copy the Like Button Code to Your ClipBoard

2. Paste your ClipBoard Content into the template of your choice

3. Save your changes.

you can also generate the code for other social plugins like share/comment etc. 

Thursday, 9 July 2015

what is the difference between URL,URI and URN

before going into URL,URI and URN
you need to know some background. Do you ever thought about, who decides what is URL? and what is URI? or who is the authority for URL, URI and such naming conventions?

W3C and IETF

There are two separate bodies W3C and IETF. The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. The specifications for URI and URL are defined by W3C.
It was founded and headed by Sir Tim Berners-Lee. He is one of the greatest scientist living now. He created this www model of server and client architecture, a web server serving web pages through network and client browsers reading it. He did it first when he was with CERN. He created the world’s first web page http://info.cern.ch/ . He was also in HTML 2.0 working group of IETF. So it is very appropriate for W3C to define URI and URL.
Internet Engineering Task Force (IETF) is an open international community working on Internet related standards. In general it addresses issues of Internet protocols.
In particular W3C defines the web, html specifications and related information. IETF defines IP, TCP, or DNS, for security at any of these levels; with SMTP or NNTP protocols.
So the stake for definition for URI and URL is with W3C. But as there is only a thin line between these organisation’s work they tend to cross each other. In some place IETF gives dissimilar definition for URL and URI.
If you read through the huge volume of journals available in web for this topic, you can sense that experts :-( are using URI and URL synonymously. Which is causing all these confusion among the web community about URI, URL and URN.

URI

URI stands for Uniform Resource Identifier. URI is a text which is used to identify any resource or name in Internet. URI has two specialization in form of URL (Uniform Resource Locator) and URN (Uniform Resource Name) to identify resource and name. We mostly see examples of URL and URN in real word. If you are working on JSP and familiar with using tag library e.g. displaytag and JSTL core tag library, then you may remember use of URI to locate binary corresponding to a tag library.


URL

URL standards for Uniform resource locator and it is a subset of URI or Uniform Resource Identifier. URL includes location as well as protocol to retrieve the resource e.g. in http://javaforocpjp.blogspot.sg/2015/09/difference between url.html, HTTP is a protocol which can be used to retrieve resource difference between url.html available in location http://javaforocpjp.blogspot.com directory. It's not necessary that URL always include http as protocol, it can use any protocol e.g. ftp://, https:// or ldap://.

URN

URN stands for Uniform Resource Name. URN is also subset of URI. One of the best example of URN is ISBN number which is used to uniquely identify a book. URN is completely different than URL as it doesn't include any protocol.