Variable Scopes, Storage Class, Recursion in Java (PDF)
Document Details

Uploaded by CostEffectivePanther
Tags
Summary
This document explains variable scopes, storage classes, and recursion in Java. It covers instance variables, local scopes, static variables, and method parameters. It also discusses JVM memory management including heap, method area, JVM stacks, native method stacks and program counter registers.
Full Transcript
T E C H N O L O G Y P R E S E N T A T I O N T E M P L A T E 01 STUDIO SHODWE S L I D E 02 STUDIO SHODWE The scope of variables is the part of the...
T E C H N O L O G Y P R E S E N T A T I O N T E M P L A T E 01 STUDIO SHODWE S L I D E 02 STUDIO SHODWE The scope of variables is the part of the Our System program where the variable is accessible. S L I D E 02 STUDIO SHODWE In Java, all identifiers are lexically (or statically) scoped, i.e. scope of a variable can be determined at compile time and Our System independent of the function call stack. S L I D E 02 T E C H N O L O G Y P R E S E N T A T I O N T E M P L A T E 01 STUDIO SHODWE ( ) These variables must be declared inside class (outside any Our System function). They can be directly accessed anywhere in class. S L I D E 02 STUDIO SHODWE The general scope of an instance variable is throughout the class except in static methods. The lifetime of an Our System instance variable is until the object stays in memory. S L I D E 02 03 STUDIO SHODWE Declared inside of a method, constructor, or code block. Only the precise block in which they are defined is accessible. The local variable exits Our System the block's scope after it has been used, and its memory is freed. S L I D E 02 Temporary data is stored in local variables, which are frequently initialised in the Our System block where they are declared. S L I D E 02 The Java compiler throws an error if a local variable is not initialised before being used. The range of local variables is the Our System smallest of all the different variable types. 02 Our System S L I D E 02 03 STUDIO SHODWE a type of class variable shared across Our System instances. S L I D E 02 Static Variables are the variables which once declared can be used anywhere even outside the class without initializing the class. Unlike Our System Local variables it scope is not limited to the class or the block. 02 03 STUDIO SHODWE Variables that are supplied to a method when it is invoked Our System are known as method parameters S L I D E 02 They serve as inputs for method execution and are used to receive values from the caller. The scope of method parameters is restricted to Our System the method in which they are defined, making them local to that method. 02 The values of the arguments given are allocated to the respective parameters when a method is called. Our System 02 03 STUDIO SHODWE A variable declared inside pair of brackets “{” and “}” in a Our System method has scope within the brackets only. S L I D E 02 03 S L I D E 02 STUDIO SHODWE is a fundamental concept that involves the automatic allocation and deallocation of Our System objects, managed by the Java Virtual Machine (JVM) S L I D E 02 STUDIO SHODWE ( ) uses a garbage collector to automatically remove Our System unused objects, freeing up memory in the background S L I D E 02 STUDIO SHODWE VM defines various run time data area which are Our System used during execution of a program. S L I D E 02 STUDIO SHODWE Some of the areas are created by the JVM whereas some are Our System created by the threads that are used in a program. S L I D E 02 STUDIO SHODWE However, the memory area created by JVM is destroyed only when the JVM exits. The data areas of thread are created Our System during instantiation and destroyed when the thread exits. S L I D E 02 These areas include: STUDIO SHODWE Heap Area Method Area JVM Stacks Native Method Stacks Our System Program Counter (PC) Registers S L I D E 02 STUDIO SHODWE Heap is a shared runtime data area where objects and arrays are stored. It is created when the JVM starts. The memory in the heap is allocated for all the class instances and arrays. Our System Heap can be of fixed or dynamic size depending upon the system’s configuration. S L I D E 02 STUDIO SHODWE JVM allows user to adjust the heap size. When the new keyword is used the object is allocated in the heap and its reference is stored in the Our System stack. There exists one and only one heap for a running JVM process. S L I D E 02 STUDIO SHODWE Scanner sc = new Scanner(System.in) Here, the Scanner object is stored in the heap and the reference sc is Our System stored in the stack Note: Garbage collection in heap area is mandatory. S L I D E 02 STUDIO SHODWE Method area is a logical part of the heap and it is created when the JVM starts. Method area is used to store class- Our System level information such as class structures, Method bytecode, Static variables, Constant pool, Interfaces. S L I D E 02 STUDIO SHODWE Method area can be of fixed or dynamic size depending on the system’s configuration. Note: Though method area is logically Our System a part of heap, it may or may not be garbage collected even if garbage collection is compulsory in heap area. S L I D E 02 STUDIO SHODWE A stack is created when a thread is created, and the JVM stack is used to store method execution data, including local variables, method Our System arguments, and return addresses Each Thread has its own stack, ensuring thread safety. S L I D E 02 STUDIO SHODWE Stacks size can be either fixed or dynamic, and it can be set when the stack is created. Our System The memory for stack needs not to be contiguous. S L I D E 02 STUDIO SHODWE Once a method completes execution, its associated stack frame is removed Our System automatically. S L I D E 02 STUDIO SHODWE This memory is allocated for each thread when it is created and can have either a fixed or dynamic size. Native method stacks handle the Our System execution of native methods that interact with the Java code. S L I D E 02 STUDIO SHODWE Native method stack is also known as C stacks. Native method stacks are Our System not written in Java language S L I D E 02 STUDIO SHODWE ( ) This memory is allocated for each thread when it is created and can have either a fixed or dynamic size. Our System S L I D E 02 S L I D E 02 STUDIO SHODWE is a process in which a method calls itself continuously. A method that calls itself is called a recursive method. Our System It is a powerful concept often used in algorithms and problem-solving. S L I D E 02 There are two types of parameters one is Formal parameters and the second is Actual Parameters. Formal parameters are those parameters that are defined during function definition and Actual parameters are those which are passed Our System during the function call in other Function. S L I D E 02 C/C++ supports the call by reference because in the call by reference we pass the address of actual parameters in the place of formal Our System parameters using Pointers. S L I D E 02 While Java does not support Pointers that’s why Java does not support Call by Our System Reference S L I D E 02 However, for objects, the "value" being passed is a reference to the object, not the object itself. This sometimes creates confusion about whether Java is Our System call-by-reference or call-by- value. S L I D E 02 When using recursion with objects in Java, you're passing references by value, Our System which means: S L I D E 02 Changes to the object's state within the recursive method will be visible outside the method Reassigning the parameter to a new object within the method Our System won't affect the original reference S L I D E 02 STUDIO SHODWE Syntax: returntype methodname(){ Our System //code to be executed methodname();//calling same method } S L I D E 02 S L I D E 02 To mimic call by reference behavior in Java for recursion, you need to work with wrapper classes or containers, Our System since Java is inherently pass- by-value. S L I D E 02 The key approach is to use a container class (like an array or a custom wrapper class) to hold the values Our System you want to modify. S L I D E 02 Since the container itself is passed by value but the reference points to the same object, changes to the object's Our System contents will be visible outside the method. S L I D E 02 S L I D E 02 S L I D E 02 S L I D E 02 S L I D E 02 S L I D E 02 STUDIO SHODWE T E C H N O L O G Y P R E S E N T A T I O N T E M P L A T E S L I D E 15