Friday, July 26, 2013

What and where are the stack and heap?

Q. 1
Programming language books usually explain that value types are created on the stack, and reference types are created on the heap, without really explaining what these two things are. With my only programming experience being in high level languages, I haven't read a clear explanation of this. I mean I understand what a stack is, but where and what are they (relative to the physical memory of a real computer)?
  • To what extent are they controlled by the OS or language runtime?
  • What is their scope?
  • What determines the size of each of them?
  • What makes one faster? 
A. 1

The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.
The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.
Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation).
To answer your questions directly:

To what extent are they controlled by the OS or language runtime?
The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

What is their scope?
The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

What determines the size of each of them?
The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

What makes one faster?
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast.

More Details here

Thursday, July 18, 2013

String Permutation in java

package com.swain;

public class Permutation {

    public static void main(String args[]) throws Exception {
        String str = "abc";
        System.out.println("String is " + str);
        System.out.println("*********************");
        System.out.println("After Permutation ");
        showString("", str);
    }

    public static void showString(String st, String str) {
        if (str.length() <= 1)
            System.out.println(st + str);
        else
            for (int i = 0; i < str.length(); i++) {
                String newValue = str.substring(0, i) + str.substring(i + 1);
                showString(st + str.charAt(i), newValue);
            }
    }

}
************************************
or
 ************************************
package com.swain;

public class Permutation {
    static String permutationStr[];
    static int indexStr = 0;

    static int factorial(int i) {
        if (i == 1)
            return 1;
        else
            return i * factorial(i - 1);
    }

    public static void permutation(String str) {
        char strArr[] = str.toLowerCase().toCharArray();
        java.util.Arrays.sort(strArr);

        int count = 1, dr = 1;
        for (int i = 0; i < strArr.length - 1; i++) {
            if (strArr[i] == strArr[i + 1]) {
                count++;
            } else {
                dr *= factorial(count);
                count = 1;
            }
        }
        dr *= factorial(count);

        count = factorial(strArr.length) / dr;

        permutationStr = new String[count];

        permutation("", str);

        for (String oneStr : permutationStr) {
            System.out.println(oneStr);
        }
    }

    private static void permutation(String prefix, String str) {
        int n = str.length();
        if (n == 0) {
            for (int i = 0; i < indexStr; i++) {
                if (permutationStr[i].equals(prefix))
                    return;
            }
            permutationStr[indexStr++] = prefix;
        } else {
            for (int i = 0; i < n; i++) {
                permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
            }
        }
    }

    public static void main(String arg[]) {
        Permutation p = new Permutation();
        p.permutation("aaa");
    }
}

output :


Monday, July 15, 2013

how to replace 2 or more spaces with single space in string using Java

package com.swain;

public class StringUtil {
    public static void main(String args[]) {
        String before = " Himanshu      Sekhar           Swain  ";
        System.out.println("before: " + before);
        String after = before.trim().replaceAll(" +", " ");
        System.out.println("after: " + after);
    }

}

Tuesday, July 9, 2013

How to disable selected date range of Datebox in zk


<zk>
  <window border="normal" title="hello" apply="pkg$.TestComposer">
     
      <div>Welcome to ZK Fiddle , run it right now!</div>
     
   
    <datebox id="db11" width="150px" constraint="no empty,after 20130710,before 20130720" />
  </window>
</zk>


* constraint="no empty,after 20130710,before 20130720"
*  20130710 means 2013-07-10 and 20130720 means 2013-07-20

Wednesday, July 3, 2013

how to change java version in websphere

 Find out this video tutorial.




How to install IBM WebSphere Application Server with java 7 in windows

Step 1:

Download  WebSphere Installation manager here 

Step 2:  install WebSphere Installation manager

Step 3: Select java 7



Step 4.
 Step 5.
Choose Path.

 Next

 Next

Next.

 Next.

 Select profile management tool then Finish

 Create Profile click on Create.


 Select Application Server


 Next



Set your admin details.



Create.





Then start window will open You can start your server.

Default profile Name : AppSrv01
Default Admin port: 9060
Http port : 9080

Admin Url- http://localhost:9060/admin
or 
https://localhost:9043/ibm/console/logon.jsp

Login here with your admin user name and password.


Then follow some video tutorial here

 1.  how to deploy war file in websphere
2.  how to create user and delete user in Websphere
3. how to change java version in websphere



How to deploy war file in websphere

Look this video tutorial.


how to create user and delete user in websphere

 Look this video tutorial.

how to check virtual host in websphere

You can find out this video tutorial.



How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...