Postingan

Menampilkan postingan dengan label java 5 tutorial

Variable argument or Varargs methods from Java 5 with Example - Programming Tutorial

Gambar
Variable argument or varargs in Java allows you to write more flexible methods which can accept as many arguments as you need. variable arguments or varargswere added in Java 1.5 along with great language features like Java Enum, Generics, autoboxing and various others . Variable arguments a relatively small feature but useful for a developer who has been well aware of method and array . Some time we have a scenario that one method can take a variable number of argument and now with varargs from language makes it much easier. In this Java tutorial, we will see How variable arguments makes it easy to write convenient method which can accept any number of arguments,  perfect candidates are sum() and average() kind of methods. This article is in continuation of exploring features of Java programming language. I have already covered thefork-join framework from Java 7 and automatic resource management or ARM blocks, if you haven't read them already you may find them useful. Variable a...

Difference between EnumMap and HashMap in Java

Gambar
HashMap vs EnumMap in Java What is the difference between EnumMap and HashMap in Java is the latest Java collection interview question which has been asked to a couple of my friends? This is one of the tricky Java questions, especially if you are not very much familiar with EnumMap in Java, which is not uncommon, given you can use it with only Enum keys. The main difference between EnumMap and HashMap is that EnumMap is a specialized Map implementation exclusively for Enum as key. Using Enum as key allows doing some implementation level optimization for high performance which is generally not possible with other objects as key. We have seen a lot of interview questions on HashMap in our article How HashMap works in Java but what we missed there is this question which is recently asked some of my friends. Unlike HashMap, EnumMap is not applicable for every case but it's best suited when you have Enum as key. We have already covered the basics of EnumMap and some EnumMap examples ...

What is static import in Java 5 with Example

Gambar
Static import in Java allows to import static members of the class and use them, as they are declared in the same class. Static import is introduced in Java 5 along with other features like Generics, Enum, Autoboxing and Unboxing and variable argument methods. Many programmers think that using static import can reduce code size and allow you to freely use static field of external class without prefixing class name on that. For example without static import you will access static constant MAX_VALUE of Integer class as Integer.MAX_VALUE but by using static import you can import Integer.MAX_VALUE and refer it as MAX_VALUE. Similar to regular import statements, static import also allows wildcard * to import all static members of a class. In next section we will see Java program to demonstrate How to use static import statements to import static fields. Static import example in Java In this static import example, we have imported constants Integer.MAX_VALUE and Integer.MIN_VALUE statica...