- Strona pocz±tkowa
- Miej sśÂ‚ośÂ„ce w sercu
- Joanna Chmielewska BuśÂ‚garski bloczek
- Aubrey Ross [Alpha Colony 04] Uninhibited Fire (pdf)
- Heinlein, Robert A For Us The Living
- Milan Ryzl Parapsychologia praktyczna
- Jadwiga Courths Mahler ZakśÂ‚adniczka
- Nora Roberts Tajemnicza gwiazda
- Foster, Alan Dean Icerigger 2 Mission to Moulokin
- Jeffrey Lord Blade 21 Champion of the Gods
- 089 Odwolac poszukiwania
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- b1a4banapl.xlx.pl
[ Pobierz całość w formacie PDF ]
an object. We can check to see whether an object has been instantiated by using the
keywordnull, as the following revised code shows:
public class Book {
private String title;
public String getTitle() {
return title;
}
public static void main(String [] args) {
Book b = new Book();
String s = b.getTitle(); // Compiles and runs
if (s != null) {
String t = s.toLowerCase();
}
}
}
The preceding code checks to make sure the object referenced by the variables
is notnullbefore trying to use it. Watch out for scenarios on the exam where you
might have to trace back through the code to find out whether an object reference
will have a value ofnull. In the preceding code, for example, you look at the instance
variable declaration for title, see that there s no explicit initialization, recognize that
the title variable will be given the default value ofnull, and then realize that the
variable s will also have a value ofnull. Remember, the value of s is a copy of
the value of title (as returned by thegetTitle()method), so if title is a null
reference, s will be too.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 1
Composite Default screen
Chapter 1: Language Fundamentals
36
Array Instance Variables
An array is an object; thus, an array instance variable that s declared but not explicitly
initialized will have a value ofnull, just as any other object reference instance variable.
But& if the array is initialized, what happens to the elements contained in the array?
All array elements are given their default values the same default values that elements
of that type get when they re instance variables. The bottom line: Array elements are
always always always given default values, regardless of where the array itself is declared
or instantiated. By the way, if you see the word always three times in a row, reread
the sentence three times. Now, once more, with feeling!
If we initialize an array, object reference elements will equalnullif they are not
initialized individually with values. If primitives are contained in an array, they will
be given their respective default values. For example, in the following code, the array
year will contain 100 integers that all equal zero by default:
public class BirthDays {
static int [] year = new int[100];
public static void main(String [] args) {
for(int i=0;i
System.out.println("year[" + i + "] = " + year[i]);
}
}
When the preceding code runs, the output indicates that all 100 integers in the
array equal zero.
Local (Stack, Automatic) Primitives and Objects
Local variables are defined within a method, including method parameters.
Automatic is just another term for local variable. It does not mean
the automatic variable is automatically assigned a value! The opposite
is true; an automatic variable must be assigned a value in the code;
otherwise, the compiler will complain.
Local Primitives
In the following time travel simulator, the integer year is defined as an automatic
variable because it is within the curly braces of a method.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 1
Composite Default screen
Using a Variable or Array Element That Is Uninitialized and Unassigned (Exam Objective 4.5)
37
public class TimeTravel {
public static void main(String [] args) {
int year = 2050;
System.out.println("The year is " + year);
}
}
Okay, so we ve still got work to do on the physics. Local variables, including primitives,
always always always must be initialized before you attempt to use them (though not
necessarily on the same line of code). Java does not give local variables a default value;
you must explicitly initialize them with a value, as in the preceding example. If you
try to use an uninitialized primitive in your code, you ll get a compiler error:
public class TimeTravel {
public static void main(String [] args) {
int year; // Local variable (declared but not initialized)
System.out.println("The year is " + year); // Compiler error
}
}
Compiling produces the following output:
%javac TimeTravel.java
TimeTravel.java:4: Variable year may not have been initialized.
System.out.println("The year is " + year);
1 error
[ Pobierz całość w formacie PDF ]