Technology Blog

Partners

WhichIsBest

Ads

ads
WhichIsBest

Saturday, April 5, 2014

What is new in Java 7? New useful features

Java 7 is everywhere now and your boss maybe thinking to migrate too. So if you are still coding old way, it's useful to sneak peek the features.

Java has been constantly growing so much bigger that I cannot fathom all that is added to it.
Let me sum up the ones I found useful in my daily use. Consider this an introduction to Java 7 and we’ll be rolling out more detailed posts on each topic eventually.




String in switch case:

Let me start with a favourite,
With java 7, we can use string in switch case!
switch(str){ //str of type String
  case "abc": //do something
  case "efg": //do other thing
}

Multiple Exception Handling:
No need to copy paste many catch expressions. We can bundle related exceptions in a single catch.
try{
get_A_Coffee (); //do someting;
}
catch(NotTastyException, NotHotEnoughException coffeExpObj){ //bundle similar exceptions
throwCoffeAndGetNew(coffeExpObj); //handle
}

Type Inference:
Lists can be put as such easily!
 Map<String, Integer> = new HashMap<String, Integer>();  //no need to write like this
 Map<String, Integer> = new HashMap<>();     //this will just do

Elvis Operator:
This is from groovy language. ( ?: looks like Elvis)
Instead of  x!=null? x: y, we can write as x ?: y
Ex:- Integer ival = (input from user); // may be null
int i = ival ?: -1; // no NullPointerException from unboxing

Module:
We hear something called Module in Java 7, module or a module definition by book is ‘a logical unit of set of files, resources and its dependencies that can be versioned, packaged and deployed in the module repository for re-use by some other application.’ Each module consists of a module meta-data that is self-describing. (Good for JAR versioning). Modules can be exported & imported between projects.

/* module meta-data */
module A @ 1.0 //this module class is of version 1.0
{
requires B @ 2.1; //this needs B of version 2.1
requires C @ 1.1; //this needs C of version 1.1
} /*meta-data end*/

module A;
package com.sample;
public class sample class{ 
public void sample()
{}
}

Note: If class or one of its members or constructors is declared module, it will be accessible from a type that belongs to the same module.
If you don’t get it chill, I didn’t either, at first. We’ll see more on this in following posts.
 
Whole new I/O API:
The Java team has rolled out a whole new IO package, java.nio(new IO)
Tons of useful things for file and path handling under this.
Path is one such new class. (http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html)
It’s like a Unix path for manipulation, finding file, properties etc
Path searchPath = Paths.get(“c:/sample”);
final Path findFile = Paths.get(“samplefile”);
//similar to FileSystems.getDefault().getPath("/samplefile");
FileVisitor visitor = new SimpleFileVisitor()
{
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
{
if(file.getName().startsWith(findFile)
{
//do something
}}
};
Files.walkFileTree(searcPath, visitor);
Other features of nio are Buffers, Channels, Selectors, Character sets, asynchronous I/O, File Notifications, Directory Operations. know more

Garbage First!:
No more chaos with a fuzzy GC,
•    All new garbage collector, the “Garbage First Collector”.
•    Memory split into multiple regions as opposed to 2 regions in the last version.
•    Quite predictable and provides greater through-put for memory intensive applications.
•    Performs faster than the last parallel collectors.

There are more features like Fork and Join, Cache API (temporary, permanent), new Date and time API. Stay tuned to pro them too.

6 comments:

  1. Good Post and useful content. Thanks for sharing with us and appreciate your effort. Core Java Training in Bangalore | Core Java course in Bangalore

    ReplyDelete
  2. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. This site instastalker is very helpful to find informaton about instagram.

    ReplyDelete
  3. Clean Air India is at the forefront of creating a healthier environment with its innovative Sterile Storage Cabinet solutions. Our Sterile Storage Cabinets are designed to maintain air quality at its best, ensuring that every inch of your storage space remains pristine. With Clean Air India's Sterile Storage Cabinets, you can trust that your valuable assets and sensitive materials will always be protected in a clean and sterile environment. Join us in revolutionizing air quality with Sterile Storage Cabinets from Clean Air India.
    https://www.cleanairindia.com/sterile-garment-cabinets.php

    ReplyDelete
  4. Ananth Info Solution is the go-to destination for businesses in Chennai seeking top-tier SAP Business One partners in Chennai. As SAP Business One partners in Chennai, Ananth Info Solution offers a comprehensive suite of services to help organizations streamline their operations, enhance efficiency, and drive growth. With a team of dedicated experts, Ananth Info Solution excels in providing tailored solutions, making them the trusted choice among SAP Business One partners in Chennai. Whether you're a small business or a large enterprise, Ananth Info Solution stands out as the premier SAP Business One partner in Chennai, ensuring your business's success.
    https://www.ananthinfo.com/erp/sap-business-one-hana/
    https://g.page/ananth-info-solutions?share

    ReplyDelete
  5. Holistic Physical Recovery & Rehabilitation Centre in Chennai, where our dedicated team specializes in comprehensive stroke treatment in Chennai. Our state-of-the-art facility is designed to provide individuals with the highest quality stroke rehabilitation services in Chennai. At our Stroke Treatment Center, we understand the unique challenges that stroke survivors face, and we are committed to offering tailored rehabilitation programs to promote holistic recovery. Whether you or a loved one are seeking stroke recovery assistance, our Chennai-based center is here to help, ensuring that stroke treatment in Chennai is synonymous with excellence and compassionate care.
    https://hprrc.in/
    https://g.page/r/CZjTQOvrP-_MEBM

    ReplyDelete
  6. Sri Balaji Travels Bangalore specializes in providing convenient and hassle-free booking options for the much sought-after Tirupati VVIP L1 darshan ticket from Bangalore. Our expert team at Sri Balaji Travels Bangalore is dedicated to ensuring your pilgrimage experience is smooth and enjoyable. With Sri Balaji Travels Bangalore, securing your Tirupati VVIP L1 darshan ticket from Bangalore is a breeze, making it the preferred choice for devotees. Don't miss the opportunity to book your Tirupati VVIP L1 darshan ticket from Bangalore through Sri Balaji Travels for a truly divine journey.
    https://sribalajitravel.com/ttd_srivani_trust_tirupati.php

    ReplyDelete

WhichIsBest
WhichIsBest