Computer Science Canada

Structures/Unions in Java?

Author:  Kuntzy [ Thu Sep 23, 2004 10:01 am ]
Post subject:  Structures/Unions in Java?

Is there sumthing like a structure or union in Java ... compariable to
code:
type point1 :
  record
     x, y, z : real
  end record

in turing?

Author:  Martin [ Thu Sep 23, 2004 11:09 am ]
Post subject: 

It's called a class.

Author:  TheZsterBunny [ Thu Sep 23, 2004 8:31 pm ]
Post subject: 

Martin,

Can you please elaborate on that. Classes have not been extensively covered in ...class.

Could you possibly give a basic explanation as to what a class really is?

-Z

Author:  wtd [ Thu Sep 23, 2004 8:47 pm ]
Post subject: 

A class is more or less a blueprint for an object. It describes an advanced data structure which contains both data and operations on that data.

Aside from the ability to include operations in the class definiton, the primary difference between the idea of structs and unions, and classes, is that the former is merely a data structure. It clumps several pieces of related data together and allows them to be moved around like a single piece of data. It leaves all operations on that data to the rest of the program, and trusts the programmer not to corrupt the data.

A class typically encapsulates its data and makes it inaccessible to the rest of the program. All operations on that data are then carried out by an interface which the class also defines.

Consider the simple example:

(In both C and Pascal style programming languages suited to the different styles of programming, as well as O'Caml because it's fun)

code:
/* Standard C, no classes */

#include <stdio.h>

typedef struct pair_of_ints_t
{
   int first;
   int second;
} pair_of_ints;

int max(pair_of_ints pair)
{
   if (pair.first > pair.second)
      return pair.first;
   else
      return pair.second;
}

int main()
{
   pair_of_ints pair = {42, 27};
   int max_int = max(pair);
   printf("%d\n", max_int);
}


code:
-- Ada95 example without classes

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Test is
        type Pair_Of_Int is
        record
                First, Second : Integer;
        end record;
                                                                                   
        function Max(Pair : in Pair_Of_Int) return Integer is
        begin
                if Pair.First > Pair.Second then
                        return Pair.First;
                else
                        return Pair.Second;
                end if;
        end Max;
                                                                                   
        Pair : Pair_Of_Int := (42, 27);
        Max_Int : Integer;
begin
        Max_Int := Max(Pair);
        Ada.Integer_Text_IO.Put(Max_Int);
        New_Line;
end Test;


code:
(* non-OO O'Caml *)

type pair_of_int = { first:int; second:int }

let max p =
   if p.first > p.second then p.first
   else p.second

let pair = { first=42; second=27 }
let max_int = max pair
print_int max_int
print_newline ()


code:
// Java

import java.lang.*;
import java.io.*;

public class Test {
   private static class PairOfInts {
      private int first;
      private int second;

      public PairOfInts(int f, int s) {
         first = f;
         second = s;
      }

      public int max() {
         if (first > second)
            return first;
         else
            return second;
      }
   }

   public static void main(String[] args) {
      PairOfInts pair = new PairOfInts(42, 27);
      int maxInt = pair.max();
      System.out.println(maxInt);
   }
}


code:
-- Smart Eiffel (definitely with classes)

class PAIR_OF_INT
creation { ANY }
   make
feature { ANY }
   make(f, s : INTEGER) is
      do
         first := f
         second := s
      end
   max : INTEGER is
      do
         if first > second then
            Result := first
         else
            Result := second
         end
      end
feature { NONE }
   first, second : INTEGER
end

class TEST
creation { ANY }
   make
feature { ANY }
   make is
      local
         pair : PAIR_OF_INT
         max_int : INTEGER
      do
         create pair.make(42, 27)
         max_int := pair.max
         std_output.put_integer(max_int)
         std_output.put_new_line
      end
end


code:
(* OO O'Caml *)

class pair_of_int (first:int) (second:int) =
object
   method max = if first > second then first else second
end

let pair = new pair_of_int 42 27
let max_int = pair#max
print_int max_int
print_newline ()

Author:  Andy [ Thu Sep 23, 2004 9:16 pm ]
Post subject: 

basicly what it does is it describes the general properties of an object... like wtd says, its the blue print... since everything in oop r objects, a class simply stores info about the object and what it could do

Author:  wtd [ Thu Sep 23, 2004 10:10 pm ]
Post subject: 

Both the struct/union approach and classes can make use of composition, where part of one complex data structure is another data structure.

However, structs and unions can't (in most languages at least) make use of inheritance, where one data structure is another data structure, with additional features.

Let's say I want to add a PairOfInt class that allows the two integers to be switched.

code:
// Java

import java.lang.*;

public class Test {
   private static class PairOfInts {
      private int first;
      private int second;

      public PairOfInts(int f, int s) {
         first = f;
         second = s;
      }

      public int max() {
         if (first > second)
            return first;
         else
            return second;
      }
   }

   public static class SwappablePairOfInts extends PairOfInts {
      public SwappablePairOfInts(int f, int s) {
         super(f, s);
      }

      public void swap() {
         int temp = first;
         first = second;
         second = temp;
      }
   }

   public static void main(String[] args) {
      SwappablePairOfInts pair = new SwappablePairOfInts(42, 27);
      pair.swap();
   }
}


code:
-- Smart Eiffel (definitely with classes)

class PAIR_OF_INT
creation { ANY }
   make
feature { ANY }
   make(f, s : INTEGER) is
      do
         first := f
         second := s
      end
   max : INTEGER is
      do
         if first > second then
            Result := first
         else
            Result := second
         end
      end
feature { NONE }
   first, second : INTEGER
end

class SWAPPABLE_PAIR_OF_INT
inherit PAIR_OF_INT
creation { ANY }
   make
feature { ANY }
   swap is
      local
         temp : INTEGER
      do
         temp := first
         first := second
         second := temp
      end
end

class TEST
creation { ANY }
   make
feature { ANY }
   make is
      local
         pair : SWAPPABLE_PAIR_OF_INT
      do
         create pair.make(42, 27)
         pair.swap
      end
end


code:
(* in O'Caml *)

class pair_of_int (init_first:int) (init_second:int) =
object
   val mutable first = init_first
   val mutable second = init_second
   method max = if first > second then first else second
end

class swappable_pair_of_int init_first init_second =
object(self)
   inherit pair_of_int init_first init_second as super
   method swap = let temp = first in
      first <- second;
      second <- temp
end

let pair = new swappable_pair_of_int 42 27
pair#swap


: