JAVA CODING TECHNIQUES

Java Course in ChennaiJAVA

Java is generally a programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies. The most advantage of java is write once, run anywhere in the world. The complied java coding can run in any applications related to java without recompilation. Java Course in Chennai offers the well trained MNC professionals as trainers. Java applications are complied to byte code that can run in Java Virtue Machine. Java is the programming language which can run on the client-side server.

History of Java

Java is designed by James Gosling at Sun Microsystems and released it in the year 1995 as the core component of    Sun Microsystems. This language consists of the C, C++ language in the coding of Java.

Java Coding Techniques

Some of the easy techniques for Coding Java language are:

1.Do you know about C++ Destructors

You never need to debug through any code leaving memory leaks due to allocated memory not having freed, after an object was removed. For implementing garbage collection the Sun oracle has done an significant job.

It often makes sense to free memory in inverse order of allocation. Java is also allocating in inverse order, when you’r operating with destructor-like semantics:

  • When your using @before and @after  JUnit annotations.
  • When allocating, free JDBC resources.
  • When calling Super methods.

The implementation of some event listener using the concrete for example.

@Override

public void beforeEvent(EventContext e) {

super.beforeEvent(e);

// Super code before my code

}

@Override

public void afterEvent(EventContext e) {

// Super code after my code

super.afterEvent(e);

}

  1. Don’t trust tour early SPI

It is easy to provide SPI to customers by injecting custom behavior into your library/code. Java Training in Chennai is the best training institute to learn these techniques. Once you added the SPI published and decided to follow semantic versioning, you need to add one-argument method to SPI and that you might need another argument in some cases.

interface Event Listener {

// Bad

void message(String message);

}

If you need message ID or message source.API evolution will prevent you from adding that parameter easily.

interface Event Listener {

// Bad

default void message(String message) {

message(message, null, null);

}

// Better

void message(

String message,

Integer id,

MessageSource source

);

}

Use a Context object, without polluting dozens of methods on SPI.

interface MessageContext {

String message();

Integer id();

MessageSource source();

}

interface EventListener

{

// Awesome!

void message(MessageContext context);

3.Avoid returning anonymous, local, inner classes

The programmers are generally uses the keyboard shortcuts in the code generating. But, this should not be done anonymous. Java Training at Fita is the best place to learn Java Course because, they will provide outer instance. And they will drag that outer instance to wherever they go cause in leakage of memory.

4.Avoid Null Point Exception with String literal

Avoiding the Null Point Exception by replacing with the String literal at left sides of the equals (). The following code is given below.

// Bad

if (variable.equals(“literal”)) { … }

// Good

if (“literal”.equals(variable)) { … }

Nothing is last by re-phrasing the expression from good lesser to better one. This technique is the best technique in programming Java.

Are you interested to know more about the Java Programming language. Enroll for Java Training in Chennai.

 

Leave a Reply