6

So I don't know much about java but I noticed this worked then according to my class notes I should be doing it a different way

Here's what my notes have

System.out.print("hello");
System.out.print(name);
System.out.print("\n");

However I tried this and it also does the same thing. It's shorter so is this an acceptable way to do it or will it break down the road?

System.out.print("hello"+name+"\n);

Also as long as the code runs right my teacher shouldn't care right? It's my first class and I'm not a computer science major.

4
  • 1
    They are effectively the same. You may get some answers about subtle ways in which they are different, but if this is your first class I think it's best to understand that they are logically the same and move on. (I would probably have done: System.out.println("hello"+name);)
    – WW.
    Mar 12, 2014 at 23:30
  • 1
    as long as the code runs right my teacher shouldn't care right? - this is rarely the case. In this situation, what you have is fine; however, there are many cases where how you code something is equally important as how well it runs.
    – Sam Dufel
    Mar 12, 2014 at 23:33
  • 5
    Basically, in the first attempt you are printing three Strings while on the second attempt, you are putting together 3 Strings into one and printing it once - same result. Less code to write, and read/maintain ;)
    – blurfus
    Mar 12, 2014 at 23:34
  • Strings are immutable so any concatenation operation (e.g. str+" words") will have to create another object. This is a non-issue until you are working with concatenation in loops. This question gives more detail on my comments: stackoverflow.com/questions/1532461/…
    – cory.todd
    Mar 12, 2014 at 23:40

4 Answers 4

9

It will work and I'd argue that it's in fact a better way to do it.

Go for it, that's the hacker spirit!

If you want something even shorter and more descriptive, try

System.out.println("hello " + name);

The println will automatically print a line end ('\n') at the end of what you print.


Just to make this complete, let's assume name = "James Gosling";.

In the code written in your notes, you first print:

hello

Then, you print name, which leads to:

helloJames Gosling

It's printed like that because we're actually missing a space after "hello". To print it with a space, use "hello ". Finally, we print a newline.

In your (arguably better) piece of code, you print only once, but when you use the expression "hello"+name+"\n", you are creating a new character string which ends up being the same. This is because the + operator concatenates (that is, chains) two strings and creates a new string with the result.

So, when you print the resulting string, you get (plus the newline):

helloJames Gosling

0
4

Others have weighed in on the specific example, but there are problems with trying to generalize this.

+ does addition when applied to primitive numeric values instead of doing concatenation when applied to a string, so

int x = 4;
int y = 2;
System.out.print(x);
System.out.print(y);
System.out.print("\n");

prints 42 while

int x = 4;
int y = 2;
System.out.println(x + y);

prints 6 followed by a line-break.

Since + associates left, you can use "" + ... to force + to mean string concatenation instead of addition

int x = 4;
int y = 2;
System.out.println("" + x + y);
2

In java the plus(+) operator is an overloaded one. This means that the + sign means different things when applied to different types of data. If you work with String objects(which is the case) it means String concatenation.

So this line of code System.out.print("hello"+name+"\n); concatenates the String object "hello" with the String object name and then with the String object "\n". Finally it prints the result to the standard output.

There are other methods for printing too.

System.out.println() prints the argument and then insert a new line

So to have the same result you would write

System.out.println("hello" + name);

Java has also the methods System.out.printf() and System.out.format() that support C-like printing.

0
1

The first writes our three String objects seperately. The second combines (concatenates) three Strings into a single String, then writes that out.

The two approaches are equally valid in terms of syntax (they are valid Java code) and semantics (they do the right thing), They both produce the expected output and perform just as well as each other.

I think your way is more readable because it has less repetitive boilerplate code so I would say it is better.

2
  • 2
    If you run javap -c to dissassemble the code you'll see that a + b + c is translated into a single expression that allocates a java.lang.StringBuilder, calls append several times, and then does toString of the result, so the number of extra allocations is independent of the number of +'s. This is covered in Ch 15 of the JLS Mar 12, 2014 at 23:47
  • My bad, I didn't know about that optimisation. I have removed that part of the answer.
    – joews
    Mar 12, 2014 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.