Reputation: 316
Please forgive me if my question seems pretty noobish. This is my first question over here.
I am having a simple query in my mind. Java language has been implemented using C and C++.
My question is
In C language,the formatting behaviour of C is undefined for using different modifiers than the given datatype.
int c=10;
printf("%f",c); // unspecified behaviour
Whereas in Java, if we use the similar concept to print the number using different format specifier, we get an IllegalFormatConversionExceptoion
.
Exception Detail :-
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
My question is :-
Please clear my doubt. I'll be thankful to you geniuses...
Upvotes: 1
Views: 88
Reputation: 533870
The printf
in Java is inspired by the same method in C but works completely differently. If you give it a BigDecimal
for example it will print it even though C doesn't have a BigDecimal.
In short, just because Java is similar to C, doesn't mean it use the same underlying functions. In most cases, Java has it's own implementations to ensure;
Why so difference between Java and C though Java is derived from C
Why not? Why would this make any difference at all?
Let me give you a different example. The verb to "manufacture" literally meant man made, and yet today it almost means the opposite. Just because one language is derived from another doesn't mean they have to be the same.
Upvotes: 5
Reputation: 34839
Because historically, the printf
function caused all sorts of headaches when the format specifiers were wrong, or arguments were missing. So languages derived from C, specifically C++ and Java, went to extraordinary lengths to avoid the printf
problem.
At the same time, C compilers were improved to understand how printf
works, and generate warnings when it's used incorrectly. Hence, the printf
problem no longer exists for the most part (unless you ignore/disable the warnings).
Upvotes: 0