JavaBasics1

Java Basics 1.01b

Garbage Collection

  • Objects referenced created in heap memory
  • As long as variable referenced, it’s retained
  • When referenced expire, they’re eligible to be garbage collected
  • Garbage Collector runs own thread
  • Can’t force garbage collection
  • OutOfMemoryError thrown if memory runs out

Expiration

  • Variable to local functions or blocks expire when function is complete
  • Set value to null

Tips for Managing Memory

Java Basics 1.22

Compiling and Running

Java package statement implies the directory structure where it exists within the project.

Should be unique

  • package com.lynda.javatraining;
  • \src\com\lynda\javatraining\HelloWorld.java

 

When compiling, use javac in the project root:

  • C:\JavaProjects\HelloWorld>javac com\lynda\javatraining\HelloWorld.java
    • HelloWorld.class
    • HelloWorld.java

When running, use package reference and filename without “.java” extension

Java Basics 1.21

Installing Java and Eclipse on Ubuntu Linux: 2013 Updates

 

Do you have a JDK installed? You likely want to put $JDK_HOME/bin on your PATH, not the /bin of a JRE, as jar comes with JDK, not JRE.

Do this:

Java Basics 1.20

JUnit Class for testing:

  • Annotations
    • @Test, @Before, @After, @BeforeClass, @AfterClass, @Ignore

 

Code:

import static org.junit.Assert.*;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Ignore;

import org.junit.Test;

 

 

public class myJUnit1 {

    //No main() method, so JUnit will take over

   

    @BeforeClass

    public static void mBeforeTestClass(){

                 System.out.println("--------ClassBegin------------------");

    }

    //Annotation: Before EACH @Test

    @Before

    public void mBeforeTest(){

Java Basics 1.19

Creating JARs

  • Build Project
  • File > Export

 

 

Don’t select Eclipse .classpath or .project

 

For Debugging...select these

 

Manifest file has project metadata

Java Basics 1.17

File I/O: Copy a Text File

Code:

package com.lynda.files;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

public class CopyFile {

 

Java Basics 1.16

Interfaces

  • Allows definition of classes’ structure
    • final fields
    • method names
    • return data type
  • Interfaces provides definition for creating classes
    • Allows for polymorphism due to similarities
  • While <ArrayList> might be declared & used in the main logic,
    • helper classes can use Collection<T> so that other data types can be fed to it
    • such as
      • HashMap, Array, Enum

Java Basics 1.15

Casting Objects

  • As in conversion...upward/downward (int -> long / long -> int)
  • casting
    • upcasting - subclass as superclass (SAFE)
    • downcasting - superclass as subclass (RISKY)

 

    //Downcasting - will cause compiler error

      Kalamata olive1 =olives.get(0);

 

    //Downcasting Excplicitly

      Kalamata olive1 = (Kalamata)olives.get(0);

 

Java Basics 1.14

Overriding Methods (super.something();)

Code: Olive.java

package com.lynda.olivepress.olives;

 

public class Olive {

   

    public static final long BLACK = 0x000000;

    public static final long GREEN = 0x00FF00;

   

    public String name = "Kalamata";

    public String flavor = "Grassy";

    public long color = Olive.BLACK;

    private int oil = 3;

   

Subscribe to JavaBasics1