Java Reference
In-Depth Information
If you wanted pt1 and pt2 to refer to separate objects, separate new Point() statements
could be used on lines 6 and 7 to create separate objects, as shown in the following:
pt1 = new Point(100, 100);
pt2 = new Point(100, 100);
References in Java become particularly important when arguments are passed to meth-
ods. You learn more about this later today.
There are no explicit pointers or pointer arithmetic in Java, as
there are in C and C++. By using references and Java arrays, how-
ever, most pointer capabilities are duplicated without many of their
drawbacks.
NOTE
Casting and Converting Objects and
Primitive Types
One thing you discover quickly about Java is how finicky it is about the information it
will handle. Like Morris, the perpetually hard-to-please cat in the old 9Lives cat food
commercials, Java methods and constructors require things to take a specific form and
won't accept alternatives.
When you are sending arguments to methods or using variables in expressions, you must
use variables of the correct data types. If a method requires an int , the Java compiler
responds with an error if you try to send a float value to the method. Likewise, if you're
setting up one variable with the value of another, they must be of the same type.
There is one area where Java's compiler is decidedly flexible:
String s. String handling in println() methods, assignment state-
ments, and method arguments is simplified by the concatenation
operator ( + ). If any variable in a group of concatenated variables is
a string, Java treats the whole thing as a String . This makes the
following possible:
float gpa = 2.25F;
System.out.println(“Honest, dad, my GPA is a “ + (gpa+1.5));
Using the concatenation operator, a single string can hold the text
representation of multiple objects and primitive data in Java.
NOTE
 
 
Search WWH ::




Custom Search