MJ#32: Formatting in Java using printf()

In Java, the printf method is used to format and output text with specified formatting. When it comes to number formatting, printf allows you to control how numbers are displayed, including the number of decimal places, leading zeros, and alignment within a field. Here's a breakdown of how number formatting works using printf in Java: … Continue reading MJ#32: Formatting in Java using printf()

MJ#31: Wrapper Classes in Java

Wrapper classes in Java are classes that encapsulate primitive data types and provide utility methods for working with them in an object-oriented manner. The Java programming language supports eight primitive data types: byte, short, int, long, float, double, char, and boolean. Wrapper classes are used to represent these primitive types as objects. Here are the … Continue reading MJ#31: Wrapper Classes in Java

MJ#30: Upcasting and Downcasting in Java

Upcasting: Upcasting refers to the process of casting an object to its superclass type. It happens automatically in Java when you assign an object of a subclass to a variable of its superclass type. Upcasting is safe and does not require an explicit cast. class Animal { void eat() { System.out.println("Animal is eating"); } } … Continue reading MJ#30: Upcasting and Downcasting in Java

MJ#29: Object Class in Java – toString(), equals(), hashcode()

In Java, the Object class is a fundamental class at the top of the class hierarchy. Every class in Java is a direct or indirect subclass of the Object class. The Object class is part of the java.lang package and provides several methods that are inherited by all other classes. Here are some key aspects … Continue reading MJ#29: Object Class in Java – toString(), equals(), hashcode()

MJ#28: FInal Keyword in Java

In Java, the final keyword is used to denote that a class, method, or variable cannot be further modified or extended. It provides a way to create constants, prevent method overriding, and enforce immutability in certain contexts. The final keyword has different applications depending on where it is used: for classes, methods, and variables. 1. … Continue reading MJ#28: FInal Keyword in Java

MJ#27: Dynamic Method Dispatch in Java

Dynamic method dispatch is a key feature of runtime polymorphism in Java. It refers to the process where the appropriate method to be executed is determined at runtime based on the actual type of the object. This enables you to write more flexible and extensible code. Here's a step-by-step explanation of dynamic method dispatch: Method … Continue reading MJ#27: Dynamic Method Dispatch in Java

MJ#26: Polymorphism in Java

Polymorphism in Java refers to the ability of a single entity (like a method or a class) to take multiple forms. There are two types of polymorphism in Java: compile-time polymorphism (also known as method overloading) and runtime polymorphism (also known as method overriding). 1. Compile-time Polymorphism (Method Overloading): Method overloading allows a class to … Continue reading MJ#26: Polymorphism in Java

MJ#25: Access Modifiers in Java

n Java, access modifiers are keywords that define the visibility and accessibility of classes, methods, and variables. They control which other classes can access a particular class, method, or variable. There are four main access modifiers in Java: Default (Package-Private): If no access modifier is specified, the default access level is package-private. This means that … Continue reading MJ#25: Access Modifiers in Java

MJ#25: Packages in Java

In Java, packages are a way to organize and encapsulate classes and interfaces into a hierarchical structure. They provide a mechanism for grouping related types, which helps in avoiding naming conflicts and enhances code organization. A package is essentially a directory that contains a collection of Java classes and interfaces, along with sub-packages. Here are … Continue reading MJ#25: Packages in Java