0

I visited the question on this question: Android SAX parser not getting full text from between tags because I had a similar problem. I solved my problem with one of the statements on this side but I do not understand the bold clamps.

String chars = -->( new String(ch).substring(start, start + length) )<--;

Can anyone help me? Because the statement can also be used without them.

Community
  • 1
  • 1
  • 3
    The "bold clamps" you are speaking are called parenthesis and they are a delimiter in the java language. You might do better to start with a basic java book and work up from there. – SRM Jun 01 '11 at 16:19
  • This is a little cleaner: String chars = ch.subString(start, start + length); – jabbie Jun 01 '11 at 16:39

2 Answers2

2

As SRM mentioned they are parenthesis. They add no value in this specific case other than possible improvements to code readability. There are case where parenthesis do change the meaning. Relate it to the order of operations in math.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • I have been using Java quite a bit, but I was suprised about to use the clamps for the whole expression. Thx for the answer ;) – Christian Jun 01 '11 at 16:30
  • If I leave the parenthesis I got different results. I forgot to say. This is very strange for me. Normally, I know how to handle with them ;) – Christian Jun 01 '11 at 16:35
  • You should not have gotten different results. Paste both versions you tested. – jzd Jun 01 '11 at 16:40
  • String chars = (new String(ch).substring(start, start + length)); String chars = new String(ch).substring(start, start + length); The both versions... – Christian Jun 01 '11 at 16:44
  • That could should produce the same results. – jzd Jun 01 '11 at 16:50
  • Yes, I thought in the same direction therefore I posted this line. But without the parenthesis the sax parser cuts sometimes my string. Whatever ;) – Christian Jun 01 '11 at 16:56
  • Most probably, the real reason lies somewhere else. – Rekin Jun 01 '11 at 17:02
1

Parenthesis can be used even there is no need to. Sometimes it can make things clearer, sometimes not.

For example 1 + 2 * 3 is the same as 1 + (2 * 3). Parenthesis, if put correctly, can be used without changing the meaning of an expression to enable someone reading the code to understand an expression, especially when the expression is much more complicated. Note that if you put the parenthesis in another way like (1 + 2) * 3, it means something different from the previous two expressions.

In your code example, that parenthesis is not necessary.

I personally wouldn't put it because it requires extra keystrokes and it can cause a human code reader to scan through that line again wondering if it's more complicated than it looks, only to find out it's not, and wasting many small packets of time. If one comes across too many such unnecessary parenthesis, one may have a tendency to gloss over significant parenthesis and misunderstand an expression later. Just my personal, subjective opinion.

String chars = (new String(ch).substring(start, start + length));

and

String chars = new String(ch).substring(start, start + length);

both mean the same thing.

so do these

String chars = ((new String(ch).substring(start, start + length)));
String chars = (((new String(ch).substring(start, start + length))));
String chars = (new String((ch)).substring(start, start + length));
String chars = (new String(ch).substring((start), start + length));

Try to see the pattern. IMO it's not a good thing to teach one to learn programming syntax intuitively via "pattern matching" though, so you should look at some java books like what SRM suggested.

Can you do this?

String (chars) = new String(ch).substring(start, start + length);

You'll probably say "no", which is correct.

But why? If you can't answer why, then it'll be good for you to find out.

(I'm not making fun of you, I just felt like typing a lot.)

blizpasta
  • 2,624
  • 3
  • 25
  • 31
  • I know everything about parenthesis (also in connection with math ;) ) but I was wondering about the strange behaviour of my parser. But maybe other people can use this post. thx – Christian Jun 01 '11 at 17:06
  • I should have asked the question into another direction...xD – Christian Jun 01 '11 at 17:08