• Post Reply Bookmark Topic Watch Topic
  • New Topic

system out print fun  RSS feed

 
Divyanand Kutagulla
Greenhorn
Posts: 18
C++ Java Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this post might seem too rudimentary - I discovered this issue in my code this morning and thought of sharing this here
Given


Assuming pattern is a string set to "ab" , the output is:


looks like to concatenate a space to an integer value via system.out.print we need use " " and NOT ' ' (using the latter causes its ascii value to be used)



 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you mean fun as in enjoyment not as in the keyword in some languages meaning function. System.out.println is not actually a function.

What you have is the behaviour of the + operator. It has two distinct behaviours, and you can read about them in the Java Language Specification (=JLS).
Applied to numbers it does addition. As in 1 + 2 = 3. Remember that if you write ' ' you are not writing the space character you are writing a number (I think 0x20 = 32 decimal) equal to its ASCII or Unicode value. Look in the JLS where it tells you a char is an (unsigned) integer. If you apply the number 2 and the ' ' char you are adding (arithmetic).

If ether of the operands is a String, however, + moves out of the realms of arithmetic into String catenation. Both its operands are Strings. So if you have 34 + "text", the 43 is converted to the String "34" and then catenated to the other String making "34text".

Pattern.length() is a number, so when added to the space (32) you get 34. Then the 34 is converted to "34" and then the two Strings are joined together. You get similar effects with this sort of code:-

Don't copy‑and‑paste this post because there are several Unicode characters which may give incorrect results.

 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are seeing the difference of adding an integer with a space character (' ') and concatenating with a single space string (" ")
 
Divyanand Kutagulla
Greenhorn
Posts: 18
C++ Java Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume you mean fun as in enjoyment not as in the keyword in some languages meaning function. System.out.println is not actually a function.

What you have is the behaviour of the + operator. It has two distinct behaviours, and you can read about them ...


@Campbell Ritchie

(I shortened your reply in quote per your instructions to avoid any unicode related issues)

Thank you for your reply.

Yes, I did mean fun as in enjoyment.

I understand that operator + is overloaded: Here if lhs of the + is a string then rhs is converted to string representation and concatenated
(I was just sharing this observation on this forum so that it might help someone new to the language and programming to see how conversions are happening.
Sorry if my post gave impression that I having trouble with my code)

BTW here is the answer to your question:

prints
Enter the 6th value:

 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of work when just the character to the left of i and to the right of 1 would do.

If I do need an Integer, I prefer valueOf over new Integer. Plus it works out toString by itself in this context (JLS 5.18.1 & 5.1.11).
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyanand Kutagulla wrote: . . .
I understand that operator + is overloaded: Here if lhs of the + is a string then rhs is converted to string representation and concatenated . . .
That applies if the String is on the left or on the right. If the right operand is a String the left operand is converted to a String, too.

And as JA suggests, there is a much simpler way to get 6th printed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!