Posts

Showing posts from February, 2025

Java annotations

 Do you want to add information about the code as meta data? If your answer is yes then this is the feature to add annotations. Why is it needed? It is used to provide supplementary information about the classes, member variables and functions. What is the difference between annotations and comments? Annotations provides the meta data about coding which can be used by tools and compilers. But comments are simple information to know about coding. Syntax: ‘@’ annotation name                                                                                     ...

Symmetric Encryption in java

              Symmetric encryption is the process of using same key for both encryption and decryption. Encryption: It is the process of converting plain text into ciphertext using an encryption key. Decryption: It converts the ciphertext into original plain text using the same key. Program implementation in java:               Program implementation starts from reading input text from the user. A secret key is generated. The input is encrypted using the secret key. Same key is used for decryption. Steps to follow: Built-in packages javax.crypto.* ,java.util.Base64 and java.util.Scanner are included. A public class is created with main() function. Input reading: Scanner object is created to read the system input. Using this object, input text is read until the next line. Secret key Generation: KeyGenerator object is created to get instance for AES(Advanced Encrypt...

Java Program to implement Password Hashing using SHA-256:

              A simple java security Program is implemented here. It uses SHA-256. What is SHA-256?               Secure Hash Algorithm 256-bit is the expansion of SHA-256. It is a hashing algorithm in the form of cryptography. The input data size is 32 bits. Common usage of this algorithm deals with password hashing, data integrity and digital signatures. Logic behind the program: A password is given by the user as input. This password is hashed in the format of 32 bits by the use of SHA-256 function. Here, MessageDigest is used to make the user password into hashed format. Program implementation:               Program implementation starts with including the built-in packages from java.security. A Public class”PassWordHashEg” is created with main() function. A string variable “pwd” is assigned with user p...

Java storage management: Heap memory

              Java objects can be stored in heap memory. It can be handled by JVM (Java Virtual Memory). How it will be created???               Whenever a new object is created using ‘new’ keyword, the memory is allocated the space in heap. As java has a garbage collector, it easily deallocates the memory. To perform the efficient garbage collection, heap is divided into different generations. Young Generation: It deals with new objects. Old Generation: when an object survived in multiple garbage collection cycles, it is considered as old generation. Permanent Generation : It has the metadata in older versions. Now, it is considered as Meta space. Java Program to illustrate Heap Memory: public class GenerationalHeapEg {     public static void main(String[] args) {         // A varible is initialised with 0 value for ...

Memory Management in Java: Weak references

              Reference is a pointer which points to another variable or pointer. It plays a vital role in memory management. It deals with the access of data. Depends on the reference, it may be strong or weak. A strong reference prevents the garbage collector to reclaim the object memory until the reference exists. A weak reference allows the garbage collector to reclaim the object memory. Note: Weak reference is used in memory sensitive type of applications. Let us create a java memory management program to implement the weak reference example. Java implementation of weak references: This program has the following logic. Logic: ·        A string object is created as Strong object reference. ·        It gets assigned with a value. ·        A weak reference is created as like strong reference. ·        Get the weak re...

Memory Management in java: Memory Leak

             Memory leak occurs when a computer program doesn’t manage the Memory allocation. This causes a part of memory is allocated, but it is not freed. Even though, the memory is not needed. Memory leak makes the system performance low. If the programming language is java, it mostly uses the garbage collector to manage the memory.  Sometimes, few of object references are there unintentionally. This may cause memory leak. For eg, let us consider an infinite loop. Here, a list is created as String. Using a while loop, the elements are added. But the loop is infinite. So, the list elements are added in infinite times. So, garbage collector unable to reclaim the memory of ‘l1’. So, it causes memory leak. Program: Memory Leak Example Steps: Include the built in packages java.util.ArrayList, java.util.List. Create a public class with main () function. A list l1 is created as String using ArrayList. Create a while loop with tru...

Memory Management in java: Object Creation and Garbage Collection

       Memory plays a vital role in programming. Any objects, variables need memory to store its value. Generally, memory management involves the allocation and deallocation of memory space. The memory areas in java can be classified into 4 types listed as follows. ·        Heap Memory: It stores the objects and variables. ·        Stack Memory: Method calls and local variables. ·        Code (Method) Area: byte codes of methods and constructors are stored. ·        Native Method Stack: It has Native Method information. Some of the concepts in memory management are given below. ·        Object Creation and Garbage Collection ·        Detecting Memory Leak ·        Using Weak References Let us implement these concepts in Java. Java Program ...

Java NIO examples to illustrate channels and buffers.

     As the earlier blog gives you a basic knowledge about java NIO. Here, is the some examples to understand the concept of channels and buffers. Channel is a foundational construction. It helps you to connect the input, output, hardware devices, files and sockets. Buffer: it is a storage place where we can store the temporary data. Let us create a channel for processing file reading and writing operations. Make it connected with a buffer. File Reading Example using java NIO: ·        It includes the built in packages. ·        A public class is created with main() function. ·        Three objects are created. FileInputStream,FileChannel and ByteBuffer. ·        Data is read through the channel and gets stored in buffer. ·        Until the buffer has data, it reads the data and display it in the output screen · ...

Java implementation of NIO(New Input/Output)

Image
       Java NIO (New Input/Output). It is an API (Application Programming Interface) which provides non-blocking input, output operations.it ensures high-speed in executing the operations. Java 1.4 includes the feature. Features: It has the following features listed below… ·        Non-blocking I/O ·        Selectors ·        Channels and Buffers ·        Charset ·        Coders/decoders ·        Path and file operations Program implementation: This implementation starts from creating a text file”example.txt” with some text. Steps to follow: Include the built in packages such as java.io.RandomAccessFile,java.nio.ByteBuffer and java.nio.channels.fileChannel. A public class with main() function is created. RandomAccessFile object is created. It opens the file “exam...

Java Networking Examples: Client and Server Application in UDP

     UDP(User Datagram Protocol). It is a transport layer protocol which is not a reliable communication. But it suitable for video streaming applications. Logic: UDPServer: ·        A DatagramSocket object is created with a port ‘9876’. ·        Data is declared as byte to receive the data packet from client. ·        Server starts receiving packets. ·        Prints the received message in the command prompt. ·        The server stops when it receives the exit message. ·        The server socket is closed. UDP Client: ·        A datagramSocket is created. ·        IP address of the system is get using getByName() function. ·        Data is declared as byte. ·     ...