Sunday, December 1, 2013

Aliasing in Java

package sample_java_package_1;

public class SampleClass_1 {

   
    public void TestAlias(Test obj){
       
        obj.num = 100;
   
       
    }
    public static void main(String[] args) {
       
        Test obj1 = new Test();
        SampleClass_1 obj2 = new SampleClass_1();
        obj1.num = 200;
        System.out.println(obj1.num);
        obj2.TestAlias(obj1);
        System.out.println(obj1.num);
       
       

    }

}
class Test{
    float num;
}

Saturday, November 30, 2013

Difference between local and class member variables

Here is a self explanatory example:

package sample_java_package_1;

public class SampleClass_1 {

    int i = 1;

    public static void main(String[] args) {

        SampleClass_1 obj1 = new SampleClass_1();
        SampleClass_1 obj2 = new SampleClass_1();
        SampleClass_1 obj3 = new SampleClass_1();
        obj1.i = 5;
        obj2.i = 6;
        int a = obj1.i;
        int b = obj2.i;
        System.out.println(a);
        System.out.println(b);
        System.out.println(obj3.i);
    }

}

Java Autoboxing

Autoboxing in java is the ability to convert or cast between object wrapper and it's primitive type.

This is the actual implementation(without autoboxing)
public class OldVersion {
 public static void main(String[] args) {
    ArrayList list_1 = new ArrayList();
    for(int i = 0; i < 10; i++){
        list_1.add(new Integer(i));
    }
 }

The new version(with autoboxing)
public class NewVersion {
  public static void main(String[] args) {
      ArrayList<Integer> list_1 = new ArrayList<Integer>();
      for(int i = 0; i < 10; i++){
         list_1.add(i);
      }
  }
}

Java Structure

Java program is made of
1. Objects
2. References.

Objects are stored in the heap.
References are stored on stack.

Objects derive all other objects in java.

E.g.
Class is an object.
Class essentially comprise of:
1)fields(data members)
2)Methods(member functions)

All class variables are initialized to default values. But if there is a variable defined inside a method. Then it does not get initialized to a default value.

Methods transfer messages among objects and are created only inside classes.

Method comprise of:
1. Name
2. Its arguments.
3. Its return type.

Static in Java:
When we use static keyword it means that that particular method or field is not associated to any class instance(i.e. object of that class).

e.g.

class Foo {
static int a = 10;
}

Now I create two instances of the class Foo.
Foo object_1 = new Foo();
Foo object_2 = new Foo();

System.out.println(object_1.a);
System.out.println(object_2.a);
System.out.println(Foo.a); //As they are not associated to any instance, they can be used directly with their className
Will give the same answer that is 10

The memory location for the static a will be the same across all instances.

Naming conventions in Java:
The class name starts with a capital with running words (if it contains more words), first letter of which is capital. e.g.

class ThisIsAnExampleClassName{}

Sunday, November 3, 2013

Checking for Anagram

There are various methods:
One of them is:

public class LivingBeing {

    public static void main(String[] args) {

        String str = "LEVEL";
        boolean flag_1 = false;
       
        for(int i = 0; i<str.length(); i++){
           
            if(str.charAt(i) == str.charAt(str.length()-i-1)){
                flag_1 = true;
            }
            else {
                flag_1 = false;
            }
        }
       
        if (flag_1 == true){
           
            System.out.println("Anagram");
           
           
        }
       
    }

}

Remove duplicates:

Removing duplicates from a string in Java:



public class LivingBeing {

    public static void main(String[] args) {

       
       String input = "ABCA";
       String output = "";
      
       for(int index=0; index < input.length(); index++){



                System.out.println("Fiste Value is:" + index % input.length());
                System.out.println("Second Value is:" + (index+1) % input.length());
                System.out.println("Length:" + input.length());
          
           if(input.charAt(index % input.length()) != input.charAt((index+1)% input.length())){
              
              
               output+=input.charAt(index);
               System.out.println("Inner:"+ output);      
           }
           System.out.println("Mid:"+ output);
       }
      
       System.out.println("Outside:"+ output);
    }
   

}

How to use hashtables

Creating HashTables:

public void peopleInfo(String a) {
        // TODO Auto-generated method stub
        if (a == "evelyn") {
            Hashtable<String, String> information = new Hashtable<String, String>();
           
            information.put("Steve", "34 block 2");
            information.put("Sheryl", "34 block 5");
            information.put("Miranda", "34 F lane");

//How to pass hashtables to another method in another class
           PrintFunction printUp = new PrintFunction();
            printUp.printDetails(information);

}


ublic class PrintFunction {

    public String test_variable;

    public void printDetails(Hashtable<String, String> vars) {


//Checks if the HasTable contains the desired key.

        if (vars.containsKey("Miranda")) {

            System.out.println("Miranda stays in : " + vars.get("Miranda"));

// by doing vars.get("Miranda") you will get the corresponding value of this key in the HashTable.

        }

        if (vars.containsKey("Steve")) {

            System.out.println("Steve stays in : " + vars.get("Steve"));

        }
      
}

When the above is invoked from a main method, the output is:

Miranda stays in : 34 F lane