Posts

JDK 1.7 Features

Diamond Operator - If we have to declare a map of trades using Generics, we write the code as follows: Map<String, List<Trade>> trades = new TreeMap<String, List<Trade>> (); The not-so-nice thing about this declaration is that we must declare the types on both the sides, although the right-hand side seems a bit redundant. Can the compiler infer the types by looking at the left-hand-side declaration? Not unless you’re using Java 7. In 7, it’s written like this: Map<String, List<Trade>> trades = new TreeMap <> (); How cool is that? You don’t have to type the whole list of types for the instantiation. Instead you use the symbol, which is called diamond operator. Note that while not declaring the diamond operator is legal, as trades = new TreeMap (), it will make the compiler generate a couple of type-safety warnings. Using String Swith in Statement witch statements work either with primitive types or enumerated types. ...

Optional JDK 8

Image
Optional JDK 1.8  Java 8 has introduced a new class Optional in java.util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run. This makes the code more readable because the facts which were hidden are now visible to the developer. Advantages of Java 8 Optional: Null checks are not required. No more NullPointerException at run-time. We can develop clean and neat APIs. No more Boiler plate code The above program using Optional Class // Java program with Optional Class import java.util.Optional; public class OptionalDemo{     public static void main(String[] args) {         String[] words = new String[10];         Optional<String> checkNull =                     Optional.ofNullable(words[5]); ...

Which character encoding should I use for my content ?

Content is composed of a sequence of characters. Characters represent letters of the alphabet, punctuation, etc. But content is stored in a computer as a sequence of bytes, which are numeric values. Sometimes more than one byte is used to represent a single character. Like codes used in espionage, the way that the sequence of bytes is converted to characters depends on what key was used to encode the text. In this context, that  key  is called a  character encoding . However, it is important to understand that just declaring an encoding inside a document or on the server won't actually change the bytes; you need to  save the text in that encoding  to apply it to your content. (The declaration just helps the browser interpret the sequences of bytes in which the text is stored.) Why to Use UTF-8 ? An HTML page can only be in one encoding. You cannot encode different parts of a document in different encodings. A Unicode-based encoding such as UTF-8 can ...

when to use Serialization

Serialization is a mechanism of converting the state of an object into a byte stream and vice versa for deserilization. Points to remember 1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true. 2. Only non-static data members are saved via Serialization process. 3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient. In case of static it will obtain the current value of that variable. 4. Constructor of object is never called when an object is deserialized. 5. Associated objects must be implementing Serializable interface. SerialVersionUID The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID, which is used during Deserialization to verify that sender and reciever of a serialized object have loaded classes for that object whic...

When to use Bigdecimal

Let's explore this problem with the help of an example: All floating point values that can represent a currency amount (in dollars and cents) cannot be stored exactly as it is in the memory. So, if we want to store 0.1 dollars (10 cents), float/double can not store it as it is. Instead, the binary can store only a closer approximation value (0.100000001490116119384765625 in decimal). The magnitude of this problem becomes significant (known as loss of significance) when we repetitively do arithmetic operations (multiply or divide) using these two data types. Below, we will demonstrate what this may look like. Here is an example of the loss of precision using double: public class DoubleForCurrency { public static void main ( String [] args ) { double total = 0.2 ; for ( int i = 0 ; i < 100 ; i ++ ) { total += 0.2 ; } System . out . println ( "total = " + total ); } } total = 20.19999999999996 Special Notes Primitive type: ...

Final Points to Remember

When a variable is declared with  final  keyword, its value can’t be modified, essentially, a constant. This also means that you must initialize a final variable. If the final variable is a reference, this means that the variable cannot be re-bound to reference another object, but internal state of the object pointed by that reference variable can be changed i.e. you can add or remove elements from final array or final collection. It is good practice to represent final variables in all uppercase, using underscore to separate words. Initializing a final variable : We must initialize a final variable, otherwise compiler will throw compile-time error.A final variable can only be initialized once, either via an initializer or an assignment statement. There are three ways to initialize a final variable : You can initialize a final variable when it is declared.This approach is the most common. A final variable is called  blank final variable ,if it i...

Data type & Usage

Before we using a  variable,  we should specify what type (datatype) of variable it is.Because, when we specify the datatype, the system can understand the memory requirements and the operations allowed on the corresponding variables. Suppose we want to store age of a person, then we specify the variable as an integer. Guess we want to store temperature of a city, then we define the it as double.  Different data types in Java Each primitive type has its significance. Out of this eight primitive data types in Java , we have four datatypes to work with integer numbers, two datatypes to work with real numbers (numbers with fraction part), one datatype for character data, and one datatype for logical (Boolean) data. Byte  -  As it occupies 1 byte of memory, we can store a value between  -128 to 127.  If we try to store a value bigger than that we will get a compilation error.       e.g. byte b =...