Posts

Showing posts with the label java

🧠 What is Stack, Heap Memory, and String Pool in Java?

If you’ve started learning Java, you’ve probably heard people say things like: “That variable is stored on the stack,” or “The object lives in the heap.” And then there’s something called the String Pool — sounds confusing, right? Don’t worry 😄 — this post will make everything super simple to understand. 🧩 Let’s start with: What happens when you run a Java program? When your Java code runs, the JVM (Java Virtual Machine) divides memory into different sections . The two most important ones are: Stack Memory Heap Memory Let’s see what each one does 👇 🥞 1. Stack Memory — for short-term data Think of Stack Memory as a temporary notebook 📒 that the JVM uses to store small, short-lived data. ✳️ Used for: Primitive variables ( int , char , boolean , etc.) References to objects (not the objects themselves) Method calls and local variables Every time a method is called, the JVM creates a stack frame for it. When the method finishes, its frame is r...

How to maintain Multiple Java sdk in Mac OS

Image
Hi Guys, In this blog, we will learn how to manage multiple Java SDKs effectively on macOS. 1) List all the available Java SDKs on the operating system. ~ % /usr/libexec/java_home -v /usr/libexec/java_home -verbose 2) Set or change Java ~ % export JAVA_HOME=$(/usr/libexec/java_home -v 22.0.2) 3) View the updated java version ~ % java -version openjdk version "22.0.2" 2024-07-16 OpenJDK Runtime Environment (build 22.0.2+9-70) OpenJDK 64-Bit Server VM (build 22.0.2+9-70, mixed mode, sharing) 3) View the updated java version ~ % which java ~ % where java

Type conversion in Kotlin vs Java

Type conversion in Kotlin vs Java In Java, The data type is automatically converted to another data type but In Kotlin we need to explicitly convert the data type like toInt(), toLong(), toString() More Type cast methods in Kotlin toChar() - To convert a data tye to Char type toInt() - To convert a data type to Int type toLong() - To convert a data type to Long type toFloat() - To convert a data type to Float type toDouble() - To convert a data type to Double type toByte() - To convert a data type to Byte type toShort() - To convert a data type to Short type Example in Java Int is automatically converted to a Long data type as long is larger than int. public class Demo { // Type casting Java public static void main (String[] args) { int a = 10 ; long b = a; System. out .println(a); System. out .println(b); } } In Kotlin the conversion is not automatic, we need to explicitly do the type conversion. Example in Kotlin fun main (args: Array...

What is an android Activity,AppCompatActivity and Fragment Activity. Interview Question

Image
An Activity is an user accessible screen. An activity class loads the all UI widget/component through XML file Every activity must be declared in androidmanifest.xml file which is in your project Activity is a baseline class. Its derived from "android.view.ContextThemeWrapper" AppComptActivity - Its used for defined the material design widget in UI. We are using AppCompatActivity instead of the ActionBarActivity (ActionBarActivity is Deprecated). AppCompatActivity derived from "androidx.fragment.app.FragmentActivity". FragmentActivity - Its used to define the Nested fragment in our activity class. Its inherit from "androidx.activity.ComponentActivity" and ComponentActivity is derived from our Baseline Activity class Dependency for AppCompatActivity:   implementation 'androidx.appcompat:appcompat:1.1.0'

Super Keyword in Java

The super keyword refers to the superclass (parent) class object super keyword can be used to refer to the parent class instance variable. super keyword can be used to invoke the parent class method. super keyword can be used to invoke parent class constructor. For Practice: public class SuperKeyExample { public static void main(String[] args) { NewClass ncObj = new NewClass(); ncObj.show(); } } class SuperClass { int a = 10; SuperClass() { System.out.println("Super/Base class constructor"); } void show() { System.out.println("Base class member method"); } } class NewClass extends SuperClass { int a = 20; NewClass() { super();//implitly super keyword used here System.out.println("New Class constructor"); } void show() { super.show(); //invoke the base class method using super keyword System.out.println(a); System.out.println(super.a); //Access the base class variable using super keywo...

Final keyword in Java

It's used in variables, methods, and classes 1) Final variables - can't be reassigned the value 2) Final Method - Can't be override 3) Final Class - Can't be extends 1) Final Variable public class FinalKeyTutorial { final static int name = 2; public static void main(String[] arg) { name = 3; //Compiler error System.out.println(name); } } 2) Final Method public class StaticKeyword { public static void main(String[] args) { B b = new B(); b.show(); } } class A { final void show() { System.out.print("baseclasse"); } } class B extends A { void show() { System.out.print("sub class"); } } 3) Final Class The best example is String public class FinalKeyTutorial { public static void main(String[] args) { FinalEx f =new FinalEx(); f.a = 20; } } final class FinalEx{ int a = 10; } Happy coding...😉

Static Keyword in Java

Image
- Mainly used for Memory Management - Static variables are stored in non-heap memory, Non-static variables are stored in heap memory - Usages         1. Block         2. Variable         3. Method         4. Nested class Static Block - It's executed exactly once when the class is first loaded - It's executed before the main method at the time of classloading. Example: public class StaticKeyword { static { System.out.println("Static block initialized."); } public static void main(String[] args) { System.out.println("Main Method"); } } Response: Static block initialized. Main Method Static Variable - It's used for a constant variable or a method that is same for every instance of a class - It can be used to refer to the common property of all objects - It can be created at class-level only. (Not allowed to create a static local variable in member func...

Thread in Android

Thread A thread is the unit of execution within a process. Java provides two ways to create a thread programmatically. Way 1) Extending the java.lang.Thread class.    new ExampleThread().start();     //Step 1 - Extend the Thread base class     class ExampleThread extends Thread{         @Override         public void run() {             for (int i = 0; i < 10; i++) {                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }         }     } Way 2) Implementing the java.lang.Runnable interface. ...

Sqlite browser for Android development

Image
Get the SQLite DB file from Android studio Below android studio 3.5 Tool window bar -> " Device File explorer "  Above Android 3.5 You can find "Device File Explorer" in the bottom right corner of the screen open directory in  data/data/your-application-package/databases In the new architecture, 3 files are created in the databases directory database-name database-name-shm database-name-wal You have to export all these 3 files in the same directory then open the first one file (that is with database-name only ) in any SQLite browser. Offline SQLite browser Download DB Browser for SQLite free software for as per your operating system then open the "DB Browser for SQLite.exe" executable file You can import the database file and view the data as per the below sample screenshot Download:  https://sqlitebrowser.org/dl/ Stetho tool for Android debugging http://facebook.github.io/stetho/ http://gmariotti.blog...
Android Architecture (MVVM) with Room database (Java) Source code link: https://github.com/AndroidManikandan5689/android_architecture_mvvm_Java