Назад Зміст Вперед

Урок 1.2. Оголошення змінних. Ідентифікатори. Ключові слова. Типи даних String, int. Oператор System.out.print(). Конкатенація

План уроку

1.Заміна виведення літерального імені (в лапках “ ”) виведенням змінної name типу String (class NameOutput_2). Використання коментарів для задокументовування зайвого коду.
2.Склад рядку оголошення змінної: тип, ім’я, символ присвоєння “=”, ініціалізаційне значення.
3.Оголошення та виведення цілої змінної типу int (class Variables_1).
4.Правила написання ідентифікаторів (class Variables_2, тест [task-mughal] № 2.3).
5.Ключові слова (class Variables_3).
6.Арифметичне додавання, віднімання, множення цілих змінних, в т.ч. всередині оператора System.out.println(); (class Variables_4).
7.Виведення комбінованих даних в одному рядку, оператор System.out.print(); (class Variables_5).
8.Конкатенація (class Variables_6, тест [Sanghera, p.356/383 (text/pdf), #67]), подвійна функціональність символу “+”: 1)арифметичне додавання, 2)конкатенація (“зчеплення”) змінних типу String.  

Програми:

public class NameOutput_2 {
            public static void main(String args[]) {
                        //System.out.println("Serg");
//заміна літерального імені змінною
                        String name = "Serg";
System.out.println(name);
            }
}
*******************************************************************
public class Variables_1 {
            public static void main(String args[]) {
//оголошення та виведення змінної типу int
int x = 10;
System.out.println(x);
            }
}
*******************************************************************
public class Variables_2 {
            public static void main(String args[]) {
//приклади неправильних або некоректних ідентифікаторів
int dog Price = 100; // неправильно – ідентифікатор містить пробіл
int dog-Price = 100; // неправильно – ідентифікатор містить тире
int DogPrice = 100; // правильно, проте некоректно: порушено Code
                                   //Convention ідентифікатор починається з            
                                   //прописної букви
            }
}
*******************************************************************
public class Variables_3 {
            public static void main(String args[]) {
//приклади неправильних ідентифікаторів, що використовують ключові слова
int assert = 100; // неправильно – ідентифікатор є ключовим словом
            }
}
*******************************************************************
public class Variables_4 {
            public static void main(String args[]) {
//приклади арифметичних операцій
int x = 100,
int y = 8;
int z = x+y;
System.out.println(z);
System.out.println(x-y);
System.out.println(x*y);
            }
}

*******************************************************************
public class Variables_5 {
            public static void main(String args[]) {
//виведення декількох змінних в одному рядку оператором System.out.print();
int x = 100;
int y = 8;
System.out.print(“x + y = ”);
System.out.print(x+y);
            }
}
*******************************************************************
public class Variables_6 {
            public static void main(String args[]) {
//особливості конкатенації;
int x = 100;
int y = 8;
// System.out.println(x + y = ”, x+y);   //не компілюється
// System.out.println(“x + y = ”+x+y);   //компілюється, але вивід
                                                                          //невірний:  x + y = 1008
// System.out.println(“x + y = ”+(x+y)); //компілюється, вивід вірний
            }
}

Література для класної роботи:

Тести для класної роботи
[task-mughal] – №2.3 (п.4):

Is this a complete and legal comment? Select the one correct answer.
 
/* // */
a.    No, the block comment (/* ... */) is not ended since the single-line comment (// ...) comments out the closing part.
b.    It is a completely valid comment. The // part is ignored by the compiler.
c.    This combination of comments is illegal and the compiler will reject it.




Література для домашньої роботи:

Тести для домашньої роботи
[task-mughal] – №2.1, 2.2:

Which of the following is not a legal identifier?  Select the one correct answer.
a.    a2z
b.    52pickup
c.    _class
d.    ca$h
e.    total#
Which statement is true?  Select the one correct answer.
a.    new and delete are keywords in the Java language.
b.    try, catch, and thrown are keywords in the Java language.
c.    static, unsigned, and long are keywords in the Java language.
d.    exit, class, and while are keywords in the Java language.
e.    return, goto, and default are keywords in the Java language.
f.     for, while, and next are keywords in the Java language.
.