General discussion

Locked

TechDiss

By sagarawasthi_71 ·
Tags: Off Topic
blog root

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Platform Independence

by sagarawasthi_71 In reply to TechDiss

<div xmlns="http://www.w3.org/1999/xhtml">
<div align="justify">
<strong>Platform Independence<br />
</strong>
<br />Java is created to develop network application. OO concept taken from C++ as a reduced subset of what was available in C++ and some restriction are being placed on some of OO concept like multiple inheritance. Since Java was created for networking application it have to be supported on many different platform, that?s where Java stand different than other programming languages.<br />Compiler being used to interpret the language semantic and syntax, and then converting it to executable form of that specified machine (here machine is used in context of hardware and OS running on it). But the problem lies here only that compiler can generate OS specific executables and compilers working on different machine has different specification (not major differences such as in MSWindows C you can use clrscr() to clear screen but in Linux C this function is not available etc) to follow, that make impossible to run one code developed on one machine to work on different machine without some modification.<br />Sun developed a Virtual Machine known as Java VM. This resides between java code and underlying machine. Whenever user want to compile and run a java code, the code passed to JVM which then compiles the code and convert it into byte code which can be used on any other machine. JVM create virtual OS for running executable code.<br />JVM also developed for many available machines, if it is available on the machine user can run any java code on that machine without any hesitation. JVM pretend java code that it running with the same machine without any modification. That is truly platform independence but needs JVM. So it is true ?Write once run anywhere?.<br />DotNet come up with concept CLR ie. Common Language Runtime which specifically state that ?Write in any language and run on one platform?, that gives us language independence (here language means that follows CLS ie. Common Language Specification).<br />This is one of the reason for people to use Java than any other language in the world.</div>
</div><p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/07/platform-independence.html">This post originally appeared on an external website</a></div>

Collapse -

JVM-I

by sagarawasthi_71 In reply to TechDiss

<div xmlns="http://www.w3.org/1999/xhtml">
<div align="justify">All computation in the JVM centers on the stack. Because the JVM has no registers for storing arbitrary values, everything must be pushed onto the stack before it can be used in a calculation.<br />A static Java compiler converts Java source code into a verifiably secure and compact architecture-neutral intermediate format, called Java byte codes. At runtime, the Java Virtual Machine (JVM) can either interpret the Java byte codes or translated into native code by Java Just-In-Time (JIT) compiler to improve the runtime performance.<br />
<br />
<strong>JIT</strong>
<br />The JIT compiler is contained within the JVM. The JIT compiler uses method-by-method translation; that is, it translates each method as it is loaded, rather than translating an entire class. When the JVM runs in interpreter mode (without the JIT compiler), it works directly with the Java bytecode, interpreting each instruction at run time.<br />When the JVM runs in JIT compiler mode (with the JIT compiler), the JIT compiler translates Java bytecode to native instructions and then the JVM then executes those instructions. During a single run of the Java interpreter, the JVM can use the same method multiple times, but the JIT compiler translates that method only once. After the JIT compiler translates a method, the JVM uses the method's native code instead of Java bytecode.<br />
<br />
<strong>Bytecodes</strong>
<br />Bytecodes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of bytecodes for each method in the class. The bytecodes streams are stored in the method area of the JVM. The bytecodes for a method are executed when that method is invoked during the course of running the program. Intepretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM could execute it. A method's bytecode stream is a sequence of instructions for the Java virtual machine. Each instruction consists of a one-byte opcode followed by zero or more operands. The opcode indicates the action to take. If more information is required before the JVM can take the action, that information is encoded into one or more operands that immediately follow the opcode.</div>
</div><p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/07/jvm-i.html">This post originally appeared on an external website</a></div>

Collapse -

Memory Management in JVM

by sagarawasthi_71 In reply to TechDiss

<div xmlns="http://www.w3.org/1999/xhtml">
<div align="justify">There are three type usually associated with VM.<br />
<br />1) Static memory management: - Here everything is allocated statically. All memory areas are allocated statically before execution begins. In this method the memory space for function parameter is also allocated before execution. This is not a good strategy for a VM implementation since it restricts use of recursive calls, it doesn?t allow multithreading. This strategy generally doesn?t allow any change to data structure at runtime.<br />
<br />2) Linear memory management: - Memory is allocated and freed in LIFO (Last In First Out) order. New object can be allocated space very easily. But for deleting old object?s memory space all new object?s memory space has to be deleted. This is used in very simple VMs. Since many data object in VM are managed in stack like structure.<br />
<br />3) Dynamic memory allocation: - Memory can be allocated in any order. Allocation takes place from special area which is known as heap. Memory once allocated is not fixed; size of the object can grow or shrink on the fly. Most of the modern VM use some form of the dynamic memory management. It can be either automatic or manual. Whenever programmer has responsibility of freeing the unused memory space explicitly like in c free (), this called as manual dynamic memory management. But if all allocation and de-allocation of memory done by VM without programmers intervention, this is called as automatic dynamic memory allocation.<br />
<br />3a) Automatic Dynamic Memory Management: - Programmers are free from all worry of allocation and de-allocation of object?s memory space. Garbage collector comes into picture in this method which reclaims unused (unreferenced) memory space. Memory system automatically expands and shrinks data in the heap as necessary. Benefits of this system include making programming easier, no dangling pointer and improve program reliability and safety. Implementation challenges for automatic memory management are keeping tracks of pointer to memory space, deleting memory space allocated to an object i.e. garbage collection, whether to compact heap whenever object memory space is deleted<br /> </div>
</div><p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/08/memory-management-in-jvm.html">This post originally appeared on an external website</a></div>

Collapse -

Garbage Collection in JVM

by sagarawasthi_71 In reply to TechDiss

<div xmlns="http://www.w3.org/1999/xhtml">
<div align="justify">The memory space which had been allocated to an object from the heap, can be reused when it is not being referenced by the original object. This means that once object finished its work, the memory space that had been used by the object can be reclaimed. This whole process of reclaiming memory is known as garbage collection.<br />
<br />Now more technically, the heap is where the objects of a Java program live. Any time you allocate memory with the new, newarray, anewarray, and multianewarray instructions, the memory comes from the heap. The Java language doesn't allow you to free allocated memory directly. Instead, the runtime environment keeps track of the references to each object on the heap, and automatically frees the memory occupied by objects that are no longer referenced this process called garbage collection.<br />
<br />Two type of garbage can be collected one is the pointer which is pointing to non existing memory address other one is memory space which doesn?t have any pointer to reference it. Also the problem with reclamation of memory arises due to restriction on physical memory size.<br />
<br />It can be called as memory recycling because the memory once freed can be used to allocate to new object. For this GC should have some mechanism to check out which are the object memory space not being referenced. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. Finalizer is activity that will check for dirty object. If it is dirty then it will be written back else will simply freed.<br />
<br />GC has to cope with heap i.e. memory compaction or defragmentation of memory. Suppose memory already allocated in some random order from available heap. Now if memory freed which was lying in between some other object?s memory space, then it will be some what similar to OS memory management scenario. Where having enough memory space to allocate it to process doesn?t satisfy whole requirement. But due to no single memory segment available which is big enough to accommodate process, process waits in queue. This same thing can happen in dynamic memory allocation if proper memory compaction methods are not being used.<br />
<br />So it is clear that any garbage collector must do three basic things. First, it must detect garbage objects i.e. unreferenced objects. Second, it must reclaim the heap space used by the garbage objects and make the space available again to the program. Third, it must take care of possible memory fragmentation. </div>
</div><p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/08/garbage-collection-in-jvm.html">This post originally appeared on an external website</a></div>

Collapse -

MyArrayList Implementation

by sagarawasthi_71 In reply to TechDiss

<div align="justify">In this blog, the assignment was to show implementation of the set and get method for ArrayList. I tried to make but I arrived at this following code in which MyArrayList extends directly AbstractList. In MyArrayList, I used get and set method. This is not a dynamic actual custom implementation of ArrayList.<br /><br /><br /><strong>CODE:</strong></div><strong><div align="justify"><br /></strong>import java.util.*;<br /><br />public class TestArrayList{<br /> <br /> private static class MyArrayList extends AbstractList<br /> {<br /> private Object a[];<br /> <br /> MyArrayList(Object array[]) {<br /> a = array;<br /> }<br /> <br /> public Object get(int index) {<br /> return a[index];<br /> }<br /> <br /> public Object set(int index, Object element) {<br /> Object oldValue = a[index];<br /> a[index] = element;<br /> return oldValue;<br /> }<br /> <br /> public int size() {<br /> return a.length;<br /> }<br /> }<br /> <br /> public static void main(String args[])<br /> {<br /> Object[] bArray= new Object[4];<br /> <br /> bArray[0] =new Integer(12);<br /> bArray[1] =new Integer(10);<br /> bArray[2] =new Integer(14);<br /> bArray[3] =new Integer(16);<br /> <br /> MyArrayList aArray = new MyArrayList(bArray);<br /> <br /> System.out.println("Previous to change \t" + aArray.get(1));<br /> aArray.set(1,new Integer(40));<br /> <br /> System.out.println("After changing value \t" +aArray.get(1));<br /> <br /> System.out.println("Size of the array \t" +aArray.size());<br /> }<br />}<br /><br /><strong>OUTPUT:</strong></div><div align="justify"><br />Previous to change 10<br />After changing value 40<br />Size of the array 4</div><p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/08/myarraylist-implementation.html">This post originally appeared on an external website</a></div>

Collapse -

MyArrayList Implementation II

by sagarawasthi_71 In reply to TechDiss

<span style="font-size:130%;color:#ff0000;"><strong>MyArray.java<br /></strong></span><br />package scit.edu.trial;<br /><br />import java.util.*;<br /><br />public class MyArray extends ArrayList<br /> {<br /> private Object a[];<br /><br /> MyArray(Object element) {<br /> a = new Object[1];<br /> a[0]=element;<br /> }<br /><br /> public Object get(int index) {<br /> if (index &lt a.length){<br /> return a[index];<br /> }<br /> else{<br /> System.out.println("Index out of bounds");<br /> return null;<br /> }<br /> }<br /><br /> public Object set(int index, Object element) {<br /> if (index &lt a.length){<br /> Object oldValue = a[index];<br /> a[index] = element;<br /> return oldValue;<br /> }<br /> else {<br /> System.out.println("Index out of bounds");<br /> return null;<br /> }<br /> }<br /><br /> public int size() {<br /> return a.length;<br /> }<br /><br /> public boolean add(Object element){<br /> int lenArray;<br /> int iterCopy;<br /> Object copyArray[];<br /><br /> lenArray=a.length;<br /> copyArray=a;<br /> a= new Object[lenArray+1];<br /> for(iterCopy=0;iterCopy&ltlenArray;iterCopy++){<br /> a[iterCopy]=copyArray[iterCopy];<br /> }<br /> a[lenArray]=element;<br /> return true;<br /> }<br /><br /> public void show(){<br /> int temp;<br /> int iterArray;<br /> temp=a.length;<br /> for(iterArray=0;iterArray&lttemp;iterArray++){<br /> System.out.println(a[iterArray]);<br /> }<br /> }<br /><br /> public boolean remove(Object element){<br /> int index;<br /> int arrayLen;<br /> int iterArray;<br /> int cnt=0;<br /> arrayLen=a.length;<br /> Object copyArray[]=new Object[arrayLen-1];<br /> index=search(element);<br /> if (index &lt 0){<br /> System.out.println("Element Not Found");<br /> return false;<br /> }<br /> for(iterArray=0;iterArray &lt arrayLen;iterArray++){<br /> if(iterArray!=index){<br /> copyArray[cnt]=a[iterArray];<br /> cnt++;<br /> }<br /> }<br /> a=copyArray;<br /> return true;<br /> }<br /><br /> private int search(Object element){<br /> int index;<br /> int arrayLen;<br /> arrayLen=a.length;<br /> for(index=0; index&ltarrayLen;index++){<br /> if (a[index].equals(element) ) {<br /> return index;<br /> }<br /> }<br /> return -1;<br /> }<br /><br /> public int indexof(Object element){<br /> int index;<br /> index=search(element);<br /> if(index&lt0){<br /> System.out.println("Element Not Found");<br /> return -1;<br /> }<br /> return index;<br /> }<br /><br /> public boolean contains(Object element){<br /> int index;<br /> index=search(element);<br /> if(index&lt0){<br /> System.out.println("Element Not Found");<br /> return false;<br /> }<br /> return true;<br /><br /> }<br /> }<br /><br /><br /><span style="font-size:130%;color:#ff0000;"><strong>MyTest.Java<br /></strong></span><br />package scit.edu.trial;<br /><br />import scit.edu.trial.MyArray;<br /><br />public class MyTest{<br /><br /> public static void main(String args[])<br /> {<br /> MyArray aArray = new MyArray(new Integer(2000));<br /><br /> aArray.add(new Integer(100));<br /> aArray.add(new Integer(200));<br /> aArray.add(new Integer(300));<br /> aArray.add(new Integer(400));<br /> aArray.add(new Integer(1000));<br /> aArray.add(new Integer(500));<br /> aArray.add(new Integer(600));<br /> aArray.add(new Integer(700));<br /><br /> <br /> aArray.remove(new Integer(1000));<br /> if(aArray.contains(new Integer(600))){<br /> System.out.println("Element 600 is in list");<br /> }<br /> else{<br /> System.out.println("Element 600 isnt in list");<br /> }<br /> <br /> aArray.remove(new Integer(700));<br /> aArray.show();<br /> System.out.println("Element 300 present at location\t" + aArray.indexof(new Integer(300)));<br /> aArray.set(0,new Integer(900));<br /> aArray.show();<br /> }<br />}<br /><br /><br /><strong><span style="color:#ff0000;">OUTPUT</span></strong><br /><br />E:\try\build>java scit.edu.trial.MyTest<br />Element 600 is in list<br />2000<br />100<br />200<br />300<br />400<br />500<br />600<br />Element 300 present at location 3<br />900<br />100<br />200<br />300<br />400<br />500<br />600<p><div class="blogdisclaim"><a href="http://sysmagic.blogspot.com/2005/09/myarraylist-implementation_112565689234628382.html">This post originally appeared on an external website</a></div>

Back to After Hours Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums