Statements and Flow

Statements

  • A sequence of tokens recognized by the compiler as a complete instruction
  • A statement does not have a value
  • Example statements: x = 5

Blocks

  • Syntax of many Java constructs allows only one single statement to be used
  • Blocks allow multiple statements to be combined into what compiler recognizes as a single statement
  • Blocks begin with open curly brace and ened with closing curly brace

If statement

  • Conditional branching

Switch statement

  • Used for jumping to a certain branch of code

    While Loop

  • Used to iterate until a condition is no longer met
  • Loop body may not ever be executed

Functions

  • Allow for modular programming
  • Write function once, callit many times
  • Group similar things together
  • Parameters allow values from calling function to be used within function
  • May, or may not be return a value to calling function
  • General function template:
return_type function_name(formal parameter list)
{
  function code...
}
public static int computerFactorial(int val)
{
  int num;
  int fact;

  return fact;
}

Java is only Pass-by-Value

  • Java passes all parameters by value
  • There is no concept of pass-by-reference in Java
  • Changing an object reference parameter in a method does not change the object reference in the calling function
  • Changing the object's state in a method does change the object state in the calling function
  • Changing a built-in type parameter in a method does not affect the value in the calling function

Constructor

  • A class can have more than one constructor
  • Called once per object creation
  • Unique methods
  • A constructor will be called a every object creation
  • Value constructors: constructors that take input via one or more parameters

Implicit Constructor will Not Exist IF Explicit Constructor is declared.

public class InitializerClass
{
  int x = 4;
  double y;
  InitializerClass(double denom)
  {
    y = x / denom;
  }
}

public static void main(String [] args)
{
  InitializerClass initializerClass = new InitializerClass();
  // THROWS AN ERROR, Unexpected symbol
  // Explicit constructor declared, implicit will not run
  // Can be fixed by declaring non-paramterized constructor also
}

Object Instantiation

  • All object declarations are only a REFERENCE to an object
  • New objects must be instantiated using the new operator
  • When an object is instantiated, the following sequence occurs
  • Memory for the object is allocates
  • All bytes of the allocated memory is ceared
  • Constructor is called, but statements in constructor not executed yet

No Destructors

  • Java does not have the concept of a destructor
  • Automatic garbage collection
  • Objects are 'freed' when Java happens to check and an oibjct's memory is not being referred to by an object reference.

Good Practice

  • Set an object reference to null. This will tell the garbage collector that the reference to this object is no longer necessary.
  • Not necessary though, will free itself when variable is out of scope

Static Members

  • Data members are declared as static are called class data
  • all objects of the class share single instance of the static data
  • Therefore, you can also think of the data as belonging to the class objects as a whole
  • An attribute that contains the next available unique identifier
  • You want to ensure that every object of a certain type that gets created is assigned a unique id value
  • You could always remember that object was created last, get that object id value, and add one
  • Instead, use a static member variable to maintain a class attribute that will keep track of what the next available unique id is

Static Methods

  • Non-static functions IS allowed to access static members
public class SaticClass
{
  private static int nextId = 100;
  int id;

  public UniqueIDPerson(String inName)
  {
    name = inName;
    id = nextId // this is allowed
    nextId++;
  }

  public static int getNextId()
  {
    int inId = id; // Not allowed, static function cannot access non-static member
  }
}

Final Variables

  • Final is analogous to C++'s const
  • Modifies a variable so that it can not be assigned a different value from its initialization
  • For built-in data types, it essential makes the variable a constant
  • For object references, it prevents the variable from referring to another object
  • Should be initialized at declaration time.
  • Blank final variables are not given a value at declaration-time
    • This is different from C++ because C++ const requires you to have a value set at compile-time.
    • May be assigned once and only once
  • Best Practices: Set input-only parameters to functions to be final to make it obvious they are input parameters

Enumerated Types

  • Introduced in Java 1.5 (newish)
  • Allows a new data type to be created
  • Variables of an enumerated type can only be set to one of a discrete set of programmer-defined values
  • Could use final ints instead
  • Enums enforce only allowable values from being assigned though
  • Printing an enum prints a string of the programmer-defined value
  • Basically classes
enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNSEDAY, THURSDAY, FRIDAY, SATURDAY };

Importing Types

  • Import is used to allow a source file to recognize types from other source files without specifying full package name
  • import static is used to allow a source file to recognize static data and individual enum values from another source file
  • Consider the following:
package eecs285.test;

public class months
{
  enum MonthEnum { JAN, FEB, MAR }
}

Don't use static import if...

  • We're importing the class

Use static if...

  • We're importing an object

results matching ""

    No results matching ""