- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Autoboxing in Java (1.)5 would fix that, I think :D
Admin
Yup, and when another coder comes to maintain your code and has no idea what those fucking magic numbers mean he's fucked to the core.
Oh, the fun
No, your have the ".equals" method because
Admin
And as we do in .Net ...
Admin
No, and there is no reason to, autoboxing will box a primitive type in it's object for argument passing, it won't even try for methods accessing.
Autoboxing in action (same compiler):
Admin
lol, if only everyone was as passionate, perhaps we wouldnt need this site.....
(*slips into quiet contemplation*)
Admin
I do need to agree:
C:\test.java:5: int cannot be dereferenced
System.out.print(Integer.parseInt("3").toString());
^
1 error
Which leads me to believe that todays post is entirely finctional, that is to say that the following makes no sense: 'John was maintaining a client management system written a few years before his time ..' as the code could never have compiled in the first place.
Admin
While I cannot confirm this particular example, I'm know programmers who have comparable habits. For example, they say "global variables are bad" and the build incredible complex constructs to imitate the semantics of global variables (but of course it's not better at all, since it is the semantic that makes global variables bad)
And the things people come up with to avoid using a goto.
Admin
Ahhh finally Microsoft developers can say something back to the Java people...
They had keep it in for sooo long, but now they don't have to take it anymore! they're fed up with being toyed around and their names smuthered!
Mwohahahaaaahhaaaaarrrgh
[:P]
Admin
You failed for not reading the code
Original code never tries to send the "toString" message to the result of
Integer.parseInt
2nd version in case my l33t syntax highlighting has been borked up by the forum
The result of
Integer.parseInt
is the argument of theelementAt
method, and thetoString
message is in fact sent to the output ofresult.getResults().elementAt()
which is an arbitrary object and therefore does feature atoString
method.Admin
Better look again... the post is not parseInt("3").toString(), but result.getResults().elementAt(parseInt("3")).toString() - mind the parentheses
Admin
I'm not even a MS dev ;)
Yay, I don't even need the forum, managed to bork my post by myself.
Was supposed to be:
You failed for not reading the code
Original code never tries to send the "toString" message to the result of
Integer.parseInt
2nd version in case my l33t syntax highlighting has been borked up by the forum
The result of
Integer.parseInt
is the argument of theelementAt
method, and thetoString
message is in fact sent to the output ofresult.getResults().elementAt()
which is an arbitrary object and therefore does feature atoString
method.Admin
Yes, it is.
That part of the JLS defines String literals. It has no bearing on this discussion at all.
No. The constant pool is created at class load time for Strings that consist of compile-time constants. These are defined in JLS (3rd edition) 15.28. Note that that list does NOT include method or constructor calls, but of it did, the constant folding would occur at compile time, not at class load time. So the "3" that occurs on both sides of the == will certainly be two references to the same object, which is created at class loading. The constructor and method call is executed at class loading if and only if it is part of a static initializer. Not if it is part of a constructor / instance initializer or method body. But what this discussion was about (as I understood it) was whether the return value of the toString() call would be the same object or a different one. It is not; try it out. The only way for it to be the same object would be if the toString() method used intern() on its result. This is, I have to admit, not explicitly forbidden by the JLS or the API spec, but I cannot imagine that any sane implementation does it, since it would lose a lot of performance for little or no gain.
No, though it is embarassing that I did not notice that calling parseInteger() returns a primitive, which does not have a toString() method. Let's assume for further discussion that we are talking about
Integer.valueOf("3").toString()
instead.
That is about the possibility of constant pool strings getting garbage collected and, again, has no bearing on this discussion.
If you are really implementing the spec, I suggest you read it more carefully.
Admin
Nope. Quite the opposite, in fact. One of the great things about Java is that its specification is very complete (no undefined behaviour all over the place as in C/C++, google for "nasal demons") AND quite readable (compared to other language specs).
Admin
No, this proves nothing whatsoever except that you personall dislike Java. Operator overloading is useless syntactic sugar that mainly serves to obfuscate code. And the difference between object value and identity is an important concept that a language hides to the programmer's detriment.
Admin
Try writing an OS in Java. hmmm...
So, the STL is relies extremely heavy on an syntatic sugar. hmmm....
Well yes, maybe Alternatively, you could be full of self-oppinionated shit
Admin
It is one thing to assert that a certain person merely hates the language, it is quite another to defend the lack of some feature merely because you have to in the face of rants against your pet language.
Operator overloading does NOT obfuscate code and is NOT useless syntactic sugar. Your statements are just as brazen and rash as the person you are quoting.
So get a life. Grow up and acknowledge the good in other languages like C# because it does not and will not devalue the worth and value of Java nor your job.
Admin
The issue here is that rougly 95% of the time you'll want to test an object value, and Java gives you and object identity because they have "removed the pointers from the language, but in fact not and they'll come around to fsck your ass".
I'm sorry mate, but having to use a specific method to test if two string have the same value is not only not quite sane sane, but more or less specific to Java, I don't think that I've ever seen any other language with a standard, native string type (this means that C and assemblies are excluded, as they do NOT have native string types) behave that way.
There is no programmer detriment as long as the language behaves logically and follows the damn Principle Of Least Surprise that the Java devs seem to have forgotten quite a while ago, especially when you can still get (and test) object identity in the few cases you need to, once again with something a bit logical if you could.
Admin
Well Java has not been created to write OSes... And there actually is an OS-like thing in Java surprisingly called JavaOS
Admin
Oh, and BTW the fact that the language has an awful lot of specifications everywhere does not mean that it's not fucked up. There is no relation whatsoever between them.
Admin
Your first sentence is correct, but the second is not. Undefined behaviour is the source of a lot of fuckups, so I'd certainly say that a language that has lots of undefined behaviour is in itself fucked up.
Admin
And what does that have to do with anything?
It would work just fine (if not better) and be just as useful if it used methods instead of all overloaded operators.
Admin
Nobody remotely competent ever claimed that Java had no pointers - one of the most common errors you see isn't called NullPointerException for nothing. What it does not have is pointer arithmetic - and good riddance.
Actually I'd say that Java does exactly that (behave logically and follow the principle of least surprise) in exactly this point. == compares identity for all objects and Strings are objects. And you won't be surprised by an innocent-looking overator resulting in a potentially very costly value comparison.
Admin
Yes, it is. Let's look at it in detail:
1. it's syntatctic sugar - simple fact.
2. it is useless - Not 100% true, but 95%. The only advantage it offers is terser code, but this is actually a grave DISadvantage (see 3.) unless the operator's meaning is ovbious and/or well-known to everyone who reads the code. This is the case only for mathematics, and only for the small subset of mathematics where ASCII symbols are used. There's a reason why nearly everyone arguing the usefulness of operator overloading will use the same example (complex numbers).
3. it obfuscates code - operators are less descriptive than method names in nearly all cases, and implicity operator precedence is a minefield. Yes, it allows you to cram more mode on one line. No, that's not a good thing.
Admin
Brazzy is right on the money here. The == operator always compares the equality of the primitive values on its left and right. Those primitive values may be traditional primitive data types such as int and char, or they may be Object References, something akin to pointers. Why should anyone expect anything different? The people that don't understand this probably never grasped that C and C like languages only support pass by value, and never do pass by reference.
The fact that other languages allow a programmer to redefine an equality operator between two pointers is a definite wtf.
Admin
Other languages usually don't let you use pointers, you only get references (which are definitely not pointers), and in dynamic languages a reference is meaningless and the object is the value (pointed to by the reference)
Oh, and most high level languages do not give you access to primitive types, ever.
Admin
But is your 010 a big endian or a little endian?
Admin
Righto. One of the greatest problems of the Java language (and I am using it for several years now) is the fact that it distinguishes between primitive types (such as 'int') and and their object counterparts (i.e. 'Integer'). The latter are only needed to wrap primitive types to be able to store them in Vectors and Hashmaps and the like. The former are needed to do calculations on them. Thus requiring constant type casting back and forth. The new autoboxing feature actually aggravates the situation, for now what's happening behind the scenes becomes more opaque.
The primitive types lift the need for objects to know about operators. But that 'advantage' comes in at a very high cost, boy! I would much rather have Integer objects know how to calculate, instead of all the hassle with casting back and forth between objects and primitive types.
Admin
I love java for many reasons but the .equals operator is a major pain in the @$$ because it yaks on nulls. The == operator in C# behaves correctly for nulls. I don't mind writing good old:
public static boolean isEqual(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
But what I do mind is going through gobs of other people's code and making it call this method which should be part of the basic syntax of the java language.
Admin
Hmmm... the SQL programmer in me wants to rewrite this as:
public static boolean isEqual(Object o1, Object o2) {
return o1 == null ? false : o1.equals(o2);
}
But the C programmer in me wants to rewrite this as:
public static boolean isEqual(Object o1, Object o2) {
return o1 == o2 ? true : o1.equals(o2);
}
Admin
The C programmer in me is an idiot.
public static boolean isEqual(Object o1, Object o2) {
if (o1 == o2) { return true; } // null == null is true, dammit
if (o1 == null) { return false; } // null == anything-else is false; null != ""
return o1.equals(o2);
}
Admin
I was referring to the statement: "Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:"
This is indeed untrue. No "object is created" in one and not the other [refer to J2SE API Specification java.lang.Integer.parseInt(java.lang.String)] unless of course, you're referring to object created at class load time, which is created in both (whichever way you look at it, the statement is false).
The remained of your ranting appears to be a paraphrase of the spec., which is close enough to correct to bother about.
For those interested, here is some fun related to constants:
http://jqa.tmorris.net/GetQAndA.action?qids=13
Admin
This is true to some extent. I'll bet that "you're" implementation indeed returns false for Integer.parseInt("3").toString() == "3".
As you noted, this behaviour is not guaranteed by the specification.
However, to suggest that there is a performance penalty in doing so is blatantly false.
In fact, that is exactly what the "autoboxing within the range of a byte" hack does, only in a different context.
Specifically, keys a value (the primitive) to an object (the wrapping type) with O(1) lookup - an attempt to prevent redundant objects - simply, all Integer instances with the same underlying int representation need not be replicated (this is ultimately a result of the constructor semantics of the language guaranteeing a new instance - an implicit requirement defect).
Related fun: http://jqa.tmorris.net/GetQAndA.action?qids=7
Admin
1. The more experienced developers who wrote the C# engine which I doubt you could do did not think so. You are just regurgitating sweeping statements with no established references.
2. Again a rigmarole of sweeping and unsupported statements. Any application with insufficient comments or documentation is going to be a nightmare to be maintained. That more than anything else makes for obfuscation in any application. NOT operator overloading. Your argument is considerably a very specious one.
3. Self describing methods (with descriptive names) is not half as useful as proper application of patterns and juducious commenting and documentation. The argument that operator overload obfuscates code is just as valid as any argument that posits that any code can and will introduce bugs, so no code is better.
In short, there is nothing that you are asserting about operator overloading that is correct or relevant.
Admin
Why can't people make arguments without insulting the intelligence of those they are arguing with? There is no need to say something like, "which I doubt you could do" - is there? As soon as I saw that nonsense, I stopped reading your post, firm in my decision that you were wrong in everything that you were saying. I appreciate that you may have a need to insult others on the Internet but try to temper that just a little bit if you want to be taken seriously.
Sincerely,
Richard Nixon
Admin
Interesting tactic - You point out that Anonymous did not need to insult the intelligence of the person with which he/she was arguing, then immediately follow that by claiming that Anonymous's statement was "nonsense", and that you were "firm in my decision that you were wrong in everything that you were saying".
Hello pot - you are just as black as the kettle http://www.goenglish.com/ThePotCallingTheKettleBlack.asp
Admin
And the VB.net guys didn't think they need operator overloading. IMO C# has it to appeal to C++ developers. In fact, MS is susceptible to featuritis, so just because they do something does not necessarily mean it's necessary or even a good idea.
So you are going to look up comments or documentation for every single operator in the application?
Comments and documentation are necessary, but so are descriptive names. There is a reason why most programmers don't call their method e.g. "add" when the method does not at all add something.
Admin
#1 As for ther classic VB.NET against C#, if I may, the current VB.NET implementation is behind that of C#. Operator overloading will be introduced in VB.NET 2.0. That does say something about the fact that operator overloading is sufficiently important that it is to be included in the newer release of VB.NET. Of course, if we start to write in IL, then we would have all the features that the IL platform offers if we can stomach the obvious coding differences in IL (tongue-in-cheek)
#2 I think you miss the point entirely, and I suspect deliberately as well. If there is obfuscation in any application, it has more to do with the lack fo comments, and proper documentation (as well as use cases where applicable) rather than the implementation of operator overloading. Like any feature in .NET, it is to be used judiciously and after due consideration. Just because it is not available in one's pet language does not necessarily indicate it is a wise or unwise decision.
#3 There is no argument about descriptive names. However, descriptive names can also be quite deceptive if you do everything else but what the name implies. Hence, the "proper application of patterns and judicious commenting and documentation". Having come into a number of projects mid-stream, I have had the opportunity to see a lot of so-called decriptive names having 10% to do with what it implies and more with other stuff.
Admin
I am honoured that you have found it necessary to reply to me with such vitriol considering that there are a lot of your posts here in this august site (re: with a touch of sarcasm!) that have provided ample examples of you sinking your teeth in other with very personal and rather unprofessional comments.
I am honoured indeed. And by the way, I never do take your posts seriously. You have, you must admit, quite a following of people against your point of view.
Admin
Forgot the quote button
I am honoured that you have found it necessary to reply to me with such vitriol considering that there are a lot of your posts here in this august site (re: with a touch of sarcasm!) that have provided ample examples of you sinking your teeth in other with very personal and rather unprofessional comments.
I am honoured indeed. And by the way, I never do take your posts seriously. You have, you must admit, quite a following of people against your point of view.
Admin
While, technically speaking, operator overloading IS nothing but syntactic sugar, it's sugar i'll gladly take. As fun as myArrayLikeObject.itemAt(5) is, I'm much more fond of myArrayLikeObject[5]. I've yet to be confused by overloaded methods in Python. Having done a few moderate-sized numeric projects in Java, I can't tell you how sick I was of .add() versus +. While it can be abused, is it really so horrid as some make it out to be?
Admin
You're correct, of course. Everyone knows that the obvious, correct, universal syntax for object value equality is
(eqv? foo bar)
Admin
IMO the VB guys had to add operator overloading, because otherwise, VB programmers could not use some classes (written in C#) that offer certain features only through overloaded operators, not normal methods.
The problem is that operators look harmless and there is just a limited set of them; but since they save some typing, programmers are tempted to abuse them. Compared to methods, IDEs cannot help you that much finding the right operator. But don't get me wrong, I see some cases where operator overloading is justified and probably a good thing. Unfortunately, "hello-world" in C++ is an example of abused operator overloading.
Deceptive names are a problem with bad programmers, not an implicit problem with descriptive names. On the other hands, there is only a very limited number of overloadable operators, and their default semantic often does not match the semantic after overloading.
Admin
Flammable, and common, topic. Operator overloading wouldn't have survived if it was just artificial sweetener. Yes, technically it can be considered sugar, but given correct application sugar makes the difference between flat cookies and savory pastries. Strictly speaking, it might not be absolutely necessary but it do makes a great difference.
Good code is good code, good programmers are good programmers. Obfuscation is not a consequence of language features but the misapplication of language features.
This makes me think you consider VB > C#. I think many would consider that a strange claim... It seems to me that certain feature are missing from VB does not constitute a good authority for that features redundancy in other languages.
Irrelevant, both of you. Methods or operators can both be clear or enigmatic. Comments help provide insight into code but this is in addition to, and beyond, methods/operators/whathaveyou.
Here we have the core. Operator overloading is best used when performing "natural" operations, f.i. analogous to mathematics, set management, etc. Just as the good Java programmer would (hopefully) not make an "add" method that downloads sitcoms, neither would the good C++/C# programmer write such a "+" operator.
But there is one thing where operator overloading DO provide functionality beyond methods, and which Java thus cannot provide. Remember that you can overload stand-alone binary operators in C++, f.i., you can overload the global + operator. I did (mathematical) vector and matrix classes, for which I overloaded the relevant binary arithmetic operators. This allowes me to write code like
vector v;
matrix m;
double scalar;
...
m = m2 + m3 * v * scalar;
Highly natural syntax. Clear, concise. Immediately understandable. And would be very messy in a language devoid of operator overloading. Of course, this is the canonical example, but it is relatively easy to produce other, non-mathematical ones.
All here should agree to that this adheres closly to some of the Grail pieces of Good Programming Practices. Code minimisation, transparancy, compartamentalisation. A similar functionality would probably be possible to obtain in, f.i., Java, but would entail much more code, more distributed code, redundant code, and less aesthetic code.
Admin
Admin
Thank you for your insightful commentary! You fail to notice the difference between insulting someone in the manner that I was replying to and the act of informing someone how they are perceived based on their behavior. I am sorry that you were not able to deduce the difference. Better luck in the future and God Bless!
Sincerely,
Richard Nixon
Admin
Why does every second thread on this board has to end with evangelizing/bashing a programming language, comparing virtual d*ck sizes, or a combination of both?
I can tell you, you will not find self-affirmation in the internet.
Admin
I was just refering to the "experience C# team" part of the previous post. I guess the VB team is just as experienced... and so is the Java team... since operator overloading is not difficult to implement in technical terms, there must be reasons why some languages don't include this features while other do.
This is without doubt one example where operator overloading is used in a good and intuitive way. Unfortunately, at least for me, the first example of operator overloading was this:
Highly unnatural IMO.
Agreed; but IMO java is not a good choice for that kind of program anyway.
Admin
I agree to an extent; the fist time I saw STL C++ code I too wondered what they'd been smoking - it is not the most immediately intuitive or newbie-friendly syntax imaginable. However, they needed to overload operators for stream inserts and extracts, and in my opinion there are no better ones suited for the purpose.
As programmers we sometimes think too much about the rigidly formalistic aspects of our work. While we like to apply the term "aesthetic" about code we tend to have a very conservative and formal, in a mathematical sense, understanding of the concept. I hold that from a pictoral and symbolic perspective the stream input and extact operators << and >> are in fact very apt and aesthetic -as everyone upon using them immediately gathers they are arrows pointing at the source or target stream- and this is proven by the fact that almost everyone takes to their use quite fast and in fact quite intuitively.
I can't imagine what stream-akin code complete with manipulators etc would look like in a language without operator overloading... *shivers*
Admin
I don't think
std:cout<<1<<2<<(3<<4)<<5
is easy to take or intuitively. What kind of program would make extensive use of that?
Admin
Well, that artificial bit-shift operation (3<<4) makes it somewhat less readable, but I'd say that a more real-world-like snippet, like
is quite intuitive.
Hopefully none. But in a language where you can't do output like above you will instead be forrced to multi-line behemoths or class/function multiplication hell. The STL stream operators is a good example of the entire discussion: they are easy to make into both good examples on the benefits of operator overloading and anti-examples of the horrors of operator overloading. Again, we come down to that good programmers are capable of making judicious use of language features. But it is poor languages-or a poor language philosophy- that restricts the programmer's toolset .Toolset restriction might preempt b0rked coders from shooting themselves in the foot in certain ways (though they will always find other means to that) but it will also limit and restrain the good programers.