Tuesday, 10 January 2017

Wrapper class in Java

Wrapper class in java provides the mechanism to convert primitive into object and object into primitive. Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known and autoboxing and vice-versa unboxing.
============================
primitive ||   Wrapper Class
============================
boolean  --> Boolean
char        --> Character
int           --> Integer
long        --> Long
double    --> Double
=============================
eg-1.Wrapper class Example: Primitive to Wrapper

    public class WrapperExample1{  
    public static void main(String args[]){  
    //Converting int into Integer  
    int a=20;  
    Integer i=Integer.valueOf(a);//converting int into Integer  
    Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  
      
    System.out.println(a+" "+i+" "+j);  
    }}  

Output:

20 20 20

eg-2. Wrapper class Example: Wrapper to Primitive

    public class WrapperExample2{    
    public static void main(String args[]){    
    //Converting Integer to int    
    Integer a=new Integer(3);    
    int i=a.intValue();//converting Integer to int  
    int j=a;//unboxing, now compiler will write a.intValue() internally    
        
    System.out.println(a+" "+i+" "+j);    
    }}    

Output:

3 3 3
---------

That's okay? But why wrapper classes necessary in java. 

The Java designers kept the two separate to keep things simple. You use the wrappers when you need types that fit in the object oriented world - like polymorphism, collections etc. You use the primitives when you need efficiency.

Marker interfaces - Java


Marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. 
Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.

"Why this indication can not be done using a flag inside a class?” this make sense right?  
Yes this can be done by using a boolean flag or a String but doesn't marking a class like Serializable or Clonnable makes it more readable and it also allows to take advantage of Polymorphism in Java.

How to check browser version?

<script>

    function myFunction() { 
     if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 ) 
    {
        alert('Opera');
    }
    else if(navigator.userAgent.indexOf("Chrome") != -1 )
    {
        alert('Chrome');
    } else{
 
 }
</script>

<SCRIPT LANGUAGE="JavaScript">
var browsername;
var browserVersion;
function checkBrowser() {
  browsername = navigator.appName;
  // Sets the version number (e.g., 4.03)
  browserVersion = parseFloat(navigator.appVersion);
  if(browsername.indexOf("Netscape")!= -1) {
    browsername="NS";
  } else {
    if(browsername.indexOf("Microsoft")!=-1) {
      browsername="MSIE";
    } else {
      browsername="N/A";
      alert("Unknown browser: " + browsername);
    }
  }
  //-- code goes here........
  alert("Your Browser Name : " + browsername + " Version : " + browserVersion);
}
</SCRIPT>

Call the above functions on the body onload event. 

How to handle multiple submission in web page?

The simplest way to handle the multiple submit problem is to prevent it from happening. Below is a form, which includes a small amount of JavaScript. The embedded JavaScript remembers if the submit button was previously pressed. On a resubmit, an alert pops up and the form is not submitted again.

 <html>
 <input type="button" name="btnSubmit"
 value="Do Submit"
 onClick="checksubmitcount();">
 </td>
 </tr>
 </table>
 </form>

 <script language="javascript">
 var submitcount = 0;
 function checksubmitcount()
 {
  submitcount++;
  if (1 == submitcount )
  {
   document.Order.submit();
  }
  else
  {
   if ( 2 == submitcount)
    alert("You have already submitted  this form");
   else
    alert("You have submitted this form"
    + submitcount.toString()
    + " times already");
  }
 }
 </script>
 </body>
 </html>

Friday, 20 November 2015

How to remove line numbers from the copy pasted code in Notepad++

Hope many of us faced this issue while copy pasting the code. Along with the code, the line numbers will also be pasted. There are many ways to remove this line numbers from the code. In Notepad++, We can use regex ^\d+ in the Find and Replace option. But there is another handy way which is explained below.

Let's say we have a html code as below: 

1: <html>
2: <body>
3: <h1> HELLO PROGRAMMER </h1>
4: </body>
5: </html>

If We wish to remove the line numbers from 1 to 5, there is a handy way for this. Press the <Alt> key and Drag the mouse on the numbers with the left mouse button pressed. This will select the numbers alone. Thus we can delete the numbers easily with this feature in Notepad++.