View RSS Feed

penguinCoder

Records Cheat Sheet

Rate this Entry
by , 10-27-2012 at 04:25 AM (1566 Views)
Brief Description:
A record is pretty much just a programmer defined data type. It combines one or more pieces of information, or potentially different data types, into one unit. Structure wise, it is just a template that defines how to interpret the contents of a sequence of memory locations.

First outside of any class, define the new data type:
Java Code:
class recordName{
  public [dataType] fieldName;
  ..repeat for any number of types..
}
An example of this would be:
Java Code:
class Employee{
  public String name;
  public int ID;
  public double salary;
}
Inside of the class you wish to use the record, declare a variable of that type:
Java Code:
recordName variableName;
An example of this would be:
Java Code:
public static void main(String[] args){
  Employee jon;
Create storage for that instance, and assign a reference to that storage location to a variable of that type:
Java Code:
variableName = new recordName;
An example would be:
Java Code:
jon = new Employee;
Last thing you have to do is just assign values to the fields of the record, one by one:
Java Code:
variableName.fieldName = value;
An example of this would be:
Java Code:
jon.name = "Jon Johnson";
jon.ID = 11223344;
jon.salary = 55000;
To use any of these values:
Java Code:
System.out.println(variableName.fieldName);
An example of this would be:
Java Code:
System.out.println("Employee Name: " + jon.name);
System.out.println("Employee ID: " + jon.ID);
System.out.println("Employee Salary: " + jon.salary);

Submit "Records Cheat Sheet" to Facebook Submit "Records Cheat Sheet" to Digg Submit "Records Cheat Sheet" to del.icio.us Submit "Records Cheat Sheet" to StumbleUpon Submit "Records Cheat Sheet" to Google

Comments