package practicejava;
public class Query {
public static void main(String[] args) {
char ch = 66;
System.out.println("character= " + ch);
ch++;
System.out.println("character = " + ch);
}
}
技术上 ch++; 和 ch=ch+1; 是一样的,但是为什么我写 ch=ch+1; 而不是 ch++;?
Technically ch++; and ch=ch+1; are the same but why do I get an error when I write ch=ch+1; instead of ch++;?
你需要提供一个演员表才能做到这一点:
You need to provide a cast in order to do that :
ch = (char) (ch + 1);
这是因为表达式 ch + 1 被提升(upcast)为 int.为了让您将此表达式重新分配给 char,您需要显式 downcast 它.
This is because the expression ch + 1 is is promoted (upcast) to an int. In order for you to reassign this expression to a char you need to explicitly downcast it.
这篇关于为什么我不能写 ch=ch+1;而不是 ch++;尽管它们具有相同的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)