Showing posts with label Hibernate struts training course in Chennai Thiruvanmiyur T Nagar JSP servlets spring training course in Chennai Thiruvanmiyur T Nagar. Show all posts
Showing posts with label Hibernate struts training course in Chennai Thiruvanmiyur T Nagar JSP servlets spring training course in Chennai Thiruvanmiyur T Nagar. Show all posts

Sunday, 22 December 2013

Underscores Between Digits in Numeric Literals

Underscores are permitted in numeric literals. You can place underscores where you feel required to increase readability; like between hundreds and thousands, thousands and lakhs etc.
This is used to group numbers in a bigger literal value (especially of long data type).
Note: Do not place underscore at the beginning or ending of the literal value.
public class Demo
{
  public static void main(String args[])
  {
    int flatCost = 48_87_653;

    float buildingCost = 6_47_812.25_67f;

    System.out.println("Flat cost Rs. " + flatCost);   
    System.out.println("Building cost Rs. " + buildingCost);   
   }
}


Note: But following does not work (cannot parse with underscores).
    String str ="12_34";
    int x = Integer.parseInt(str);
The above parsing raises NumberFormatException.


Saturday, 14 December 2013

Try with resources.

This change is easy to explain, but it has proved to have hidden subtleties, which made it much less easy to implement than originally hoped. The basic idea is to allow a resource (for example, a file or something a bit like one) to be scoped to a block in such a way that the resource is automatically closed when control exits the block. This is an important change, for the simple reason that virtually no one gets manual resource closing 100 percent right.  The proposal submitted to Project Coin for this change includes the astounding claim that two-thirds of the uses of close() in the JDK had bugs in them! Fortunately, compilers can be made to produce exactly the sort of pedantic, boilerplate code that humans so often get wrong, and that’s the approach taken by this change This is a big help in writing error-free code. To see just how helpful, consider how you’d write a block of code that reads from a stream coming from a URL (url) and writes to a file (out) with Java 6. Here’s one possible solution.
InputStream is = null;
try {
 is = url.openStream();
 OutputStream out = new FileOutputStream(file);
 try {
 byte[] buf = new byte[4096];
 int len;
 while ((len = is.read(buf)) >= 0)
 out.write(buf, 0, len);
 } catch (IOException iox) {
 } finally {
 try {
 out.close();
 } catch (IOException closeOutx) {
 }
 }
} catch (FileNotFoundException fnfx) {
} catch (IOException openx) {
} finally {
 try {
 if (is != null) is.close();
 } catch (IOException closeInx) {
 }
}
InputStream is = null;
try {
 is = url.openStream();
 OutputStream out = new FileOutputStream(file);
 try {
 byte[] buf = new byte[4096];
 int len;
 while ((len = is.read(buf)) >= 0)
 out.write(buf, 0, len);
 } catch (IOException iox) {
 } finally {
 try {
 out.close();
 } catch (IOException closeOutx) {
 }
 }
} catch (FileNotFoundException fnfx) {
} catch (IOException openx) {
} finally {
 try {
 if (is != null) is.close();
 } catch (IOException closeInx) {
 }
}
Let’s look at the Java 7 code for performing the same task as listing 1.3. As before, url is a URL object that points at the entity you want to download, and file is a File object where you want to save what you’re downloading. Here’s what this looks like in Java 7.

try (OutputStream out = new FileOutputStream(file);
InputStream is = url.openStream() ) {
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
}

You still have to be careful with try-with-resources, as there are cases where a resource might still not be closed. For example, the following code would not close its FileInputStream properly if there was an error creating the ObjectInputStream
from the file (someFile.bin).

try ( ObjectInputStream in = new ObjectInputStream(new
FileInputStream("someFile.bin")) ) {
...
}

Sunday, 8 December 2013

Infinity Logic in java Without Exception.

It is normal when we divide a integer number by 0 we get arithmetic exception in java . But you will be surprised to see that the following program does not throw exception rather prints Infinity .

Please have a look at the following code and the corresponding output.

public class TestInfinity {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
float f = 10.0f;
float g = 0.0f;
float z = f/g;
System.out.println(z);
}

}


Friday, 29 November 2013

Concurrency utilities for JavaEE

Poor performance can be detrimental to an application’s success. Performance can be measured in many ways. Whether application users can initiate tasks and then move onto others without waiting, or whether a database needs to be queried and return results without blocking a user interface, the bottom line is that waiting for tasks to be completed can become a user nightmare, making a poorly performing application difficult to use. In Java SE, the idea of threading is a standard technique that is used to help prevent bad user experiences,because it allows for long-running tasks to be spawned into a separate thread and executed  in the background without waits occurring. Java EE 7 includes the concurrency utilities for Java EE, which standardize a solution for using application components and Java EE services in an asynchronous manner. The API provides an easy path for those familiar with Java SE concurrency to use it for enterprise applications. Before Java EE 6, it was more difficult to perform concurrent tasks with server side applications,because application server containers typically ran application component code on a thread that was managed by the container, and container supplied object access occurred within the same thread. It is unwise to spawn new threads in an application server environment using java.lang.Thread or java.util .Timer, because that can lead to unreliable results. The situation improved when asynchronous Enterprise JavaBeans (EJB) was introduced with Java EE 6, which allowed processing to occur asynchronously on a thread other than the request handling thread. The concurrency utilities for Java EE build upon asynchronous EJB, and introduce a set of application server concurrency services that assist in the asynchronous execution of tasks in an effort to provide a more complete concurrency solution for Java enterprise applications.

Sunday, 24 November 2013

Accessing Local and instance variable in Java 8 using Lambda .

The following code helps us understand the concept of accessing local and instance variables in java 8.
In the following code , "wildAnimal" is the instance variable and "domesticAnimal"  is the local variable declared in the program.The thread constructor is overrided to start the thread using the -> expression .

package com;
public class LambdaVariableAccess {
  public String wildAnimal = "Lion";

  public static void main(String[] arg) {
    new LambdaVariableAccess().lambdaExpression();
  }
public void lambdaExpression(){
        String domesticAnimal = "Dog";
        
        new Thread (() -> {
            System.out.println("Class Level: " + this.wildAnimal);
          System.out.println("Method Level: " + domesticAnimal);
       }).start();       
    }
}

Thursday, 10 October 2013

Java developers in need of cloud computing

Cloud Computing is now an emerging field and let us discuss on why java developer’s should focus on cloud computing. First, it’s important to appreciate the wide scope of the term cloud computing. Obviously, we talk about the public cloud, including Amazon EC2 or Microsoft Azure, not to mention Oracle’s own public cloud. But cloud computing is also changing the way that we manage internal data centers what we refer to as the private cloud. In other words, the very same concepts, efficiencies, and capabilities that the public cloud brought us are now available inside the data center—for example, basic tenets such as self-service and being able to get a development, staging, or production environment for an application in minutes—without going through any significant paperwork or human workflow. On top of that, the elasticity of the cloud means that we’re no longer constrained by a one-size-fits-all model.  Applications must be built so that they can dynamically scale out and dynamically balance the load across multiple servers. This means that when servers are added while an application is running, there’s no interruption of service. To accomplish this, it’s good programming practice to always assume that an application or the components of an application are running in a scaled environment, which means many previous assumptions no longer apply. I’m talking about things such as file  systems—which could be either local or shared and could trip up developers either way—or global Java objects on the heap, which won’t actually be “global” when the application is scaled out. Developers must also understand whether the addition of resources will
actually enable an application to scale out effectively. In other words, just because an application runs correctly when you add additional servers, that doesn’t mean it will actually support more transactions or users. So, you need to explicitly design into the scaleout model the ability for the application to scale as close to linearly as possible. Obviously, data caching is a huge part of this, as is minimizing the amount of information that has to be shared across servers, as is minimizing contention on shared resources such as database systems. Finally, the application needs to be understood in terms of the metrics of elasticity: when are additional resources actually needed, and when is it safe for resources to be taken away? So, we  need to know when we  need to scale out the application,  which involves knowing what to monitor.