[Java] Incompatible types error occurred when assigning a character string to a char type variable

environment

-Host OS: Windows10 Home ・ Guest OS: WSL2 Ubuntu18.04 LTS ・ VScode ver 1.44.2 · Java openjdk11

error contents


public static int execute(int firstNum, char operator, int secondNum) throws ArithmeticException{
        switch (operator) {
            case "+": //1
                return firstNum + secondNum;
                break;

When I ran the above code, the following error occurred at // 1. ・ Incompatible types ・ Type mismatch: cannot convert from String to char

It seems that "+" is recognized as a String type character string instead of a char type.

Solution

-When handling a character as a char type variable, enclose it in single quotation marks.


public static int execute(int firstNum, char operator, int secondNum) throws ArithmeticException{
        switch (operator) {
            case '+': //Modification place
                return firstNum + secondNum;
                break;

As a result of the above correction, the compile error disappeared.

Consideration etc.

When describing "character" data in the source code, enclose it in quotation marks ('). And when describing "string" data, use double quotes ("). (Kiyotaka Nakayama, Daigo Kunimoto, "Introduction to Java with a Clear Understanding, 2nd Edition" p.50)

So String type character string → Enclose in "" Char type character → Enclose in''

I want to remember.

Reference material

[Introduction to Java 2nd Edition](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82] % 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9% E3% 83 % 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X)

Recommended Posts

[Java] Incompatible types error occurred when assigning a character string to a char type variable
[Java] How to convert a character string from String type to byte type
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
[Java] How to use substring to cut out a character string
A validation error occurred when saving to the intermediate table.
I tried to convert a string to a LocalDate type in Java
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
[Java] How to use substring to cut out a part of a character string
[Java] Divide a character string by a specified character
[Java] Convert Object type null to String type
Notation to put a variable in a string
[Java] Comparison of String type character strings
[Introduction to Java] Variable declarations and types
Type conversion from java BigDecimal type to String type
[Java] How to convert one element of a String type array to an Int type
[Java] Difference between equals and == in a character string that is a reference type
[Android] How to convert a character string to resourceId
Code to escape a JSON string in Java
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Java] char type can be cast to int type
java: How to write a generic type list [Note]
What to do when a null byte error occurs
When trying to insert a specific gem (levenshtein) with Docker, an error occurred without ruby-dev