• Post Reply Bookmark Topic Watch Topic
  • New Topic

String  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str1="";
String str2="";
String str3="";


str1="b"+3+9;

str2='b'+'3'+'9';

str3="b"+"3"+"9";


what is the difference between the three?
They all give same result when printed and why ide doesn't show error or any warning when Lhs and rhs are different data types as it shows when we assign a floating value to int or a no to char data type?
 
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because when you use '+' with string then it will add operands as string. So in for all of 3 cases b, 3 and 9 will be added as string irrespective of their type.
hence you get same output.
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I do this

String Str4="";

Str4=b+3+9;
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is a error.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why is it an error?
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I am asking
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the error message tell you?
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"b can't be resolved" I know b can be variable or constant. I am not trying to ask lame question. But is that the reason?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you define b?
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant b can be a variable or constant that's why error message is "b can't be resolved". I was just trying to figure out why b is not concatenated like 3and 9. Maybe because 3 and 9 can't be identifiers ?
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, come on. The two are not alike at all. In your first examples you used "b" with quotes around it, which is a String literal. But in this other example you've used b with no quotes around it, which is a variable which presumably you haven't declared.
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay ,thanks.I got it. I didn't put my question in right way.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One note, you didn't ask for that but I feel it is related.

"1" + 2 + 3 evaluates to 123
but
1 + 2 + "3" evaluates to 33

It is because an expression is evaluated from left to right.

In the first one "1" + 2 is eveluated first. One of the operands is a String so the other one is converted to also be a String. This results in "1" + "2" which is evaluated to "12".
The same happens to the third operand. "12" + 3"12" + "3""123".

In the second example 1 + 2 is evaluated first. Both of the operands are integers, so the resulting operation is an addition. 1 + 2 evaluates to 3.
Next we have 3 + "3""3" + "3""33".

The conclusion is you need to be careful when concatenating Strings and numbers together.
 
Sheriff
Posts: 4935
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zohr Ridhan,

I don't see this was mentioned, but:

String str2="";
str2='b'+'3'+'9';

It is illegal, because of type mismatch. "something" (double quotes, with single or multiple characters in between) denotes the string (object), and 'a' (single quotes, with single character allowed in between) denotes char data type (primitive)
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right!That's my bad . Thanks for reminding me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!