Showing posts with label corejava. Show all posts
Showing posts with label corejava. Show all posts

Monday 22 July 2013

Java (JVM) Memory Types

// siddhu vydyabhushana // 2 comments
Java has only two types of memory when it comes to JVM. Heap memory and Non-heap memory. All the other memory jargons you hear are logical part of either of these two.

Heap Memory

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.

Non-heap Memory

It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’.

Method Area

As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.
The above three (heap memory, non-heap memory and method area) are the main jargon when it comes to memory and JVM. There are some other technical jargon you might have heard and I will summarize them below.

Memory Pool

Memory pools are created by JVM memory managers during runtime. Memory pool may belong to either heap or non-heap memory.

Runtime Constant Pool

A run time constant pool is a per-class or per-interface run time representation of the constant_pool table in a class file. Each runtime constant pool is allocated from the Java virtual machine’s method area.jvm memory

Java Stacks or Frames

Java stacks are created private to a thread. Every thread will have a program counter (PC) and a java stack. PC will use the java stack to store the intermediate values, dynamic linking, return values for methods and dispatch exceptions. This is used in the place of registers.

Memory Generations

HotSpot VM’s garbage collector uses generational garbage collection. It separates the JVM’s memory into and they are called young generation and old generation.

Young Generation

Young generation memory consists of two parts, Eden space and survivor space. Shortlived objects will be available in Eden space. Every object starts its life from Eden space. When GC happens, if an object is still alive and it will be moved to survivor space and other dereferenced objects will be removed.

Old Generation – Tenured and PermGen

Old generation memory has two parts, tenured generation and permanent generation (PermGen). PermGen is a popular term. We used to error like PermGen space not sufficient.
GC moves live objects from survivor space to tenured generation. The permanent generation contains meta data of the virtual machine, class and method objects.

Discussion:

Java specification doesn’t give hard and fast rules about the design of JVM with respect to memory. So it is completely left to the JVM implementers. The types of memory and which kind of variable / objects and where they will be stored is specific to the JVM implementation.

Key Takeaways

  • Local Variables are stored in Frames during runtime.
  • Static Variables are stored in Method Area.
  • Arrays are stored in heap memory.

References:

Read More

Differentiate JVM JRE JDK JIT

// siddhu vydyabhushana // Leave a Comment
Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.
JVM becomes an instance of JRE at runtime of a java program. It is widely known as a runtime interpreter. The Java virtual machine (JVM) is the cornerstone on top of which the Java technology is built upon. It is the component of the Java technology responsible for its hardware and platform independence. JVM largely helps in the abstraction of inner implementation from the programmers who make use of libraries for their programmes from JDK.
Diagram to show the relations between JVM JRE JDK
Diagram to show the relations between JVM JRE JDK

JVM Internals

Like a real computing machine, JVM has an instruction set and manipulates various memory areas at run time. Thus for different hardware platforms one has corresponding implementation of JVM available as vendor supplied JREs. It is common to implement a programming language using a virtual machine. Historicaly the best-known virtual machine may be the P-Code machine of UCSD Pascal.
A Java virtual machine instruction consists of an opcode specifying the operation to be performed, followed by zero or more operands embodying values to be operated upon. From the point of view of a compiler, the Java Virtual Machine (JVM)is just another processor with an instruction set, Java bytecode, for which code can be generated. Life cycle is as follows, source code to byte code to be interpreted by the JRE and gets converted to the platform specific executable ones.

Sun’s JVM

Sun’s implementations of the Java virtual machine (JVM) is itself called as JRE. Sun’s JRE is availabe as a separate application and also available as part of JDK. Sun’s Java Development Tool Kit (JDK) comes with utility tools for byte code compilation “javac”. Then execution of the byte codes through java programmes using “java” and many more utilities found in the binary directory of JDK. ‘java’ tools forks the JRE. Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JVM for other languages

A JVM can also be used to implement programming languages other than Java. For example, Ada source code can be compiled to Java bytecode, which may then be executed by a Java virtual machine (JVM). That is, any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine (JVM). Attracted by a generally available, machine-independent platform, implementors of other languages are turning to the Java virtual machine (JVM) as a delivery vehicle for their languages. PHP with Quercus is such an example.

Just-in-time Compiler (JIT)

JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
Read More

Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency

// siddhu vydyabhushana // Leave a Comment
These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.

Association

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.
Example: A Student and a Faculty are having an association.

Aggregation

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

Composition

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

Difference between aggregation and composition

Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!

Abstraction

Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
Example: A wire frame model of a car.

Generalization

Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.
Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.

Realization

Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, you can understand this as the relationship between the interface and the implementing class.
Example: A particular model of a car ‘GTB Fiorano’ that implements the blueprint of a car realizes the abstraction.

Dependency

Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.
Example: Relationship between shape and circle is dependency.
Read More

Tuesday 16 July 2013

Top 5 Free Java Ebooks

// siddhu vydyabhushana // 3 comments
There are many free Java eBooks, but most are outdated or not accurate. Here’s the best 5 free Java eBooks in my collection, they are well-known, complete, updated and detailed coverage of using Java programming language. Best for Java beginners and might also good reference for experienced programmers.
P.S The order is based on my personal priority.

1. The Java Language Specification, Third Edition

book cover
Download : http://java.sun.com/docs/books/jls/
Author : James Gosling, Bill Joy, Guy Steele, Gilad Bracha
Description : Written by the inventors of the Java Language Specification. This book provides complete and detailed coverage of the Java programming language.

2. Thinking in Java, 3rd Edition

book cover
Download : http://www.mindviewinc.com/Books/downloads.html
Author : Bruce Eckel
Description : Great and recommended Java book, chapters and tutorials are posted, public review, correction.
Note
Thinking in Java, 3rd edition is still free, but you need to pay for the latest 4th edition.

3. The Java Tutorial 4th Edition

book cover
Download : http://download.oracle.com/javase/tutorial/
Author : Sharon Zakhour, Scott Hommel, Jacob Royal, Isaac Rabinovitch, Tom Risser, Mark Hoeber
Description : Accurate and up-to-date Java tutorials, and you can download the entire Java tutorials in bundle.

4. Core Servlets and JavaServer Pages, Second Edition

book cover
Download : http://pdf.coreservlets.com/
Author : Marty Hall and Larry Brown
Description : Complete and detailed coverage of using Servlets and JavaServer Pages (JSP).

5. Introduction to Programming Using Java, Sixth Edition

book cover
Download : http://math.hws.edu/javanotes/
Author : David J. Eck
Description : Good book for beginning programmers, and might also be useful for experienced programmers.
Read More

Top 5 Open Source Q&A Systems

// siddhu vydyabhushana // 1 comment
List of the open source Q&A implementation or stackoverflow-like website, with “Active” development and promising features and community.
Read More

Most Popular Top 8 Java People

// siddhu vydyabhushana // 311 comments
Here are the top 8 Java people, they’re created frameworks, products, tools or books that contributed to the Java community, and changed the way of coding Java.
P.S The order is based on my personal priority.
1. Tomcat & Ant Founder
James-Duncan-Davidson
James Duncan Davidson, while he was software engineer at Sun Microsystems (1997–2001), created Tomcat Java-based web server, still widely use in most of the Java web projects, and also Ant build tool, which uses XML to describe the build process and its dependencies, which is still the de facto standard for building Java-based Web applications.
Related Links
  1. James Duncan Davidson Twitter
  2. James Duncan Davidson Wiki
  3. James Duncan Davidson personal blog
  4. Apache Ant
  5. Apache Tomcat

    2. Test Driven Development & JUnit Founder

    Kent-Beck
    Kent Beck, creator of the Extreme Programming and Test Driven Development software development methodologies. Furthermore, he and Erich Gamma created JUnit, a simple testing framework, which turn into the de facto standard for testing Java-based Web applications. The combine of JUnit and Test Driven Development makes a big changed on the way of coding Java, which causes many Java developers are not willing to follow it.
    Related Links
    1. Kent Beck Twitter
    2. Kent Beck Wiki
    3. Kent Beck Blog
    4. JUnit Testing Framework
    5. Extreme Programming Wiki
    6. Test Driven Development Wiki
    News & Interviews
    1. Kent Beck: “We thought we were just programming on an airplane”
    2. Interview with Kent Beck and Martin Fowler
    3. eXtreme Programming An interview with Kent Beck
    Kent Beck Books
    1. Extreme Programming Explained: Embrace Change (2nd Edition)
    2. Refactoring: Improving the Design of Existing Code
    3. JUnit Pocket Guide

    3. Java Collections Framework

    Joshua-Bloch
    Joshua Bloch, led the design and implementation of numerous Java platform features, including JDK 5.0 language enhancements and the award-winning Java Collections Framework. In June 2004 he left Sun and became Chief Java Architect at Google. Furthermore, he won the prestigious Jolt Award from Software Development Magazine for his book, “Effective Java”, which is arguably a must read Java’s book.
    Related Links
    1. Joshua Bloch Twitter
    2. Joshua Bloch Wiki
    News & Interviews
    1. Effective Java: An Interview with Joshua Bloch
    2. Rock Star Josh Bloch
    Joshua Bloch Books
    1. Effective Java (2nd Edition)
    2. Java Concurrency in Practice

    4. JBoss Founder

    Marc-Fleury
    Marc Fleury, who founded JBoss in 2001, an open-source Java application server, arguably the de facto standard for deploying Java-based Web applications. Later he sold the JBoss to RedHat, and joined RedHat to continue support on the JBoss development. On 9 February 2007, he decided to leave Red Hat to pursue other personal interests, such as teaching, research in biology, music and his family.
    Related Links
    1. Marc Fleury Wiki
    2. Marc Fleury Blog
    3. JBoss Application Server
    News & Interviews
    1. Could Red Hat lose JBoss founder?
    2. JBoss founder Marc Fleury leaves Red Hat, now what?
    3. JBoss’s Marc Fleury on SOA, ESB and OSS
    4. Resurrecting Marc Fleury

    5. Struts Founder

    Craig-McClanahan
    Craig Mcclanahan, creator of Struts, a popular open source MVC framework for building Java-based web applications, which is arguably that every Java developer know how to code Struts. With the huge success of Struts in early day, it’s widely implemented in every single of the old Java web application project.
    Related Links
    1. Craig Mcclanahan Wiki
    2. Craig Mcclanahan Blog
    3. Apache Struts
    News & Interviews
    1. Interview with Craig McClanahan
    2. Struts Or JSF?

    6. Spring Founder

    Rod-Johnson
    Rod Johnson, is the founder of the Spring Framework, an open source application framework for Java, Creator of Spring, CEO at SpringSource. Furthermore, Rod’s best-selling Expert One-on-One J2EE Design and Development (2002) was one of the most influential books ever published on J2EE.
    Related Links
    1. Rod Johnson Twitter
    2. Rod Johnson Blog
    3. SpringSource
    4. Spring Framework Wiki
    News & Interviews
    1. VMware.com : VMware to acquire SpringSource
    2. Rod Johnson : VMware to acquire SpringSource
    3. Interview with Rod Johnson – CEO – Interface21
    4. Q&A with Rod Johnson over Spring’s maintenance policy changes
    5. Expert One-on-One J2EE Design and Development: Interview with Rod Johnson
    Rod Johnson Books
    1. Expert One-on-One J2EE Design and Development (Programmer to Programmer)
    2. Expert One-on-One J2EE Development without EJB

    7. Hibernate Founder

    gravin-king
    Gavin King, is the founder of the Hibernate project, a popular object/relational persistence solution for Java, and the creator of Seam, an application framework for Java EE 5. Furthermore, he contributed heavily to the design of EJB 3.0 and JPA.
    Related Links
    1. Gavin King Blog
    2. Hibernate Wiki
    3. Hibernate Framework
    4. JBoss seam
    News & Interviews
    1. Tech Chat: Gavin King on Contexts and Dependency Injection, Weld, Java EE 6
    2. JPT : The Interview: Gavin King, Hibernate
    3. JavaFree : Interview with Gavin King, founder of Hibernate
    4. Seam in Depth with Gavin King
    Gavin King Books
    1. Java Persistence with Hibernate
    2. Hibernate in Action (In Action series)

    8. Father of the Java programming language

    James-Gosling
    James Gosling, generally credited as the inventor of the Java programming language in 1994. He created the original design of Java and implemented its original compiler and virtual machine. For this achievement he was elected to the United States National Academy of Engineering. On April 2, 2010, he left Sun Microsystems which had recently been acquired by the Oracle Corporation. Regarding why he left, Gosling wrote on his blog that “Just about anything I could say that would be accurate and honest would do more harm than good.”
    Related Links
    1. James Gosling Blog
    2. James Gosling Wiki
    News & Interviews
    1. Interview with Dennis Ritchie, Bjarne Stroustrup, and James Gosling
    2. Interview: James Gosling, ‘the Father of Java’
    3. Developer Interview: James Gosling
Read More

Convert String With Commas To Long – Java

// siddhu vydyabhushana // 3 comments
A short guide to show you how to convert a String with commas to a long type.
1. For a normal String, you can use Long.valueOf to convert it directly.
	String bigNumber = "1234567899";
	long result = Long.valueOf(bigNumber);
2. For a String with commas, you can use java.text.NumberFormat to convert it.
	String bigNumber = "1,234,567,899";
	NumberFormat format = NumberFormat.getInstance(Locale.US);
        Number number = 0;
	try {
		number = format.parse(bigNumber);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	long result = number.longValue();
3. Alternatively, if you don’t care about Locale, just replace all the commas.
	String bigNumber = "1,234,567,899";
	long result3 = Long.valueOf(bigNumber.replaceAll(",", "").toString());
Read More

Monday 15 July 2013

Wrapper class: Byte class example in java

// siddhu vydyabhushana // 1 comment

 The Byte class encapsulates a byte value. It defines the constants MAX_VALUE and MIN_VALUE and provides these constructors:

      Byte(byte b)
      Byte(String str)
Here, b is a byte value and str is the string equivalent of a byte value. 
EX :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
public class Byte_Demo
{
    public static void main(String args[])
    {
        Byte b1 = new Byte((byte)120);
        for(int i = 125; i<=135; i++)
        {
            Byte b2 = new Byte((byte)i);
            System.out.println("b2 = " + b2);
        }
        System.out.println("b1 Object = " + b1);
        System.out.println("Minimum Value of Byte = " + Byte.MIN_VALUE);
        System.out.println("Maximum Value of Byte = " + Byte.MAX_VALUE);
        System.out.println("b1* 2 = " + b1*2);
        System.out.println("b1* 2 = " + b1.byteValue()*2);
        Byte b3 = new Byte("120");
        System.out.println("b3 Object = " + b3);
        System.out.println("(b1==b3)? " + b1.equals(b3));
        System.out.println("(b1.compareTo(b3)? " + b1.compareTo(b3));
         /*Returns 0 if equal. Returns -1 if b1 is less than b3 and 1 if b1 is
        greater than 1*/
    }
}

 
Output :
 
b2 = 125
b2 = 126
b2 = 127
b2 = -128
b2 = -127
b2 = -126
b2 = -125
b2 = -124
b2 = -123
b2 = -122
b2 = -121
b1 Object = 120
Minimum Value of Byte = -128
Maximum Value of Byte = 127
b1* 2 = 240
b1* 2 = 240
b3 Object = 120
(b1==b3)? true
(b1.compareTo(b3)? 0

Read More