1

I've been looking into JDK9 and i noticed that in jshell you don't have to prefix printf() with System.out.printf() i.e you can write printf("test") but you can't write println("test")

enter image description here

Is this a feature of jshell only ? How you can achieve the same behaviour in an application ?

The following is the closest :

import static java.lang.System.out ;
out.println("test");
out.printf("test");
firephil
  • 830
  • 10
  • 18
  • For the record, `printf` was [removed](https://stackoverflow.com/a/43864201/6730571) – Hugues M. Jun 06 '17 at 22:25
  • Dissapointed with the pace of innovation in java and the decision making in alot of areas of the language anyway printing is a basic and important functionality i dont understand why they removed it from the default import of the shell – firephil Jun 07 '17 at 03:06

2 Answers2

2

In earlier versions of early access there was a predefined printf method that redirected to System.out.printf. There were two problems with this: it isn't standard Java and, as pointed out in other answers, it was inconsistent in that print and println were not defined.

In the current early access and in what will ship, there are no print methods automatically defined. If you want this, start jshell with:

jshell --start DEFAULT --start PRINTING

Which will give you the complete set of print methods.

To answer the second part of the question, you can see the definitions of the redirection methods by using the /method command after starting as above. Adding these to a class would give you access -- but that it pretty ugly. Alternatively, you could make them public static methods in a class, and import them.

Robert Field
  • 479
  • 4
  • 5
  • At least as of Java 18.0.1.1, you can drop `--start DEFAULT --start PRINTING`, since invoking `jshell` without those flags allows `System.out.println("hi")` and `System.out.printf("hi %s\n", "there")` to work as expected. – mellow-yellow Jul 02 '22 at 22:39
1

Iit looks like printf is a command in JShell but println is not (just like in bash shell). The printf() method used by System.out is not the same as the one jshell uses and therefore can't be statically imported in a java application.

Sources:

http://jakubdziworski.github.io/java/2016/07/31/jshell-getting-started-examples.html https://jshelltutorial.com/jshells-printf-method/

Eddie Curtis
  • 1,207
  • 8
  • 20
  • Thanks for updating to show your usage. I have edited my answer, if it still doesn't make sense let me know and I will explain further. – Eddie Curtis Nov 24 '16 at 15:57
  • I was also looking for some insights, on how it is achieved... Or it is just a feature of jshell – firephil Nov 24 '16 at 16:05
  • 1
    It is just a feature of jshell. Think of it as a function that is only available by using jshell, it just happens to have the same name as a Java method. – Eddie Curtis Nov 24 '16 at 16:32
  • Found it https://youtu.be/jziVaht480w?t=18m29s From the creator of jshell, its a method that wraps the System.out.printf() to a local prinf() method which is imported implicitly when jshell starts... – firephil Nov 24 '16 at 16:33
  • Nice find! If my answer helped you please mark it as the accepted answer. If not then please post your own answer, or update the question with anything else you need to know. – Eddie Curtis Nov 24 '16 at 16:38