# 08\_references\_and\_packages

### Pointers vs java references

* Pointer arithmetic is not allowed in java.
* Reference holds internal representation of address

```java

  Box b1=new Box(1,2,3);
  //The following operations yield error
  b1++;
  b1 += 10;
  String s="hello";
  s *= 10;
```

***

**Array handling**

* Array of primitive types

```java
  double[] data = new double[10];
  
  //print array contents using for
  for(int i=0;i<data.length;i++)
    sop(data[i]);
  
  //print array contents using for-each (enhanced for loop)
  //d=data[0],d=data[1]....., d=data[data.length-1]
  for(double d : data) 
    sop(d);
```

* Array of References

```java
  Box[] boxes;
  boxes = new Box[sc.nextInt()];
  for(....){
    sop(...);
    boxes[i]=new Box(.....);
  }
  for(Box b : boxes) //b=boxes[0]
    b.displayDims();
```

***

### Packages

* What is a package ?
  * Collection of **functionally similar** classes & interfaces.
* Why packages?
  1. To group functionally similar classes together.
  2. Avoids name space collision
  3. Finer control over access specifiers.
*   About Packages

    * Creation : package statement has to be placed as the 1st statement in Java source.

    ```java
      package p1; // the classes will be part of package p1.
    ```

    *   Package names are mapped to folder names.

        ```java
          package p1; 
          class A{....}
        ```

        * `A` class must exist in folder `p1`.

3. For simplicity create folder p1 under `src` folder & compile from `src`

*   From `src` folder, run the command

    ```sh
        javac -d ..\bin p1\A.java
        #-d : flag used to specify the destination folder 
    ```
* javac will automatically create the sub-folder `p1` under the `bin` folder & place A.class within `p1`.

**NOTE: Its not mandatory to create java sources(.java) under package named folder. BUT its mandatory to store packged compiled classes(.class) under package named folders.**

* Earlier half is just maintained as convenience so that javac can then automatically detect dependencies & compile classes.
* To run the packaged classes from any folder, you must set Java specific environment variables : classpath

```sh
  set classpath=g:\dac1\day2\bin;
```

* **classpath** is the Java's only environment variable
* Class path is mainly used by JRE's classloader, to locate & load the classes.
* Class loader will try to locate the classes from current folder, if not found it will refer to classpath entries and try to resolve & load Java classes.
*   What should be value of classpath ?

    * Must be set to top of packged hierarchy _(i.e .class eg : bin)_

    ```sh
      set classpath=G:\dac\dac1\day2\bin;(cmd line invocation)
    ```

    * OR better, set it in environment variables.

**Rules about packages**

1. If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
2. If there are import statements, they must go between the package statement _(if there is one)_ and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.
3. If there are no package or import statements, the class declaration must be the first line in the source code file.
4.  import and package statements apply to all classes within a source code file.

    * In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.

    _Note: Normally package names are all lower cased._

* For installing open JDK, setting path and setting java home variable in Ubuntu. Refer [this](https://vitux.com/how-to-setup-java_home-path-in-ubuntu/).
* Linux/Mac classpath setting
  * Open the terminal window
  * echo ${CLASSPATH}
  * export CLASSPATH=.:/home/user/day2/bin _(folder name pointing to top of the pkged hierarchy)_.
* Another option can be, run the command
  * java -cp /home/user/day2/bin p1.Test
