You can have an overflow when doing mathematic opetations between integers and chars, this can affect the expected result since that value is going to be assigned to a char... here you have

invr[len1++] = str[i]-32;

but you should cast to char like:

invr[len1++] = (char)(str[i]-32);
Answer from ΦXocę 웃 Пepeúpa ツ on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › lossy conversion in java
Lossy Conversion in Java | Baeldung
January 8, 2024 - Therefore, the incompatible types in lossy conversion can either have different sizes or types (integers or decimals). In Java, there are many primitive data types available with their corresponding wrapper classes. Next, let’s compile a handy list of all possible lossy conversions in Java: ... Note that though short and char have the same size.
Discussions

For each loop task error: possible lossy conversion from int to char
Avi Tsipshtein is having issues with: Hi All, Link to task: https://teamtreehouse.com/... More on teamtreehouse.com
🌐 teamtreehouse.com
5
October 6, 2016
java - Possible lossy from int to char; Occurs when reading in from a text file - Stack Overflow
Explore Stack Internal ... I am trying to implement the Shunting Algorithm, but after I read in a character, I will need to determine if it is an operand or an operator. The input file has multiple lines of infix expressions that I will convert to postfix expressions and evaluate. So I need to read each character of each line, evaluate, and then proceed to the next line. The error that I get is: "error: incompatible types: possible lossy conversion ... More on stackoverflow.com
🌐 stackoverflow.com
Why am I getting the error "possible lossy conversion from double to int", when there are no integers in my program.
Array indexes are always integers. When you try to use a double as an index it is getting cast to an int. There's no reason for your indexes and array SIZE variable to be anything but int because they can only be ints, there are no fractional array addresses. Change your loop counters and the SIZE variable to ints. The values in your array can still be doubles, just not the indexes. https://codehs.com/sandbox/id/java-main-2hzLlm More on reddit.com
🌐 r/learnprogramming
12
0
August 22, 2023
java - Subtract int from char : possible lossy conversion int to char - Stack Overflow
0 lossy conversion between “S.charAt(index) + '0' “ yields compile-time constant expression error · 0 I don't understand why I get the compiler error "Lossy conversion from int to short" 2 Possible loss of precision; extracting char from string More on stackoverflow.com
🌐 stackoverflow.com
November 10, 2016
🌐
OnlineGDB
question.onlinegdb.com › 4101 › incompatible-types-possible-lossy-conversion-char-java-solve
incompatible types: possible lossy conversion from int to char in java.how to solve it. - OnlineGDB Q&A
July 23, 2019 - Login · OnlineGDB Q&A · Questions · Unanswered · Ask a Question · incompatible types: possible lossy conversion from int to char in java.how to solve it · Please log in or register to answer this question · 2 Answers
🌐
Medium
medium.com › @the_caffeinated_programmer › mastering-java-type-conversion-a-comprehensive-guide-to-widening-narrowing-and-type-casting-bbce5f1ed6ea
Mastering Java Type Conversion: A Comprehensive Guide to Widening, Narrowing, and Type Casting | by Gaurav@The caffeinated programmer | Medium
June 29, 2023 - int i = 65; char c = i; // Narrowing conversion: int to char · Upon compiling the code, you’ll notice a compilation error indicating incompatible types and a possible lossy conversion from int to char.
🌐
Rollbar
rollbar.com › home › how to handle the incompatible types error in java
How to Handle the Incompatible Types Error in Java | Rollbar
September 28, 2022 - Parsing is a more complex process than type casting as it involves analyzing the underlying structure of a given data before converting it into a specific format or type. For instance, programmers often deal with incoming streams of characters, ...
🌐
myCompiler
mycompiler.io › view › 3kz0CjnILNM
Character - Basics (Java) - myCompiler
August 25, 2024 - import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static void convertCase(char[] ar){ int N = ar.length; int x = 'a' - 'A'; for (int i = 0; i < N; i++){ char ele = ar[i]; if (ele >= 'A' && ele <= 'Z'){ ele += 32; } else if (ele >= 'a' && ele <= 'z'){ ele -= 32; } System.out.println(ele); } } public static void main(String[] args) { // char c = '9'; // System.out.println(c); // 9 // int x = 'A'; // System.out.println(x); // 65 // int x = 65; // char ch = (char) x; // System.out.println(ch); // A // char ch = 65; //
Find elsewhere
🌐
Coderanch
coderanch.com › t › 665058 › java › Converting-ASCII-code-characters
Converting ASCII code to characters (Java in General forum at Coderanch)
April 28, 2016 - gives this error on compilation: CodeDisplay.java:6: error: incompatible types: possible lossy conversion from int to char char c = codevalue; ^ 1 error should I be casting the int as a char and if not how do you parse the value in the int named codevalue into a char?
🌐
Medium
medium.com › @aqilzeka99 › java-fundamentals-part-2-primitive-data-types-7dc2acb3d08b
Java Fundamentals: Part 2 (Primitive Data Types) | by Agil | Medium
September 2, 2020 - A short data type is greater than byte in terms of size and less than an integer. Note: short is the most rarely used data types in Java. ... short s = 32767; is valid short s = 32768; is invalid (CE: java: incompatible types: possible lossy conversion from int to short) short s = 10.6; is invalid (CE: java: incompatible types: possible lossy conversion from double to short) short s = false; is invalid (CE: java: incompatible types: boolean cannot be converted to short)
🌐
Errorprone
errorprone.info › bugpattern › NarrowingCompoundAssignment
NarrowingCompoundAssignment
If the type of the expression is ... a byte, char, short, or float), then the compound assignment will perform a narrowing primitive conversion. Attempting to perform the equivalent simple assignment would generate a compilation error. ... byte b = 0; b = b << 1; // ^ // error: incompatible types: possible lossy conversion from int to ...
🌐
Code2care
code2care.org › home › java › [fix] error: incompatible types: possible lossy conversion (java)
[Fix] error: incompatible types: possible lossy conversion (Java) | Code2care
January 26, 2026 - byte b = true; //Compilation Error: incompatible types: boolean cannot be converted to byte short s = false; //Compilation Error: incompatible types: boolean cannot be converted to short char c = true; //Compilation Error: incompatible types: boolean cannot be converted to char int i = false; //Compilation Error: incompatible types: boolean cannot be converted to int long l = true; //Compilation Error: incompatible types: boolean cannot be converted to long float f = false; //Compilation Error: incompatible types: boolean cannot be converted to float double d = true; //Compilation Error: incompatible types: boolean cannot be converted to double Example 2: incompatible types: possible lossy conversion
🌐
Sololearn
sololearn.com › en › Discuss › 2771906 › whats-is-the-error-java
What's is the error (java) | Sololearn: Learn to code for FREE!
Good day guys, please am finding it difficult debugging my java code, am try to reverse a string converted into array(arr) using for loop. How i did it Char result = ' '; For( int i = arr.length; i >=0; i--) { char result =+ arr[i]; } System.out.println(result); Error = possible lossy conversion int to char.
🌐
Geekconvert
geekconvert.com › homepage
Type Conversions · I am just a random guy doing random things
January 4, 2024 - ch + 1 here value will be a integer as 1 is integer and ch is char so result will be of bigger type which is int. So we are trying to store a int in char so it will be error as int being of bigger size can't be implicitly stored in char.
🌐
findnerd
findnerd.com › list › view › Java---How-to-Fix-or-Avoid-Incompatible-Types-Possible-Lossy-Conversion-Error › 31590
Java - How to Fix or Avoid Incompatible Types Possible Lossy Conversion Error?
April 13, 2017 - error: incompatible types: possible lossy conversion from int to short · In the case of type byte: Let say we add two variables of type byte we get the following error.
🌐
myCompiler
mycompiler.io › view › 9ccw9j08fPX
Day 29 String class (Java) - myCompiler
October 15, 2023 - import java.util.*; import java.lang.*; ... but just trying to store variable of type int into variable of type char // Possible data loss (large container to small container) // compiler give error possible lossy conversion // int a = 97; // char ch= a;// Here it is not responsibility ...
Top answer
1 of 2
4

To answer your question, you are basically Down-casting the int data type to short in both the cases which is not acceptable in Java. To describe in detail, below are the detailed description:-

Case 1:-

public class Demo 
{
    public static void main(String[] args) 
    {
       int a=10,b=20;
       short c = (a<b)?a:b;
       System.out.println(c);
    }
}

In this scenario, you are just down-casting data type from int to short which is throwing

error: incompatible types: possible lossy conversion from int to short

Case 2:-

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=10,b=20;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}

In this scenario, since you have declared your variables viz. a & b as final. As a result, both the values becomes CONSTANT for variable c. It means for the variable c, the values of a & b won't get changed & it has been finalized to 20 which is in range of short data type (-32,768 to 32,767). Therefore, you won't get any error unless & until you keep the value within the range.

Real Test case:-

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=32768,b=32788;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}

Compile this class & see the magic! You'll again get the same error.

For your further understanding, please refer here to get a clear picture in general.

Moral of the story:- Just do not downcast your data type!!

2 of 2
2

This is specified in the Java Language Specification §5.2 Assignment Contexts:

Assignment contexts allow the use of one of the following:

...

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

The expression with ternary operator that you have used in the second snippet is a constant expression, because all the variables used in that expression are final. In addition, 10 is representable in short. Therefore, a narrowing primitive conversion from int to short is allowed.

This makes sense, doesn't it? The compiler knows all about the values of the final variables, so this can surely be allowed.

In the first case however, a and b are not constant variables, so (a<b)?a:b is not a constant expression, so an explicit cast is needed:

short c = (short)((a<b)?a:b);
🌐
Coderanch
coderanch.com › t › 738439 › java › Narrowing-casting
Narrowing casting (Beginning Java forum at Coderanch)
Jj Roberts wrote:The switch statement can only take a byte, short, int, char (and their wrapper classes), String or Enum. The Java Tutorial for switch mentions that. Alright, that was my mistake. But still I don't get why this works: what is the rule that allows this automatic narrowing cast from int to short? ... Why would you expect that not to compile? An int can be compared to a short without any conversion, so there is no issue there.