Friday, April 6, 2012

What is Java Object Initialization?

The concept of object in a broad sense can be described as a collection of data along with the methods to manipulate the corresponding data. The Objects are be loaded into the memory before processing. In main memory the object has a state determined by its instance variables and their values, which can be processed and evolved throughout its lifetime. The newly-created object should have a newly-allocated memory( heap) to accommodate the object's instance variables, this is done by Java virtual machine (JVM).

Object initialization is very important in programming, as it is a common source of bugs. The Java Programming language provides different built-in mechanisms to ensure proper initialization of the memory occupied by a newly-created object, there by helping the objects to get a valid initial state.

The Java Programming language offers three mechanisms to ensure proper initialization of newly created objects.

* Constructors.
* Instance variable initialization.
* Instance initialization.

When we are creating an object using new operator or the new Instance() method of class, JVM allocates memory for it, then Java virtual machine will run the initialization code before we use the newly-allocated memory.

Constructors

Constructors are methods, with a set of parameters and a body of code and has the same name as the class but has no return type. The constructors can be overloaded by varying the number, types, and order of parameters. When we are creating new instance using new keyword we often provide a constructor that initialize the Object. If we are using class that has no constructor defined, the compiler will automatically generate a default constructor for the class. This constructor has no parameter and has empty body, this ensures that each class has at least one constructor.

class Demo

{

int a;

public Demo()

{

// the initialization code for instance variables

a=0;

}

}

Instance variable initialization

In an instance variable initialization method java uses an equals sign and expression technique to initialize the instance variable.

class Demo

{

private int a = 1; //this evaluate a with a value of 1

}

Instance initialization

Instance initialization is also known as instance initialization block, which is one of the alternatives to Instance variable initialization. It is very useful in following situations

* Initializer code need to catch exceptions.
* Initializer code need to do calculations that can't be expressed with an instance variable initializer.
* If class has multiple constructors - no need to repeat the same initializing code in all constructors.
* When we use anonymous inner classes, which normally do not have constructors.

class Demo

{

private int a;

//intializer

{

a=2;

}

}

One of the main properties of initializers is they cant make forward reference, as the initializers are executed in textual order.You have to make sure that the referring of instance variable should be only after it is initialized.

Default initial values

If we are not providing any explicit initialization method to variables including instance and class variables, they will be initialized according to their type. Each data type has its own default initial values. The local variable must be initialized explicitly before use.

e.g boolean-false

int - 0

Java's initialization mechanisms help the programmer to ensure that objects we design have a valid and predictable state.



Article Source: http://EzineArticles.com/4564513
Sapient India : A wise man once said: “The principle goal of education is to create men who are capable of doing new things, not simply of repeating what other generation
Sapient : Work-from-home is the HR trend that seems to have evolved from IT & ITES firms who mainly leverage on Internet to connect & collaborate with business associates, partners and clients.

New to Java

Can I learn java quickly? Yes now it's your cup of tea.

The new beginners section "New to Java technology" on Roseindia.net, can help the dummies to learn java from basic to masters level.

This new to java tutorial briefs you on following topics.

1. Basics of Java Technology

2. Understanding the Java Technology

3. Why Java Technology is so important?

4. Different Editions of Java Technology

5. Components of each edition

· JSE Components:

· JEE - Components: etc... etc...

How to create and run my first java program, to what extension java file should be saved, java compiler, java and jdbc database connectivity or java with database management system. These are the very common problems raised while learning java.
Here in this new to java section you will find all the required solution to step ahead on next level of java technology.

The Additional features "Java Examples" of New to java tutorial can help you in learning java quickly and easily. As example codes are already written, you only have to copy and paste it to execute and run the program. In this way you can save yourself from writing code and fixing the bugs.

Find out more about java tutorials, struts tutorials and other programming language at Rroseindia.

Take a quick glance of what you can learn here ...

1. Java tutorials: Basic and advance Java to enhance your skills.

2. JSP Tutorials: Java server pages and other scripting language.

3. Spring and Hibernate Tutorials

4. MySql tutorials: Database connectivity by using java and MySql

That's not all, here you will find numerous tutorials on latest programming technology along with running examples and descriptions.

This review “new to java” has written for the java beginners, at roseindia.net you can read more New to java tutorial, struts tutorial for beginners and professionals to enhance your skills.



Article Source: http://EzineArticles.com/562728

Introduction to Java Concurrent Programming

When computers didn't have operating systems yet, they executed only one program at the time. The program would run from the beginning to the end. Running only one program was the only way back then, and this is now seen as very inefficient. Because the program had access to all the resources in the computer at any time, programming was a lot simpler. But because the machine (computer) was only running once program at the time, the machine's resources where not used efficiently, resulting in an inefficient use of the computers capabilities. Nowadays, machines are able to run multiple programs. The OS (operating system) can even run multiple programs simultaneously, and one single program can even be divided into multiple concurrent threads. All these threads together do all the work that used to be done by just one program. Threads can also communicate with each other, with for instance; message passing and shared memory concurrency.

The last few years, multicore processors have become less expensive. Most desktop computers and laptops are using multicore chips and developers are programming more and more with multiple concurrent threads. A downside is that the bug reports related to threading are also increasing every day; this shows that multicore programming is not the easiest thing to do. In Java, concurrency bugs are one of the most painful problems and in many occasions they are highly unpredictable. One of the biggest issues regarding Java concurrent programming is the mind-set of the programmers that doesn't match the concurrency features offered in Java. Programmers need to start thinking about concurrency and parallelism in the foundation of their programs. The Java language provides low level tools for concurrency such as condition waits, synchronization, message passing and resource sharing. The problem is that these tools need to be implemented at application level consistently. The conclusion? It can already be hard to write correct programs, but writing correct concurrent programs is even harder. The right mind-set is needed. The reason behind the high difficulty level is that there are simply more levels where things can go wrong with Java concurrent programming. The reason developers still choose to use concurrency on their programs is because threads are an inevitable feature of the Java language and the easiest way to use the multicore power we see almost everywhere these days. When looking are the future, the only conclusion that can be made is that concurrency programming will become increasingly important with the rising number of cores.

For more information regarding Java concurrent programming, please visit www.ateji.com.



Article Source: http://EzineArticles.com/6401289