0

This post may be considered to be inappropriate for stackoverflow, although I'm not trying to insult the Java language or anything like that. I enjoy coding in Java, but there is something regarding System.out.println that I have been wondering for quite a while.

Why is it that we are always forced to type System.out.println() or System.out.print() every time we want to print? Sure, we could make a void function to save time and energy in programs that we will have several print statements (which I sometimes do) like so:

public static void print(String output) {
    System.out.print(output);
}

and then just call print (and if you want to really be thorough you can overload the function with arguments involving ints, doubles, chars, etc). But why is it that the Java language itself doesn't already allow us to print to the console by just writing print? Some languages (such as Python) make printing to console that neat and simple - so why doesn't Java?

Again - I'm not saying that the Java language is poorly designed, or trying to start a thread that is intended to bash Java. I'm sure the language designers had their reasons for designing it the way they did, and it would help me to understand why it is so. It would be much easier to just need to type print instead of System.out.print, so there must be reasons for why we must type System.out.print - I just can't figure out those reasons. I've tried googling for information on this issue and can't find anything relevant to the issue.

Please refrain from opinionated responses about the Java language - I want actual facts that explain this phenomenon.

b4hand
  • 9,550
  • 4
  • 44
  • 49
Chris C
  • 73
  • 1
  • 5
  • I doubt whether the actual language designers will see your question, so you're unlikely to get the answer that you're looking for. – Dawood ibn Kareem Mar 30 '15 at 02:16
  • The question cannot be answered definitively so it is not appropriate for this forum. However, you make an number of assumptions which are not correct. There is any number of ways to avoid so much typing if you don't want to. Your IDE will support the `sout` template and you can use `out.println()` instead with a static import, or just `println()` by adding a method. In fact you can use just `p()` if you wish. – Peter Lawrey Mar 30 '15 at 02:17
  • 1
    You can see: http://stackoverflow.com/questions/3406703/whats-the-meaning-of-system-out-println-in-java – chengpohi Mar 30 '15 at 02:17
  • 4
    Because Java is a OO language (or at least when it was first designed), it requires an Object to provide functionality. `System` is an `Object`, which provides a `static` field `out` which is an instance of `PrintStream`, another Object. This also centralises the stdout/err into a single `Object`, which greatly decreases complexity and increases customisation, rather then having it hanging of `Object` for example – MadProgrammer Mar 30 '15 at 02:17

1 Answers1

3

Simply, Java doesn't have global functions.

Also, according to The Java Language Environment (a '90s book co-authored by James Gosling):

Java has no functions. Object-oriented programming supersedes functional and procedural styles. Mixing the two styles just leads to confusion and dilutes the purity of an object-oriented language. Anything you can do with a function you can do just as well by defining a class and creating methods for that class.

It's not to say that functions and procedures are inherently wrong. But given classes and methods, we're now down to only one way to express a given task. By eliminating functions, your job as a programmer is immensely simplified: you work only with classes and their methods.

So there is at least one language designer's reasoning.


You may shorten the call by statically importing System.out:

import static System.out;

class Example {
    public static void main(String[] args) {
        out.println("hello world!");
    }
}

Since System.out is an object, its instance methods cannot be statically imported.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125