Posts

Showing posts from December, 2019

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 =...

Memory Leakage

What is Memory leakage ? Memory leaks often involve small amounts of memory resources, and you would probably not have problems with it. But when your application returns a java.lang.OutOfMemoryError, then your first and most likely suspect would be a memory leak. Various Precautions required while programming  Release the session when it is no longer needed. Use the HttpSession.invalidate() to do this. Keep the time-out time low for each session. Store only the necessary data in your HttpSession. Avoid using string concatenation. Use StringBuffer’s append() method because the string is an unchangeable object while string concatenation creates a lot of unnecessary objects. A large number of temporary objects will slow down performance. If you are writing a query that is frequently executed, use PreparedStatement object rather than using Statement object. Why? PreparedStatement is precompiled while Statement is compiled every time your SQL statement is transmitted to t...