Manage Data

Holding Data
Objets and Wrapper Classes
Objects: -An object is composed of fields and methods. The fields, also called data members, characteristics, attributes, or properties, describe the state of the object. The methods generally describe the actions associated with a particular object. Think of an object as a noun, its fields as adjectives describing that noun, and its methods as the verbs that can be performed by or on that noun.
Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
The objects are different from the primitive types because:
1 The primitive types are not instantiated.
2 In the memory, only their value is stored, directly. No reference to an instance is stored.
3 In the memory, the allocated space is fixed, whatever their value. The allocated space of an object vary, for instance either the object is intantiated or not.
4 The primitive types don't have methods callable on them.
5 A primitive type can't be inherited.
Classes: - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
A class specifies the design of an object. It states what data an object can hold and the way it can behave when using the data.
A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. Core properties include the data types and methods that may be used by the object. All class objects should have the basic class properties. Classes are categories, and objects are items within each category.A class declaration is made up of the following parts:
* Modifiers
* Class name
* Keywords
* Class body within curly brackets {}
This may be explained with a hypothetical example of a tree and types of trees. Generally, a tree should have branches, stems and leaves. Thus, if Banyan is a tree, Banyan should have all of the characteristics of a tree, such as branches, stems and leaves. It is impossible to say that a pigeon is a tree, because the pigeon does not have branches, stems and leaves. Similarly, basic Java object properties are defined within that object’s corresponding class.
Wrapper Class: - A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visualforce this type of class can be extremely useful

The wrapper classes in java servers two primary purposes. -
* To provide mechanism to ‘wrap’ primitive values in an object so that primitives can do activities reserved for the objects like being added to ArrayList, Hashset, HashMap etc. collection.
* To provide an assortment of utility functions for primitives like converting primitive types to and from string objects, converting to various bases like binary, octal or hexadecimal, or comparing various objects.
Naming conventions for Java wrapper classes
EGL creates a name in accordance with these rules: -
* If the name is all upper case, lower case all letters.
* If the name is a keyword, precede it with an underline
* If a hyphen or underline is in the name, remove that character and upper case the next letter
* If a dollar sign ($), at sign (@), or pound sign (#) is in the name, replace each of those characters with a double underscore (__) and precede the name with an underscore (_).
* If the name is used as a class name, upper case the first letter.
The following rules apply to dynamic array wrapper classes:
* In most cases, the name of a class is based on the name of the part declaration (data item, form, or record) that is the basis of each element in the array. For example, if a record part is called MyRec and the array declaration is recParms myRec[], the related dynamic array wrapper class is called MyRecArray.
* If the array is based on a declaration that has no related part declaration, the name of the dynamic array class is based on the array name. For example, if the array declaration is intParms int[], the related dynamic array wrapper class is called IntParmsArray.
This is a perfect scenario where a wrapper class can be used. Most of my instructions and comments are in the code but anyone should feel free to modify this entry to make it easier to understand.
Variables
A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a data type. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or boolean. And, every variable must be given an initial value before it can be used.
int myAge = 38;
The variable "myAge" is declared to be an int data type and initialized to a value of 38
There are three kinds of variables in Java: -
* Local variables
* Instance variables
* Class/static variables
Local variables : -
* Local variables are declared in methods, constructors, or blocks.
* Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
* Access modifiers cannot be used for local variables.
* Local variables are visible only within the declared method, constructor or block.
* Local variables are implemented at stack level internally.
* There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
Example: -
public class Test{
public void anuAge(){
int age = 0;
age = age +38;
System.out.println("Anuj age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.anuAge();
}
}
Output: -
Anuj age is: 38
Instance variables: -
* Instance variables are declared in a class, but outside a method, constructor or any block.
* When a space is allocated for an object in the heap, a slot for each instance variable value is created.
* Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
* Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.
* Instance variables can be declared in class level before or after use.
* Access modifiers can be given for instance variables.
* The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommended to make these variables private (access level). However visibility for subclasses can be given for these variables with the use of access modifiers.
* Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
* Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name
. ObjectReference.VariableName.
Example: -
import java.io.*;
public class Employee{
public String name;
private double salary;
public Employee (String empName){
name = empName;
}
public void setSalary(double empSal){
salary = empSal;
}
public void printEmp(){
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]){
Employee empOne = new Employee("Anuj");
empOne.setSalary(12000);
empOne.printEmp();
}
}
Output: -
name : Anuj
salary :12000
Class/static variables:
* Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
* There would only be one copy of each class variable per class, regardless of how many objects are created from it.
* Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value.
* Static variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.
* Static variables are created when the program starts and destroyed when the program stops.
* Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
* Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks.
* Static variables can be accessed by calling with the class name . ClassName.VariableName.
* When declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables.
Example: -
import java.io.*;
public class Employee{
private static double salary;
public static final String DEPARTMENT = "Development ";
public static void main(String args[]){
salary = 12000;
System.out.println(DEPARTMENT+"average salary:"+salary);
}
}
Output: -
Development average salary:12000
Command -Line Arguments
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.
The command-line argument is an argument i.e. passed at the time of running the java program.
The arguments passed from the console can be received in the java program and it can be used as an input.
So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Example: -
class A
{
public static void main(String args[])
{
for(int i=0 ; i < args.length ; i++)
System.out.println(args[i]);
}
}
compile by -> javac A.java
run by -> java A Anuj 38 12000 Alwar
Output: -
Anuj
38
12000
Alwar
Garbage Collection
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. This enables faster development with less boilerplate code, while eliminating memory leaks and other memory-related problems. At least in theory.
Ironically, Java garbage collection seems to work too well, creating and removing too many objects. Most memory-management issues are solved, but often at the cost of creating serious performance problems. Making garbage collection adaptable to all kinds of situations has led to a complex and hard-to-optimize system. In order to wrap your head around garbage collection
We start with the heap, which is the area of memory used for dynamic allocation. In most configurations the operating system allocates the heap in advance to be managed by the JVM while the program is running. This has a couple of important ramifications:
* Object creation is faster because global synchronization with the operating system is not needed for every single object. An allocation simply claims some portion of a memory array and moves the offset pointer forward (see Figure 2.1). The next allocation starts at this offset and claims the next portion of the array.
* When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.

New objects are simply allocated at the end of the used heap
All objects are allocated on the heap area managed by the JVM. Every item that the developer uses is treated this way, including class objects, static variables, and even the code itself. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.
Garbage-Collection Roots
There are four kinds of GC roots in Java: -
* Local variables are kept alive by the stack of a thread. This is not a real object virtual reference and thus is not visible. For all intents and purposes, local variables are GC roots.
* Active Java threads are always considered live objects and are therefore GC roots. This is especially important for thread local variables.
* Static variables are referenced by their classes. This fact makes them de facto GC roots. Classes themselves can be garbage-collected, which would remove all referenced static variables. This is of special importance when we use application servers, OSGi containers or class loaders in general.
* JNI References are Java objects that the native code has created as part of a JNI call. Objects thus created are treated specially because the JVM does not know if it is being referenced by the native code or not. Such objects represent a very special form of GC root
Marking and Sweeping Away Garbage
To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm. It has two-step process:.
* The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
* All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.


Free Web Hosting