是否在printf运算符重载中使用%?如果没有,那是什么?

时间:2017-08-06 11:28:45

标签: java operator-overloading

请解释这里发生的事情:

class Test{
public static void main(String[] args){
    int a = 43%2;
    System.out.printf("The test remainder is %d %s",a,"hello");
 }
}

在上面的代码中,我想知道%运算符是operator overloading吗?

2 个答案:

答案 0 :(得分:1)

您似乎对%String的使用感到困惑。

Java中""之间的任何内容都不是运算符。所以当你的代码像是  43 % 2 % String是运营商,但当"asdf%asdf%++*adsf" % + * printf%不是运营商时。 Java也没有运算符重载。

; ; A simple boot sector program that demonstrates addressing. ; mov ah, 0x0e ; int 10/ ah = 0eh -> scrolling teletype BIOS routine ; First attempt mov al, the_secret int 0x10 ; Does this print an X? ; Second attempt mov al, [the_secret] int 0x10 ; Does this print an X? ; Third attempt mov bx, the_secret add bx, 0x7c00 mov al, [bx] int 0x10 ; Does this print an X? ; Fourth attempt mov al, [0x7c1e ] int 0x10 ; Does this print an X? jmp $ ; Jump forever. the_secret : db "X" ; Padding and magic BIOS number. times 510 -( $ - $$ ) db 0 dw 0xaa55 函数使用$ nasm BOOK_EXAMPLE.asm -f bin -o BOOK_EXAMPLE.bin $ qemu-system-i386 BOOK_EXAMPLE.bin 来标记稍后将传递给它的变量的位置,它可能是任何其他符号,与运算符重载无关。 / p>

答案 1 :(得分:1)

不。 Java目前对运算符重载的支持非常有限,而这不是其中之一。

string literal中的%java.util.Formatter的实施处理。 String#formatPrintWriter#printf将格式化工作委托给Formatter,后者会手动解析字符串。

%有价值的唯一原因是Formatter处理字符串的原因。

如果你查看代码,你会发现:

if (conversion == '%' || conversion == 'n')

后跟一个处理不同类型的switch语句。