Friday, April 17, 2009

Eclipse in Action eBook - A Guide for the Java Developer

Eclipse in Action eBook:
-
A Guide for the Java Developer

About the Book: A guide to using Eclipse features and plug-ins effectively in the context of real-world Java development, this book shows Java developers and software architects how to test, debug, and work with the tools provided by Eclipse and other third-party open source plug-ins. Additionally, developers will learn how to use plug-in tools for using Eclipse in a team environment. Techniques discussed include using Ant for more sophisticated building processes and CVS for source control. Also covered are the essentials of building web applications using J2EE technologies such as JSP/Servlets and EJB. Information on self-hosted development is provided with an introduction to the Eclipse Plugin Development Environment (PDE).

-Author
David Gallardo


Download Eclipse in Action eBook

Read more...

HashSet Collection Sample Program Code

Sample Program on HashSet Collection:

A HashSet is a collection set that neither allows duplicate elements nor order or position its elements.

HashSet Methods

  • add() method is used to insert an element in the HashSet collection.
  • size() method helps you in getting the size of the collection.
  • remove() method will be used to delete an element in the HashSet collection.
  • clear() method is used to remove all data from the HashSet collection.
Sample program: HashSet program shows the methods to add, remove and iterate the values of collection. Keys will be used to put and get values. When the HashSet is empty, the below program checks and will display a message "Collection is not having any Elements". If the collection is having Elements then program displays the size of HashSet collection.

import java.util.*;

public class HSetHasHSetet {
public static void main(String [] args) {
System.out.println( "HashSet Example" );
int size;

// Create a HashSet
HasHSetet HSet = new HasHSetet ();
String string1 = "Yellow", string2 = "White",
string3 = "Green", string4 = "Blue";
Iterator iterator;

//Adding data in to HashSet
HSet.add(string1);
HSet.add(string2);
HSet.add(string3);
HSet.add(string4);
System.out.print("HashSet data: ");

//Create a iterator
iterator = HSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();

// Get size of a HSet
size = HSet.size();
if (HSet.isEmpty()){
System.out.println("HashSet is empty");
}
else{
System.out.println( "HashSet size: " + size);
}
System.out.println();

// Remove specific data
HSet.remove(string2);
System.out.println("After removing [" + string2 + "]\n");
System.out.print("Now HashSet data: ");
iterator = HSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
size = HSet.size();
System.out.println("HashSet size: " + size + "\n");

//HashSet empty
HSet.clear();
size = HSet.size();
if (HSet.isEmpty()){
System.out.println("Collection is not having any Elements");
}
else{
System.out.println( "HashSet size: " + size);
}
}
}

Read more...

Wednesday, April 15, 2009

Java Wrapper Classes (java.lang package)

(java.lang package) Wrapper Classes in Java:

Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

For each primitive type there is a Wrapper Class: Boolean, Byte, Character, Double, Float, Integer, Long, and Short. Byte, Double, Float, Integer and Short extend the abstract Number class and all are public final ie cannot be extended.

Classes have two constructor forms:

  • constructor that takes the primitive type and creates an object eg Character(char), Integer(int)
  • constructor that converts a String into an object eg Integer("1"). Throws a NumberFormatException if the String cannot be converted to a number.
There is also a wrapper class for Void which cannot be instantiated.

Observe that an Object starts with a capital letter, while the primitives all start with a lowercase and also remember that Strings are Ojects.

Check here for primitive to Wrapper mapping:
byte --> Byte
short --> Short
int --> Integer
long --> Long
char --> Character
float --> Float
double --> Double
boolean --> Boolean
void --> Void
Sample Code for some of the Java Wrapper Classes :
Integer:
int i = 5;
Integer I = Integer.valueOf(i); //Wrapper Class
int i2 = I.intValue(); //Converting to primitive

Float:
float f = 5.5f;
Float F = Float.valueOf(f); //Wrapper Class
float f2 = F.floatValue(); //Converting to primitive

Double:
double d = 5.55555;
Double D = Double.valueOf(d); //Wrapper Class
double d2 = D.doubleValue(); //Converting to primitive

Boolean:
boolean b = true;
Boolean B = Boolean.valueOf(b); //Wrapper Class
boolean b2 = B.booleanValue(); //Converting to primitive
The wrapper classes also provide various tools such as constants nd static methods. We will often use wrapper methods to convert a number type value to a string or a string to a number type as mentioned above.

Read more...

Wednesday, April 8, 2009

Heap Memory and Stack Memory in Java

Difference between Heap Memory and Stack Memory in Java:

STACK memory is referred as temporary memory,if you come out of the program the memory of the variable will not no more there. STACK memory and HEAP memory may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and de-allocated at only one 'end' called the Top of the stack.

HEAP memory is referred as permanent memory,memory allocated for the object will be maintained even if we came out of the program. For example memory for OBJECT will remains there ever.

The heap segment provides more stable storage of data for a program memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.

Comparison:

When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other. The stack is important to consider in exception handling and thread executions.

The heap is simply the memory used by programs to store variables. Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP