Introducing Java
Introducing Java
Introducing Java
Îëåêñié Òèõîìèðîâ
Âåðåñåíü 2013
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Çìiñò
Âñòóï
Çìiñò
Âñòóï
Çìiñò
Âñòóï
Çìiñò
Âñòóï
Çìiñò
Âñòóï
×îìó Java ?
Òðîõè iñòîði¨
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ïðèíöèïè Java
Java òà Android
Copyright by Sum Microsystems, image was borrowed from Java Tutorial and modyed by
O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Java òà Android
Copyright by Sum Microsystems, image was borrowed from Java Tutorial and modyed by
O. Tykhomyrov
Àðõèòåêòóðíà íåçàëåæíiñòü
Àðõèòåêòóðíà íåçàëåæíiñòü
Àðõèòåêòóðíà íåçàëåæíiñòü
Àðõèòåêòóðíà íåçàëåæíiñòü
Àðõèòåêòóðíà íåçàëåæíiñòü
API
Application Programming
Interface
API
Iíòåðôåéñ ïðîãðàìóâàííÿ
çàäà÷
Îá'¹êòíî-îði¹íòîâàíå ïðîãðàìóâàííÿ
Íàäiéíiñòü òà áåçïå÷íiñòü
Ðîçïîäiëåíi îá÷èñëåííÿ
Çìiñò
Âñòóï
Copyright by O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Copyright by O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Copyright by O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Copyright by O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Copyright by O. Tykhomyrov
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Çìiñò
Âñòóï
Äîêóìåíòàöiÿ ïðîãðàìè
Äîêóìåíòàöiÿ ïðîãðàìè
Ïîâíèé òåêñò çàäîêóìåíòîâàíî¨ ïðîãðàìè
/**
* Classical program to start <b>HelloWorld</b>
* It prints the line 'Hello World' in the console screen
and exits than.
* @author somewere from Sun Microsystems(tm)
* @version 1.0
*/
public class HelloWorld {
/**
* @param args the command line arguments
* @return nothing
*/
public static void main(String [] args) {
System.out.println("Hello World"); // Console output
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Äîêóìåíòàöiÿ ïðîãðàìè
Äîêóìåíòàöiÿ ïðîãðàìè
Äîêóìåíòàöiÿ ïðîãðàìè
Ùî çàáóëüáåíèâ javadoc:
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Äîêóìåíòàöiÿ ïðîãðàìè
/**
...Comments for inserting into the documentation...
*/
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Äîêóìåíòàöiÿ ïðîãðàìè
Íåïîãàíî!
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Çìiñò
Âñòóï
Áàçîâi òèïè
Áàçîâi òèïè
Áàçîâi òèïè
 Java âèçíà÷åíî äåâ'ÿòü áàçîâèõ òèïiâ (primitive):
÷îòèðè âàðiàíòà äëÿ öiëèõ çìiííèõ (integer)
Áàçîâi òèïè
Áàçîâi òèïè
Áàçîâi òèïè
9 void
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Áàçîâi òèïè
Ðÿäêè Strings
Çìiííi
int x;
double df = 0.33;
float ff = 0.33F;
char c = 'a';
long m;
String s = "Java is nice";
x = 55; m=55L;
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Êîíñòàíòè
Çìiñò
Âñòóï
Âèðàçè
àðèôìåòè÷íi
Java íàä๠áàãàòî âiäíîøåííÿ
ìîæëèâîñòåé äëÿ âèðàçè
çàïèñó âèðàçiâ. ïîáiòîâi
ëîãi÷íi
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Àðèôìåòè÷íi âèðàçè
-
Àðèôìåòè÷íi äi¨:
• äîäàâàííÿ (+) àðèôìåòè÷íi *
• âiäíiìàííÿ (-) /
• ìíîæåííÿ (*)
%
• äiëåííÿ (/)
âèðàçè
• îñòà÷à âiä äiëåííÿ âiäíîøåííÿ
(%).
ïîáiòîâi
ëîãi÷íi
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Àðèôìåòè÷íi âèðàçè
class Arithmetic {
public static void main(String[] args) {
int x = 12; int y = 2 * x; System.out.println(y);
int z = (y - x) % 7; System.out.println(z);
nal oat pi = 3.1415F; oat f = pi / 0.62F;
System.out.println(f); }
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Àðèôìåòè÷íi âèðàçè
class Arithmetic {
public static void main(String[] args) {
int x = 12; int y = 2 * x; System.out.println(y);
int z = (y - x) % 7; System.out.println(z);
nal oat pi = 3.1415F; oat f = pi / 0.62F;
System.out.println(f); }
}
}
Àðèôìåòè÷íi âèðàçè
Àðèôìåòè÷íi âèðàçè
Àðèôìåòè÷íi âèðàçè
Àðèôìåòè÷íi âèðàçè
Îïåðàòîðè âiäíîøåííÿ
Âèðàçè ç îïåðàòîðàìè
àðèôìåòè÷íi
âiäíîøåííÿ
• äîðiâíþ¹ ==
(equivalent) (==)
!=
• íå äîðiâíþ¹ (!=)
• ìåíø íiæ (<) <
âiäíîøåííÿ
• áiëüøå íiæ (>)
âèðàçè >
• ìåíø àáî äîðiâíþ¹
(<=) <=
• áiëüøå àáî >=
äîðiâíþ¹ (>=).
çàâæäè ïîâåðòàþòü ïîáiòîâi
ëîãi÷íå (áóëåâå)
ëîãi÷íi
çíà÷åííÿ
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîðè âiäíîøåííÿ
class Boolean {
public static void main(String[] args) {
int x = 12,y = 33;
System.out.println(x < y);
System.out.println(x != y - 21);
boolean test = x >= 10;
System.out.println(test); }
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîðè âiäíîøåííÿ
class Boolean {
public static void main(String[] args) {
int x = 12,y = 33;
System.out.println(x < y);
System.out.println(x != y - 21);
boolean test = x >= 10;
System.out.println(test); }
}
Ïðîãðàìà äðóêó¹ íàñòóïíå:
true
false
true
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
àðèôìåòè÷íi
Îïåðàòîðè:
• áóëåâié àëãåáðà: âiäíîøåííÿ
• ëîãi÷íå i &
• ëîãi÷íå àáî | &(and)
• ëîãi÷íå íi
|(or)
• çñóâ áiòiâ:
• çñóâ âëiâî >> âèðàçè !(not)
• çñóâ âïðàâî çi ïîáiòîâi
çíàêîì >> <<
• çñóâ âïðàâî ç >>
íóëåì >>>
char, short, byte ⇒ >>>()
int; ðåçóëüòàò int.
ëîãi÷íi
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class Bits1 {
public static void main(String[] args) {
int x = 0x16; // 00000000000000000000000000010110
int y = 0x33; // 00000000000000000000000000110011
System.out.println(x & y);// 00000000000000000000000000010010
System.out.println(x | y);// 00000000000000000000000000110111
System.out.println(~x); // 11111111111111111111111111101001
x &= 0xf; // 00000000000000000000000000000110
System.out.println(x); // 00000000000000000000000000000110
short s = 7; // 0000000000000111
System.out.println(~s); // 11111111111111111111111111111000
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class Bits2 {
public static void main(String[] args) {
int x = 0x16; //00000000000000000000000000010110
System.out.println(x << 3);//00000000000000000000000010110000
int y = 0xfe; //00000000000000000000000011111110
y >>= 4; //00000000000000000000000000001111
System.out.println(y); //00000000000000000000000000001111
x = 9; //00000000000000000000000000001001
System.out.println(x >> 3);//00000000000000000000000000000001
System.out.println(x >>>3);//00000000000000000000000000000001
x = -9; //11111111111111111111111111110111
System.out.println(x >> 3);//11111111111111111111111111111110
System.out.println(x >>>3);//00011111111111111111111111111110
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ëîãi÷íi îïåðàöi¨
class Logical {
public static void main(String[] args) {
int x = 12,y = 33;
double d = 2.45,e = 4.54;
System.out.println(x < y && d < e);
System.out.println(!(x < y));
boolean test = 'a' > 'z';
System.out.println(test || d - 2.1 > 0);
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class Logical {
public static void main(String[] args) {
int x = 12,y = 33;
double d = 2.45,e = 4.54;
System.out.println(x < y && d < e);
System.out.println(!(x < y));
boolean test = 'a' > 'z';
System.out.println(test || d - 2.1 > 0);
}
}
Îïåðàöi¨ ç ðÿäêàìè
• Ðîçãëÿíåìî ïðîãðàìó
class Strings {
public static void main(String[] args) {
String s1 = "Hello" + " World!";
System.out.println(s1);
int i = 35,j = 44;
System.out.println("The value of i is " + i +
" and the value of j is " + j);
}
}
• Ïðîãðàìà äðóêó¹
Hello World!
The value of i is 35 and the value of j is 44
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
• Ðîçãëÿíåìî ïðîãðàìó
class Strings {
public static void main(String[] args) {
String s1 = "Hello" + " World!";
System.out.println(s1);
int i = 35,j = 44;
System.out.println("The value of i is " + i +
" and the value of j is " + j);
}
}
• Ïðîãðàìà äðóêó¹
Hello World!
The value of i is 35 and the value of j is 44
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Çìiñò
Âñòóï
Îïåðàòîð if
class If {
public static void main(String[] args) {
char c = 'x';
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println("letter: " + c);
else if (c >= '0' && c <= '9')
System.out.println("digit: " + c);
else {
System.out.println("the character is: " + c);
System.out.println("it is not a letter");
System.out.println("and it is not a digit"); } }
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð if
class If {
public static void main(String[] args) {
char c = 'x';
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println("letter: " + c);
else if (c >= '0' && c <= '9')
System.out.println("digit: " + c);
else {
System.out.println("the character is: " + c);
System.out.println("it is not a letter");
System.out.println("and it is not a digit"); } }
}
Ïðîãðàìà äðóêó¹
letter: x
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð while
class While {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
float var = initialValue;
int counter = 0;
while (var < limit) { var += step; counter++; }
System.out.println("Incremented " + counter + " times");
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð while
class While {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
float var = initialValue;
int counter = 0;
while (var < limit) { var += step; counter++; }
System.out.println("Incremented " + counter + " times");
}
}
Ïðîãðàìà äðóêó¹
Incremented 22 times
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð for
class For {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
int counter = 0;
for (float var = initialValue;var < limit;var += step)
counter++;
System.out.println("Incremented " + counter + " times");
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð for
class For {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
int counter = 0;
for (float var = initialValue;var < limit;var += step)
counter++;
System.out.println("Incremented " + counter + " times");
}
}
Ïðîãðàìà äðóêó¹
Incremented 22 times
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class BreakContinue {
public static void main(String[] args) {
for (int counter = 0;counter < 10;counter++) {
// start a new iteration if the counter is odd
if (counter % 2 == 1) continue;
// abandon the loop if the counter is equal to 8
if (counter == 8) break;
// print the value
System.out.println(counter);
} System.out.println("done."); }
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class BreakContinue {
public static void main(String[] args) {
for (int counter = 0;counter < 10;counter++) {
// start a new iteration if the counter is odd
if (counter % 2 == 1) continue;
// abandon the loop if the counter is equal to 8
if (counter == 8) break;
// print the value
System.out.println(counter);
} System.out.println("done."); }
}
Ïðîãðàìà äðóêó¹
0 2 4 6 done.
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð switch
class Switch {
public static void main(String[] args) {
boolean leapYear = true;
int days = 0;
for(int month = 1;month <= 12;month++) {
switch(month) {
case 1: // months with 31 days
case 3: case 5: case 7: case 8: case 10:
case 12: days += 31; break;
case 2: // February is a special case
if (leapYear) days += 29; else days += 28; break;
default: days += 30; break; // it must be a month with 30 days
} } System.out.println("number of days: " + days); }
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Îïåðàòîð switch
class Switch {
public static void main(String[] args) {
boolean leapYear = true;
int days = 0;
for(int month = 1;month <= 12;month++) {
switch(month) {
case 1: // months with 31 days
case 3: case 5: case 7: case 8: case 10:
case 12: days += 31; break;
case 2: // February is a special case
if (leapYear) days += 29; else days += 28; break;
default: days += 30; break; // it must be a month with 30 days
} } System.out.println("number of days: " + days); }
}
Çìiñò
Âñòóï
Ìàñèâè
• Ó Java ìîæíà çàñòîñîâóâàòè ìàñèâè äàíèõ äëÿ çáåðåæåííÿ
äåêîãî ÷èñëà åëåìåíòiâ îäíàêîâîãî òèïó
int[] a; // an unitialized array of integers
float[] b; // an unitialized array of floats
String[] c; // an unitialized array of Strings
• Îïèñ ìàñèâó íå çàä๠éîãî ðîçìið. Íàâiòü ïàì'ÿòü íå
âèäiëÿ¹òüñÿ
• Ðîçìið ìàñèâó ìîæíà çàäàòè ïiä ÷àñ çàâäàííÿ éîãî ïî÷àòêîâèõ
çíà÷åíü
int[] a = {13,56,2034,4,55}; // size: 5
float[] b = {1.23F,2.1F}; // size: 2
String[] c = {"Java","is","great"}; // size: 3
• (Íå)Ìîæíà ðîçìiùóâàòè îãîëîøåííÿ â iíøèé ñïîñiá:
float anArrayOfFloats[]; // this form is discouraged}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ìàñèâè
• Ó Java ìîæíà çàñòîñîâóâàòè ìàñèâè äàíèõ äëÿ çáåðåæåííÿ
äåêîãî ÷èñëà åëåìåíòiâ îäíàêîâîãî òèïó
int[] a; // an unitialized array of integers
float[] b; // an unitialized array of floats
String[] c; // an unitialized array of Strings
• Îïèñ ìàñèâó íå çàä๠éîãî ðîçìið. Íàâiòü ïàì'ÿòü íå
âèäiëÿ¹òüñÿ
• Ðîçìið ìàñèâó ìîæíà çàäàòè ïiä ÷àñ çàâäàííÿ éîãî ïî÷àòêîâèõ
çíà÷åíü
int[] a = {13,56,2034,4,55}; // size: 5
float[] b = {1.23F,2.1F}; // size: 2
String[] c = {"Java","is","great"}; // size: 3
• (Íå)Ìîæíà ðîçìiùóâàòè îãîëîøåííÿ â iíøèé ñïîñiá:
float anArrayOfFloats[]; // this form is discouraged}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ìàñèâè
• Ó Java ìîæíà çàñòîñîâóâàòè ìàñèâè äàíèõ äëÿ çáåðåæåííÿ
äåêîãî ÷èñëà åëåìåíòiâ îäíàêîâîãî òèïó
int[] a; // an unitialized array of integers
float[] b; // an unitialized array of floats
String[] c; // an unitialized array of Strings
• Îïèñ ìàñèâó íå çàä๠éîãî ðîçìið. Íàâiòü ïàì'ÿòü íå
âèäiëÿ¹òüñÿ
• Ðîçìið ìàñèâó ìîæíà çàäàòè ïiä ÷àñ çàâäàííÿ éîãî ïî÷àòêîâèõ
çíà÷åíü
int[] a = {13,56,2034,4,55}; // size: 5
float[] b = {1.23F,2.1F}; // size: 2
String[] c = {"Java","is","great"}; // size: 3
• (Íå)Ìîæíà ðîçìiùóâàòè îãîëîøåííÿ â iíøèé ñïîñiá:
float anArrayOfFloats[]; // this form is discouraged}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ìàñèâè
• Ó Java ìîæíà çàñòîñîâóâàòè ìàñèâè äàíèõ äëÿ çáåðåæåííÿ
äåêîãî ÷èñëà åëåìåíòiâ îäíàêîâîãî òèïó
int[] a; // an unitialized array of integers
float[] b; // an unitialized array of floats
String[] c; // an unitialized array of Strings
• Îïèñ ìàñèâó íå çàä๠éîãî ðîçìið. Íàâiòü ïàì'ÿòü íå
âèäiëÿ¹òüñÿ
• Ðîçìið ìàñèâó ìîæíà çàäàòè ïiä ÷àñ çàâäàííÿ éîãî ïî÷àòêîâèõ
çíà÷åíü
int[] a = {13,56,2034,4,55}; // size: 5
float[] b = {1.23F,2.1F}; // size: 2
String[] c = {"Java","is","great"}; // size: 3
• (Íå)Ìîæíà ðîçìiùóâàòè îãîëîøåííÿ â iíøèé ñïîñiá:
float anArrayOfFloats[]; // this form is discouraged}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Ìàñèâè (2)
Ìàñèâè (2)
Ìàñèâè (2)
Ìàñèâè (2)
class Arrays {
public static void main(String[] args) {
int[] a = {2,4,3,1};
int sum = 0; // compute the summation of the elements of a
for(int i = 0;i < a.length;i++) sum += a[i];
float[] d = new float[sum];// create an array of floats with this size
// assign some values
for(int i = 0;i < d.length;i++) d[i] = 1.0F/(i+1);
// print the values in odd positions
for(int i = 1;i<d.length;i += 2) System.out.println("d["+i+"]="+d[i]);
}
}
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class Arrays {
public static void main(String[] args) {
int[] a = {2,4,3,1};
int sum = 0; // compute the summation of the elements of a
for(int i = 0;i < a.length;i++) sum += a[i];
float[] d = new float[sum];// create an array of floats with this size
// assign some values
for(int i = 0;i < d.length;i++) d[i] = 1.0F/(i+1);
// print the values in odd positions
for(int i = 1;i<d.length;i += 2) System.out.println("d["+i+"]="+d[i]);
}
}
d[1]=0.5
d[3]=0.25
d[5]=0.16666667
d[7]=0.125
d[9]=0.1
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
Áàãàòîâåêòîðíi ìàñèâè
class CommandArguments {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++)
System.out.println(args[i]); } }
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó
class CommandArguments {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++)
System.out.println(args[i]); } }
Ïðèêëàäè âèêîíàííÿ ïðîãðàìè:
# java CommandArguments Hello World
Hello
World
# java CommandArguments
# java CommandArguments I have 25 cents
I
have
25
cents
Âñòóï Îñîáëèâîñòi Java Ðîçðîáêà Java -ïðîãðàì Áàçîâi êîíñòðóêöi¨ ìîâè Àðãóìåíòè êîìàíäíîãî ðÿäêó