1
class A {
    public static void main(String args[]) {
        String.format("%c",2);
    }
}

Above program is compiling fine but how it can assign the integer to char. Shouldn't it give lossy conversion compile error?

class A {
    public static void main(String args[]) {
        String.format("%d",45.67);
    }
}

This is not causing the compile error, but it should cause the compile error because how we can assign a double value to the int. Rather its giving runtime error?

Please help me in resolving this

Ele
  • 33,468
  • 7
  • 37
  • 75
Rishab Shinghal
  • 591
  • 3
  • 16

3 Answers3

4

String.format is just a standard method that consumes String instance as a first argument.

Java doesn't compile content of string literals. It is developers responsibility to provide correct pattern.

The String.format documentation says:

Throws: java.util.IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.

Update #1

Note: Java uses integers for char representations. So the first example won't fail. The following code is valid in Java:

char c = 1;
int i = 'o';

Update #2

According to the documentation char has a minimum value of 0 and a maximum value of 65535 inclusive. That is why you can't assign 65537 to a char. On the other hand 65537 is valid argument for c conversion in String.format.

See the documentation:

Character - may be applied to basic types which represent Unicode characters: char, Character, byte, Byte, short, and Short. This conversion may also be applied to the types int and Integer when Character.isValidCodePoint(int) returns true

Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
1

Answer from: Can the char type be categorized as an integer?

Yes, a char is an integral type in all the popular languages in which it appears. "Integral" means that its spectrum is discrete and the smallest difference between any two distinct values is 1. The required range of supported values is usually quite small compared to that of other integral types. Computer hardware traditionally treats integers as the fundamental data type; by contrast, arithmetic floating-point types are a more recent and more complicated addition.

Hope this explanation helps you to understand a little more about primitive types.

Ele
  • 33,468
  • 7
  • 37
  • 75
  • You are right on your part, but if we do the assignment, char c=1000, it gives a compile error. How do we justify the above logic then? – Rishab Shinghal Dec 02 '17 at 22:35
  • Sorry I meant to say runtime error as the second snippet got runtime error as we were trying to assign incompatible types. – Rishab Shinghal Dec 02 '17 at 22:39
  • You're not going to face a runtime error because for Java a char has a representation as int and vice-versa, for example, the Char 'a' is the Integer 97. – Ele Dec 02 '17 at 22:49
0

You may want to have a look at the sources, specifically:

java.util.Formatter.format(Locale l, String format, Object ... args)

You can think of String.format (which calls Formatter.format) as a small framework for formatting Objectss according to instructions encoded in the format string

David Soroko
  • 8,521
  • 2
  • 39
  • 51