Posts

Showing posts from September, 2025

OOP OOAD

Object  means a real-world entity such as a pen, chair, table, computer, watch, etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. Object Any entity that has state and behavior is known as an object.  An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Class Collection of objects is called class. It is a logical entity. Inheritance(IS-A) When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.  It provides code reusability.  It is used to achieve runtime polymorphism. Polymorphism If one task is performed in different ways, it is known as polymorphism. Uuse method overloading and method overriding to achieve polymorphism. Abstraction Hiding internal details and showing functionality is known as abstraction. In Java, use abstract class and interface to achieve abstraction. Encapsulation ...

Internal Workings Of

  ConcurrentHashMap: The ConcurrentHashMap class is introduced in JDK 1.5 - java.util.concurrent package,  - implements ConcurrentMap as well as Serializable interface. - is a thread-safe implementation of the Map interface in Java. - allows concurrent access to the map. - is divided into number of SEGMENTs. In ConcurrentHashMap, the Object is divided into a number of segments according to the concurrency level. Part of the map called Segment (internal data structure) is only getting locked while adding or updating the map. ConcurrentHashMap allows concurrent threads to read the value without locking at all. The default concurrency-level of ConcurrentHashMap is 16. In ConcurrentHashMap, at a time any number of threads can perform retrieval operation but for updated in the object, the thread must lock the particular segment in which the thread wants to operate.  This type of locking mechanism is known as Segment locking or bucket locking.  Hence at a time, 16 update o...

Java 21 Features

Java 21 Features String Templates (Preview)  Ex : String name = "BSS Java"; String greeting = STR."Hello, \{name}!"; Sequenced Collections  - SequencedCollection Ex: SequencedCollection<String> names = new ArrayList<>(); names.addFirst("Alice");   names.addLast("Bob"); names.getFirst(); names.getLast(); names.removeFirst();  names.removeLast(); - SequencedSet Ex: SequencedCollection<String> set = new LinkedHashSet<>(); // Adding elements set.addFirst("Alice"); set.addLast("Bob"); set.getFirst(); set.getLast(); set.removeFirst(); set.removeLast(); - SequencedMap putFirst(K key, V value) putLast(K key, V value) pollFirstEntry() pollLastEntry() firstEntry() lastEntry() Ex: SequencedMap<String, Integer> map = new LinkedHashMap<>(); // Adding entries map.putFirst("Alice", 25); map.putLast("Bob", 30); map.firstEntry() map.lastEntry() map.pollFirstEntry(); map.pollLastEntry(); R...

Core Java

Image
Java is a programming language and a platform. Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform. Java is a high level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995 Java was developed by James Gosling, who is known as the father of Java, in 1995. Types of Java Applications: 1) Standalone Application 2) Web Application 3) Web Application 4) Mobile Application Java Platforms / Editions: There are 4 platforms or editions of Java: 1) Java SE (Java Standard Edition) 2) Java EE (Java Enterprise Edition) - used to develop web and enterprise applications. 3) Java ME (Java Micro Edition) - It is a micro platform that is dedicated to mobile applications. 4) JavaFX - It is used to develop rich internet applications. It uses a lightweight user interface API. What happens ...

Java Version Features

Java has evolved significantly from JDK 1.0 to Java 22, introducing powerful features like lambdas, modules, records, virtual threads, and structured concurrency. Java Version Highlights Version | Key Features Java 8 (2014) | Lambda expressions, Streams API, Optional, Default methods, Nashorn JS engine Java 9 (2017) | Module system (Project Jigsaw), JShell, Stream improvements Java 10 (2018) | Local variable type inference (`var`) Java 11 (2018) | New HTTP Client API, String methods, Flight Recorder, Removed deprecated APIs Java 12 (2019) | Switch expressions (preview), Shenandoah GC Java 13 (2019) | Text blocks (preview) Java 14 (2020) | Records (preview), Pattern matching for instanceof (preview) Java 15 (2020) | Sealed classes (preview), Hidden classes Java 16 (2021) | Records, Pattern matching, JEP 376: ZGC improvements Java 17 (2021) | Sealed classes, Pattern matching, Foreign function & memory API (preview) Java 18 (2022) | Simple web server, UTF-8 by default, Code snip...

Comparable vs Comparator in Java

Comparable vs Comparator in Java Java provides two interfaces to sort objects using data members of the class:   1) Comparable   2) Comparator Comparator A comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of the same class. public int compare(Object obj1, Object obj2): class Sortbyname implements Comparator<Student> {   // Method   // Sorting in ascending order of name   public int  compare (Student a, Student b) {     return a.name.compareTo(b.name);   } } Comparable A comparable object is capable of comparing itself with another object. class Movie implements Comparable<Movie>{   public int  compareTo (Movie m) {     return this.year - m.year;   } } using comparator we were able to use different attributes. using comparable we can use only one comparison.

Java17 Features

Java 17 Features Java SE 17 (2021): LTS release with sealed classes , foreign function API (preview), and improved garbage collection . Java 17 is a Long-Term Support (LTS) release packed with powerful features like sealed classes , pattern matching , and enhanced switch expressions —all designed to improve code clarity, safety, and performance. Sealed Classes Sealed classes restrict which other classes can extend or implement them—great for modeling controlled hierarchies. ```java public sealed class Shape permits Circle, Square {} final class Circle extends Shape {} final class Square extends Shape {} ``` Benefits : Improves security and maintainability by limiting subclassing. Pattern Matching for `instanceof` Simplifies type checks and casting. ```java Object obj = "Hello Java 17!"; if (obj instanceof String s) {     System.out.println(s.toUpperCase()); } ``` Benefits : Reduces boilerplate and improves readability. Switch Expressions Enhancements Supports pattern matchi...

MetaSpace in Java 8

MetaSpace in Java 8 JVM Memory Structure : JVM defines various run-time data area which are used during execution of a program.  Some of the areas are created by the JVM whereas some are created by the threads that are used in a program. The memory area created by JVM is destroyed only when the JVM exits. The data areas of thread are created during instantiation and destroyed when the thread exits. JVM Memory Structure is divided into multiple memory area like heap area, stack area, method area, PC Registers etc. The heap area where all the java objects are stored. The heap is created when the JVM starts.  The heap is generally divided into two parts.  1)  Young Generation( Nursery): All the new objects are allocated in this memory.  Whenever this memory gets filled, the garbage collection is performed.  This is called as Minor Garbage Collection. 2)  Old Generation : All the long lived objects which have survived many rounds of minor garbage collectio...

JAVA 8 Features

Java 8 Features 1) Lambda Expressions : Concise functional code using ->. Lambda Expression basically expresses an instance of the functional interface, Lambda Expression provides a clear and concise way to represent a method of the functional interface using an expression. 2) Functional Interfaces : Single-method interfaces. An interface that contains only one abstract method is known as a functional interface. There is no restriction, that can have n number of default and static methods inside a functional interface. A functional interface is an interface that contains only one abstract method.  @FunctionalInterface  annotation is used to ensure that the functional interface can’t have more than one abstract method. Runnable, ActionListener, and Comparable are some of the examples of functional interfaces.   Runnable –> This interface only contains the run() method.   Comparable –> This interface only contains the compareTo() method.   ActionListener ...

Java Cryptography

Cryptography Foundations Cryptology is the broader field that encompasses both cryptography and cryptanalysis. Cryptography is the practice of creating secure communication by encoding messages in a way that is difficult to understand by unauthorized parties. Cryptanalysis is the study of methods for breaking encryption algorithms and protocols without knowing the key. PlainText-Encryption-CipherText-Decryption-PlainText The Goals of Cryptography Confidentiality Data Integrity Data Origin Authentication Entity Authentication Non-Repudiation Cryptographic Primitives Primitives Encryption Hash Function MAC - Message Authentication Code. Digital Signature Q. SALT, What Is a Salt? A salt is: - A random sequence of bytes - Combined with the input (e.g., password) before hashing - Used to prevent attacks like rainbow table lookups or precomputed hash collisions Without a salt: - Two users with the same password will have the same hash. With a salt: - Even identical passwords produce differen...

SOLID

SOLID stands for: S - Single-responsiblity Principle A class should have one and only one reason to change. A class should have only one Job. O - Open-closed Principle Objects or entities should be open for extension but closed for modification. L - Liskov Substitution Principle Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T. Every subclass or derived class should be substitutable for their base or parent class. I - Interface Segregation Principle A client should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced to depend on methods they do not use. D - Dependency Inversion Principle Entities must depend on abstractions, not on concretions.          It states that the high-level module must not depend on the low-level module, but they should depend on abstractions. This principle allows for decoupling. SOLID...

Java 8 Programs

Java 8 Programs Program to ADD PREFIX and SUFFIX to the STRING? - StringJoiner class, used to add prefix and suffix in a given String      StringJoiner stringJoiner = new StringJoiner(",", "#", "#");      stringJoiner.add("Interview");      stringJoiner.add("Questions");      stringJoiner.add("Answers");      System.out.println(stringJoiner); Program to PRINT 10 RANDOM NUMBERs using forEach? - Use Random class to generate Random number      Random random = new Random();      random.ints().limit(10).forEach(System.out::println); Program to iterate a Stream using the forEach method?      List<String> str = Arrays.asList("Hello","Hola","Namaste","Hi");      str.stream().forEach(System.out::println); Program to find the Minimum and Maximum number of a Stream? To find minimum or maximum number in stream, use comparator.comparing() method which will take...