Reputation: 63
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.
Upvotes: 6
Views: 142
Reputation: 30330
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.
Upvotes: 1
Reputation: 2049
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
Upvotes: 9
Reputation: 101
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.
Upvotes: 2
Reputation: 120526
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);
Upvotes: 4