Таблиця символів та ASCII
Why Can We Perform Mathematical Operations on the char Data Type?
The char data type is quite interesting. It stores characters, but you can also perform operations on this data type.
What will be the result of executing this code?
Main.java
1234567891011package com.example; public class Main { public static void main(String[] args) { char firstLetter = 'A'; char secondLetter = '('; char result = (char) (firstLetter + secondLetter); //we use type casting here to convert value from int to char System.out.println(result); } }
Зауважте
Тут ми використовуємо знак "+" для об'єднання значень двох різних змінних. Ми розглянемо основні операції у наступній главі, а поки що вам слід знати, що "+" об'єднує значення двох змінних.
Можна було б припустити, що результат буде "A(", але, як бачимо, результат буде "i". Чому так сталося? char не є чисто символьним типом даних, він приймає символи з ASCII таблиці. Символ "A" відповідає номеру 65 у таблиці, а символ "(" відповідає номеру 40. Як ви вже здогадалися, символ "i" відповідає номеру 105, оскільки 65 + 40 = 105.
Це важливо знати, оскільки це дозволяє нам професійно і гнучко оперувати з типом даних char.
Ось посилання на таблицю: ASCII таблиця.
But why is (char) required? The addition operation returns a result of type int because it sums the numeric codes of the characters. To store this result in a variable of type char, an explicit conversion is necessary. This is exactly what the (char) construct does—it converts the numeric value back into a character.
In our example, the result of the addition is the number 105. The (char) construct converts this number into the character corresponding to code 105 in the ASCII table, which happens to be the character i.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.7
Таблиця символів та ASCII
Свайпніть щоб показати меню
Why Can We Perform Mathematical Operations on the char Data Type?
The char data type is quite interesting. It stores characters, but you can also perform operations on this data type.
What will be the result of executing this code?
Main.java
1234567891011package com.example; public class Main { public static void main(String[] args) { char firstLetter = 'A'; char secondLetter = '('; char result = (char) (firstLetter + secondLetter); //we use type casting here to convert value from int to char System.out.println(result); } }
Зауважте
Тут ми використовуємо знак "+" для об'єднання значень двох різних змінних. Ми розглянемо основні операції у наступній главі, а поки що вам слід знати, що "+" об'єднує значення двох змінних.
Можна було б припустити, що результат буде "A(", але, як бачимо, результат буде "i". Чому так сталося? char не є чисто символьним типом даних, він приймає символи з ASCII таблиці. Символ "A" відповідає номеру 65 у таблиці, а символ "(" відповідає номеру 40. Як ви вже здогадалися, символ "i" відповідає номеру 105, оскільки 65 + 40 = 105.
Це важливо знати, оскільки це дозволяє нам професійно і гнучко оперувати з типом даних char.
Ось посилання на таблицю: ASCII таблиця.
But why is (char) required? The addition operation returns a result of type int because it sums the numeric codes of the characters. To store this result in a variable of type char, an explicit conversion is necessary. This is exactly what the (char) construct does—it converts the numeric value back into a character.
In our example, the result of the addition is the number 105. The (char) construct converts this number into the character corresponding to code 105 in the ASCII table, which happens to be the character i.
Дякуємо за ваш відгук!