Skip to main content

Posts

Showing posts from April, 2023

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...

Class Level Modifier Part 2

Java Modifiers Notes strictfp (Strict Floating Point) Introduced in Java 1.2 We can declare strictfp for classes and methods but not for variables. Floating-point results vary across platforms; strictfp ensures IEEE-754 standard behavior. strictfp Method If a method is declared as strictfp , all floating-point calculations inside it follow IEEE-754 and become platform-independent. abstract talks about no implementation, strictfp requires an implementation → so abstract strictfp is illegal for methods. public abstract strictfp void m(); // ❌ Invalid // Error: illegal combination of abstract and strictfp strictfp Class If a class is declared strictfp , all concrete methods in that class follow IEEE-754. ✔️ abstract strictfp is valid for classes. abstract strictfp class Test { } Me...

Class Level Modifers part 1

                   Class Level Modifiers whenever writing our own classes we have to provide some information about our class to the JVM like. whether this class can be accessed from anywhere are not. whether child class creation or not. whether object creation is possible or not. we can specify this information by using the appropriate modifier. the only applicable modifiers for top-level classes are-:        public               default          abstract          final           strictfp but for inner classes applicable modifiers are -:          public             default         abstract         final         strictfp      ...

Packages In Java

Java Packages — Notes Packages Packages are an encapsulation mechanism that group related classes and interfaces into a single unit. What is a package? A package groups related types (classes/interfaces) for modularity, naming, and access control. Examples java.sql — classes and interfaces related to database operations. java.io — classes and interfaces for file I/O operations. Main advantages Resolve name conflicts (unique identification). Improve modularity of the application. Improve maintainability. Provide security for components (access control). Naming convention Use the internet domain name in reverse followed by module/submodule/class. Example: com.icicibank.loan.housing.Account com.icicibank — reversed domain loan — module housing — submodule Account — class Code example ...