Java Reference
In-Depth Information
if (value.equals(allowedValues[i]))
return true ;
}
return false ;
}
}
Class Feature has two basic attribute: name and value . The association
between the feature and its type can be implemented by means of an attribute
( type ) that contains the reference to a FeatureType object (Sidebar 4.1).
This is a standard approach to implement one-to-one and many-to-one
associations.
Sidebar 4.1 Object variables
Java provides only one way of dealing with objects, i.e. through objects references
(while C!! provides three different ways, i.e. objects, objects pointers and object
references). The object variables in Java (i.e. variables not referencing primitive
types such as int , float , etc.) are not objects: they are just references to objects.
This has several consequences:
when an object variable is declared it refers to no object and cannot be used;
objects can be created only by means of the new operator;
a single object can be referenced by any number of object variables;
the equal operator ( ## ) compares object variables, not objects, therefore it is
true only if the object variables refer to the same object (or they are both null);
to compare objects instead of references the method equals() should be used,
not ## ;
an assignment between object variables only changes the reference. It does not
touch the object.
Therefore in the following examples all the print statements are executed:
String someText; //just a reference NOT an object
//if(someText ## null) System.out.println("1 TRUE");
// If the previous line is not commented,
// the compiler generates the following error:
// ...: variable someText might not have been initialized
someText # new String("some text"); // now an object exists
if (someText ! # null )
System.out.println("2 TRUE");
String theSameObject # someText;
// they both refer to the same object
if (someText ## theSameObject)
System.out.println("3 TRUE");
String theSameText # new String("some text");
// another object containing the same text
if (someText ! # theSameText)
System.out.println("4 TRUE");
Search WWH ::




Custom Search