— Scott McNealy, Sun Microsystems Chairman, President and ceo


get into the details later, for now just



Download 40,57 Mb.
Pdf ko'rish
bet82/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   78   79   80   81   82   83   84   85   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

get into the details later, for now just 
remember that the compiler won’t let you 
put the wrong thing in an array, based on 
the array’s declared type.


62
 
chapter 3
using 
references 
class Dog {
String name;
public static void main (String[] args) {
// make a Dog object and access it
Dog dog1 = new Dog();
dog1.bark();
dog1.name = “Bart”;
// now make a Dog array
Dog[] myDogs = new Dog[3];
// and put some dogs in it
myDogs[0] = new Dog();
myDogs[1] = new Dog();
myDogs[2] = dog1;
// now access the Dogs using the array
// references
myDogs[0].name = “Fred”;
myDogs[1].name = “Marge”;
// Hmmmm... what is myDogs[2] name?
System.out.print(“last dog’s name is “);
System.out.println(myDogs[2].name);
// now loop through the array
// and tell all dogs to bark
int x = 0;
while(x < myDogs.length) {
myDogs[x].bark();
x = x + 1;
}
}
public void bark() {
System.out.println(name + “ says Ruff!”);
}
public void eat() { }
public void chaseCat() { }
}
Dog
name
bark()
eat()
chaseCat()
A Dog example
File Edit Window Help Howl
%java Dog
null says Ruff!
last dog’s name is Bart
Fred says Ruff!
Marge says Ruff!
Bart says Ruff!
Output
 BULLET POINTS
ß
Variables come in two flavors: primitive and 
reference.
ß
Variables must always be declared with a name 
and a type. 
ß
A primitive variable value is the bits representing 
the value (5, ‘a’, true, 3.1416, etc.).
ß
A reference variable value is the bits 
representing a way to get to an object on the 
heap.
ß
A reference variable is like a remote control. 
Using the dot operator (.) on a reference 
variable is like pressing a button on the remote 
control to access a method or instance variable.
ß
A reference variable has a value of 
null
when 
it is not referencing any object.
ß
An array is always an object, even if the array 
is declared to hold primitives. There is no such 
thing as a primitive array, only an array that 
holds primitives.
arrays have a variable ‘length’ 
that gives you the number of 
elements in the array



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   78   79   80   81   82   83   84   85   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish