+ 4

how %s is working here?

public class GenericMethodTest { // generic method printArray public static < E > void printArray( E[] inputArray ) { // Display array elements for(E element : inputArray) { System.out.printf("%s ", element); } System.out.println(); } public static void main(String args[]) { // Create arrays of Integer, Double and Character Integer[] intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println("Array integerArray contains:"); printArray(intArray); // pass an Integer array System.out.println("\nArray doubleArray contains:"); printArray(doubleArray); // pass a Double array System.out.println("\nArray characterArray contains:"); printArray(charArray); // pass a Character array } } This will produce the following result − Output Array integerArray contains: 1 2 3 4 5 Array doubleArray contains: 1.1 2.2 3.3 4.4 Array characterArray contains: H E L L O

17th May 2017, 5:51 PM
Somnath Ghosh
Somnath Ghosh - avatar
12 Respostas
+ 17
Okay, let's explain this from another point of view: Jvm sees: System.out.printf ("%s", element); Understands it has to print something. Notices the %s formatting option. So gets excited: "Yeah, I will print a string! Where's the string, let me totally printf it!". Looks for the string after the comma, finds element there. Wonders: "This is not a string... oh yeah, I know! I will call toString!" Gets a string returned from toString and prints it.
17th May 2017, 7:16 PM
Tashi N
Tashi N - avatar
+ 15
%s in the printf is the formatting option which formats the value after the comma as a string and then outputs it. See this for further information: http://alvinalexander.com/programming/printf-format-cheat-sheet
17th May 2017, 6:46 PM
Tashi N
Tashi N - avatar
+ 15
@Burey It isn't always. Sometimes it just wants to flip tables. But it can't, so it just throws exceptions ^^
17th May 2017, 8:04 PM
Tashi N
Tashi N - avatar
+ 15
@Burey I think that's what it always wanted to do! Im sure you did it a great favor :D
17th May 2017, 8:24 PM
Tashi N
Tashi N - avatar
+ 13
@Serena Thx. The jvm way ^^
17th May 2017, 7:20 PM
Tashi N
Tashi N - avatar
+ 12
i'll flip it for him/her then (╯°□°)╯︵ ┻━┻ (┛◉Д◉)┛彡┻━┻ (ノ≧∇≦)ノ ミ ┸━┸ (ノಠ益ಠ)ノ彡┻━┻ (╯ರ ~ ರ)╯︵ ┻━┻ (┛ಸ_ಸ)┛彡┻━┻ (ノ´・ω・)ノ ミ ┸━┸ (ノಥ,_」ಥ)ノ彡┻━┻ (┛✧Д✧))┛彡┻━┻ (╯°□°)╯︵ ┻━┻ (┛◉Д◉)┛彡┻━┻ (ノ≧∇≦)ノ ミ ┸━┸ (ノಠ益ಠ)ノ彡┻━┻ (╯ರ ~ ರ)╯︵ ┻━┻ (┛ಸ_ಸ)┛彡┻━┻ (ノ´・ω・)ノ ミ ┸━┸ (ノಥ,_」ಥ)ノ彡┻━┻ (┛✧Д✧))┛彡┻━┻ (╯°□°)╯︵ ┻━┻ (┛◉Д◉)┛彡┻━┻ (ノ≧∇≦)ノ ミ ┸━┸ (ノಠ益ಠ)ノ彡┻━┻ (╯ರ ~ ರ)╯︵ ┻━┻ (┛ಸ_ಸ)┛彡┻━┻ (ノ´・ω・)ノ ミ ┸━┸ (ノಥ,_」ಥ)ノ彡┻━┻ (┛✧Д✧))┛彡┻━┻
17th May 2017, 8:18 PM
Burey
Burey - avatar
+ 12
okies let me just clean my mess then.... (ヘ・_・)ヘ┳━┳
17th May 2017, 8:25 PM
Burey
Burey - avatar
+ 8
never imagined JVM to be such a cheery fella
17th May 2017, 8:03 PM
Burey
Burey - avatar
+ 6
(ノಠ益ಠ)ノ彡┻━┻(•_•) ( •_•)>⌐■-■ (⌐■_■)
17th May 2017, 10:05 PM
Jubbs
Jubbs - avatar
+ 3
i could not understand
17th May 2017, 7:06 PM
Somnath Ghosh
Somnath Ghosh - avatar