Skip to main content

Posts

Data Structure

  Data Structure Recursion Introduction   Head Recursion Tail Recursion Tree Recursion Nested Recursion Basic Question on Recursion Array Introduction Static vs Dynamic Array

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

Static Import Statement

     Static Import Statement 1.5v new features  for-each loop var-args AutoBoxing and AutoUnBoxing Generic Method co-variant return type. Queue Annotation  enum static import                                                                           Static Import introduce in 1.5v  according to sun uses of static import reduces the length of the code and improves readability but according world wide programming expert (like us uses of static import creates confusion and reduces readability hence if there is no specific requirement then it is not recommended to use static import. usually, we can access static members using the class names but whenever writing static import we can access static members directly without a class name. without sta...

Import Statement

      Import statement /* class Test{       public static void main(String... args){               ArrayList arr=new ArrayList()      } } compile Time Error-:              cannot find symbol : class ArrayList             location : class Test                                       ArrayList arr=new ArrayList();                                                                                ^ */ we can solve this problem by using a fully qualified name          ...

Declaration and Access Modifier

                                                              Declaration and Access Modifier java source file structure A java program can contain any number of classes but at most one class can be declared as public.if there is a public class the name of the program and the name of the public class must match other wise we will get compile time error. /* case 1-:if there is no public class then we can use any name and there are no restrictions.  class A{ } class B{ } class C{ } class D{ } save program name any classname.java case 2-: if class B is public then name of program should be B.java other wise we will get compile time error saying class B is public , should be declared in a file named B.java. class A{ } public class B{ } class C{ } class D{ } case 3-:if class B and C declared as public and...

Break and Continue

                                                        Transfer Statement break; continue; label Break-: The break statement in java is used to terminate from the loop, switch and labeled block immediately. When a break statement is encountered inside a loop, the loop iteration stops there, and control returns from the loop immediately to the first statement after the loop. Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop, or we want to terminate the loop based on some condition. we can use break statements in the following places. inside switch to stop fallthrough.                example-:                int x=0;            ...