10 Apr 2014

String is immutable in Java


String is Immutable means that once it is created a String object cannot be changed.

Following program contains 3 test cases to illustrate that the String is immutable
public class stringImmutable { public static void main(String[] args) { stringImmutable si1 = new stringImmutable(); for (String s: args) { System.out.println(s); } if(args.length>0) { if(args[0].equals("t1")) si1.t1(); else if(args[0].equals("t2")) si1.t2(); else if(args[0].equals("t3")) si1.t3(); } } public void t1() { //In 1st test case, the str1 is assigned a String and then later a new string String str1="is this constant"; System.out.println("str1>>>"+str1+"<<<"); //attempting to change the above str1; //String str1="a new value"; // throws error:  // Error: variable str1 is already defined in method t1()   // so another way to bypass the above error String str3="a new value"; str1 =str3; System.out.println("str1>>>"+str1+"<<<"); } public void t2() { //In 2nd test case, similar to above test String str1="is this constant"; System.out.println(">>>"+str1+"<<<"); //attempting to change the above str1; str1="a new value"; System.out.println(">>>"+str1+"<<<"); } public void t3() { // In 3rd test, str1 is declared and saved to str2,
// and then str1 is modified to new content String str1="is this constant"; String str2= str1; System.out.println("str1>>>"+str1+"<<<"); System.out.println("str2>>>"+str2+"<<<"); //attempting to change the above str1; str1="a new value"; System.out.println("str1>>>"+str1+"<<<"); System.out.println("str2>>>"+str2+"<<<"); } }

Output

$java stringImmutable t1
t1
str1>>>is this constant<<<
str1>>>a new value<<<
$java stringImmutable t2
t2
>>>is this constant<<<
>>>a new value<<
The above output of t1 and t2 test case clearly shows that String is changing so it cannot be immutable. But that's because we are not looking at the memory in the way its stored.
String str1="is this constant"; 
Here, str1 is a reference variable that points to "is this constant" String Object
str1="a new value";
Now with this statement, str1 points to "a new value" String Object and "is this constant" String object is now not linked to str1 reference variable.

In 3rd test case, we are using another reference variable str2 also to link to the "is this constant" String Object and lets see what happens to the String Object.

Output

$java stringImmutable t3
t3
str1>>>is this constant<<<
str2>>>is this constant<<<
str1>>>a new value<<<
str2>>>is this constant<<<
From this output its clear, that String object is immutable, since str2 is still pointing to "is this constant" String Object. So whenever a string is changed, its just that the reference variable is pointing to a new String object and the old String object hasn't changed.
String str1="is this constant";    
String str2= str1; 
str1="a new value";

11 Jul 2013

How to fix 403 Forbidden error message in wampserver


After Wampserver installation when users are trying to access Localhost or PhpMyAdmin, they get the below error message:

Forbidden

You don't have permission to access / on this server.
 

Solution

Either of the below solutions should work.

1. Make your Wampserver online

  1. Point on the wampserver icon in the quicklaunch taskbar for a few seconds to get a message to check whether the wampserver is online or not.
  2. If the message is "Wampserver - Offline" , then you have to make it online.
  3. Click on the wampserver icon in the quicklaunch taskbar , then click on the "Put Online" at the bottom.

wampserver properties

2. Allow access permission in apache httpd.conf

  1.  Open the httpd.conf  in C:\wamp\bin\apache\apache2.2.22\conf path or browse your wampserver root folder for apache
  2.  Make sure you have such a entry in your httpd.conf , if not put such a entry
 <Directory "C:/wamp/www/">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Allow,Deny
    Allow from all
</Directory>


16 Feb 2013

Difference between public private protected access modifiers in java


  Access Modifier Visibility
Modifier
Class
Package
Subclass
Everywhere
public
Yes
Yes
Yes
Yes
protected
Yes
Yes
Yes
No
default
Yes
Yes
No
No
private
Yes
No
No
No

public  - Members are visible in same class, same package, sub classes and in other classes.

protected - Members are visible in same class, same package, sub classes but not in other classes.

default - Members are visible in same class, same package but not in sub classes and other classes.

private - Members are visible only in same class but not in same package, sub classes and other classes.


  Access Modifier Level Applicability

Modifier
Member Field
Member Method
Nested Class
Top Level Class
public
Yes
Yes
Yes
Yes
default
Yes
Yes
Yes
Yes
private
Yes
Yes
Yes
No
protected
Yes
Yes
Yes
No
   
  public, default  - These modifiers can be applied to members of the class and also to the top class.   

  private, protected  - These modifiers can be applied to member of a class only but not to the top class.


Simple Inheritance

parent.java
package r1;
public class parent
{
        int x;
      
       public parent()
       {
              x=10;
       }
             
       public static void main(String args[])
       {
             
              parent p = new parent();
              System.out.println("parent main x="+p.x);
       }
}
Output
parent main x=10


sub1.java
package r1;
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}
Output
In r1 pckg, sub1 class, main fn, sub1.x = 20



Examples of  Access Modifiers

Following are the errors that occurs if access modifiers are not used properly:

private - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        private int x;      // private access modifier
}
sub1.java
package r1;  // same package as the parent
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}


Output
The field parent.x is not visible

Because: sub1.java is not the same class where x is declared.





default - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        int x;  // no access modifier is default
}
sub2.java
// sub2 class is not part of r1 package
import r1.parent;   // so  its imported
public class sub2 extends parent  // a subclass of parent
{
       sub2()
       {
              x=30;
       }
      
       public static void main(String args[])
       {
              sub2 s2 = new sub2();
              System.out.println("In no pckg, sub2 class, main fn, sub2.x = "+s2.x);
       }     
}

Output
The field parent.x is not visible

Because: sub2.java is not in the same package of parent where x is declared.





protected - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        protected int x;  // protected access modifier
}
some1.java
// some1 class is not part of r1 package
import r1.parent;
public class some1 // not a subclass of parent
{            
       public static void main(String args[])
       {
          parent p = new parent();
          System.out.println("In no pckg, some1 class, main fn, parent.x = "+p.x);
       }     
}

Output
The field parent.x is not visible

Because: some1.java is neither in the same package nor in subclass of parent  where x is declared.



Nested Class

A nested class has access to all the private members of its enclosing class.

public class parent
{
    public static void main(String args[])
    {            
          sub nestedsub1 = new sub();
           
          int disp = nestedsub1.show();
           
          System.out.println("Access to pvt nested subClass's pvt show() = "+disp);
    }
    private static class sub // Nested subclass of parent
    {
        private int y;
         
       private int show()
       {
              y=20;
              return y;
       }
    }
}
public class parent
{
    public static void main(String args[])
    {             
          sub nestedsub1 = new sub();
          
          int disp = nestedsub1.show();
          
          System.out.println("Access to pvt nested subClass's pvt show() = "+disp);
    }
    private static class sub // Nested subclass of parent
    {
        private int y;
        
       private int show()
       {
              y=20;
              return y;
       }
    }
}

The parent class has access to all members of the nested sub class even private members like show() method