473,842 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

On Java and C++

Java programmers seem to always be whining about how confusing and
overly complex C++ appears to them. I would like to introduce an
explanation for this. Is it possible that Java programmers simply
aren't smart enough to understand C++?

This is not merely a whimsical hypothesis. Given my experience with
Java programmers --- the code they write and the conversations they
have --- Occam's Razor points to this explanation. For example,

"Oooh I'm confused about the difference between pointers, references,
and objects! How confusing!"

"Oooh operator overloading confuses me! The expression x + y is so
confusing, who knows what's happening with that? If x and y are
complex numbers, what the hell could x + y mean?"

"Oooh multiple inheritance is so confusing! Though I am both a father
and a programmer, I still find it so confusing how the same object can
be two different things! How confusing!"

"Oooh and virtual bases are so bizarre! I am a student --- myself
'the father' is the same student as myself 'the programmer' --- but
nonetheless the idea of virtual bases is absolutely confounding and
confusing to me!"

Again, Occam's Razor is a valuable tool here. In deciding among
competing hypotheses, choose the simplest one. To impartial observers
of indoctrinated Java programmers, the explanation is simple indeed.

Apr 26 '06
458 21563
werasm wrote:
http://c2.com/cgi/wiki?MicrosoftSampleCode


Hmmm, that's Win32 for you. A good c++ implementation would hide that
as far as possible - but you know that! Yes, it compiles with a c++
compiler - so does assembler. Obviously non-standard.


Are we discussing the same sample code? Its leaks and overflow risks all
come from its use of Standard C. (Arrays, if statements, etc.)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 3 '06 #341

Phlip wrote:
werasm wrote:
http://c2.com/cgi/wiki?MicrosoftSampleCode
Hmmm, that's Win32 for you. A good c++ implementation would hide that
as far as possible - but you know that! Yes, it compiles with a c++
compiler - so does assembler. Obviously non-standard.


Are we discussing the same sample code? Its leaks and overflow risks all
come from its use of Standard C. (Arrays, if statements, etc.)


Yes, we are. Typical Win32 style programming. Yes, using standard C.
This kind of code is usually tucked far away, behind a clean inteface
and tested well. The LPWSTR or whatever they may call it, needn't be
used std::wstring would do the job. BTW, how are the ifs exploitable?
The file IO could be scope guarded by either using ofstream, or
wrapping the API call.

Things could have looked like this or similar:

bool CreateURLShortc ut(const std::wstring& url, const std::wstring&
shortcutFile)
{
using namespace std;
wofstream of( shortcutFile, ios::out | ios::trunc );
if( of )
{
return ( of << url );
}
return false;
}
I suppose this body would work too, but little terse for my liking:
....
{
using namespace std;
wofstream of( shortcutFile, ios::out | ios::trunc );
return (of << url);
}

Please comment on exploitability, as that is not my strength.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


May 3 '06 #342
werasm wrote:
Yes, we are. Typical Win32 style programming. Yes, using standard C.
This kind of code is usually tucked far away, behind a clean inteface
and tested well. The LPWSTR or whatever they may call it, needn't be
used std::wstring would do the job. BTW, how are the ifs exploitable?
The file IO could be scope guarded by either using ofstream, or
wrapping the API call.
The ifs are fragile because the file IO could be scope-guarded.

(I don't know if handle exhaustion is exploitable, but it's still icky!!)
Things could have looked like this or similar:
Thanks! I put them both on that web page, as a rejoinder. But I would have
passed the strings by copy. I have heard that some std::basic_stri ng
implementations don't copy-on-write, but I don't care.

Premature optimization is no excuse for fragility!
Please comment on exploitability, as that is not my strength.


And that's the point: Lean C++ code is trivially bulletproof, and trivially
reviewable. Lean C code is trivially exploitable!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 3 '06 #343

Phlip wrote:
Thanks! I put them both on that web page, as a rejoinder. But I would have
passed the strings by copy. I have heard that some std::basic_stri ng
implementations don't copy-on-write, but I don't care.
Then your programs can very well be incredibly slow compared to what
they would be.
Premature optimization is no excuse for fragility!


There's no excuse for premature pessimization either. It is quite
clear that passing around a string by value will cause major slow downs
in code if those strings are not ref counted and copy on write. One of
the most common implementations of the std library use such a string.
Passing in objects as const & is one simple step to remove a common
performance problem.

May 3 '06 #344

Mishagam wrote:
Noah Roberts wrote:
Mishagam wrote:
C++ references are not SO different from pointers. Just like Roedy Green
said - one more addressing mode. I doubt any well designed language
(like Java) would have (or has) references.


Java has references.

Excuse me. I meant separate references and pointers with so close
functions. Java has references, but they have also some features of C++
pointers like they can be null.


C++ pointers and references have completely different purposes. The
fact that Java lacks pointers is just another can't.

I can't say Java's, "everything is a reference except when it is not,"
is a move up from having explicit value, reference, and pointer
semantics that operate in a uniform manner.

May 3 '06 #345
Noah Roberts wrote:
There's no excuse for premature pessimization either.


If you discover you must optimize, you can convert from foo(string bar) to
foo(string const & bar) in only one logical place; typically 2 physical
places. The change won't cause force other designs to change.

When you have a design decision that's harder to reverse, _then_ you fret
about its performance as you commit it.

(My current boss, though otherwise good at coding, refuses to allow us to
rely on the return value optimization, so maybe I take it out on this
newsgroup;)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 3 '06 #346

Phlip wrote:
Noah Roberts wrote:
There's no excuse for premature pessimization either.


If you discover you must optimize, you can convert from foo(string bar) to
foo(string const & bar) in only one logical place; typically 2 physical
places. The change won't cause force other designs to change.

When you have a design decision that's harder to reverse, _then_ you fret
about its performance as you commit it.

(My current boss, though otherwise good at coding, refuses to allow us to
rely on the return value optimization, so maybe I take it out on this
newsgroup;)


Return value optimization is not always available and that IS a hard to
reverse design decision.

Besides, that won't help you with inputs.

May 3 '06 #347
Noah Roberts wrote:
Return value optimization is not always available and that IS a hard to
reverse design decision.
I didn't say it wasn't.
Besides, that won't help you with inputs.


I didn't say it did.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 3 '06 #348

Phlip wrote:
Noah Roberts wrote:
Return value optimization is not always available and that IS a hard to
reverse design decision.


I didn't say it wasn't.
Besides, that won't help you with inputs.


I didn't say it did.


Then you should know better.

May 3 '06 #349
Just for fun,

Phlip <ph*******@gmai l.com> wrote:
Java:

- write once debug everywhere
I find this to be not at all true. In fact, most of what I do for a
living these days is write code (and manage a development team that
writes code) in Java on Windows, and deploy the code on Linux. In the
last five years of doing so, I have seen exactly ONE bug that was
operating system specific... and it failed on Windows but not Linux,
rather than the other way around. It had to do with another programmer
misinterpreting String.replaceA ll and not realizing that the parameters
were regular expressions, and passing the platform-specific file
separator as a parameter. Of course, on Windows that was a backslash
and resulted in an error trying to parse the regular expression.

That's it. No other bugs in five years. Of course other bugs are
possible. There are even areas I'd describe as pitfalls: character
encodings, Runtime.exec, and the like. However, I haven't run into them
in practice. I have enough confidence that I feel pretty good releasing
code tested on Windows directly onto a production system running Linux.

(I imagine there are also issues with the Macintosh and GUI apps, since
I see questions about it. I wouldn't really know.)
- forgets everything on all its CLASSPATHs at
the drop of a hat
I don't understand what you mean by this. As far as I can tell, it's a
completely senseless statement. If you're talking about a CLASSPATH
environment variable, then this is an operating system issue, not at all
related to Java. If you're talking about the classpath (in more general
terms, not the environment variable) used by the system class loader,
then it doesn't change, ever. Perhaps you managed to confuse yourself
at some point with a custom class loader?
- projects must depend on fragile and
programmer-hostile tools, like ANT,
that make easy things hard and hard
things absurd
I also dislike ant. That's why I've never used it -- except for a brief
period of experimentation with XDoclet, which seems to depend on it. So
far as I can tell, I don't suffer any great disadvantages from not using
ant.
- impersonates the worst of C++
multiple String classes
Are you referring to my earlier post where I listed different ways of
representing character sequences? The point of that post was that
different functionality requirements require different types for the
same basic concept, regardless of language... The only dispensable
representation from my list in Java is StringBuffer, and it will go away
over time.
and last but least generics!
I have my complaints about generics in Java. Their existence, though,
is not one of those complaints.
- arrays aren't really arrays. But they really
are. Kinda.
Eh? Arrays are arrays. Perhaps you're too married to some language-
specific concept of what an array is.
- pretends you broke something if your file
name differs from your class name. Figure
it out, stoopid!
Exactly what important information did you expect to express by naming
your source file something different? A decent development environment
might have mitigated the headache you would have caused by doing so...
but it's no justification.

(Technically, this is not a requirement of the Java language. It is
only suggested by the Java Language Specification, and is enforced by
choice in all major Java compilers. The JLS doesn't mention source
files at all in any normative statement.)
- when a smart editor like Eclipse can finish
every line for you, it makes you wonder
what the language is _there_ for!
This is perhaps a useful thing for some to wonder, but only so that they
can get around to knowing the answer. The answer is that we're talking
about two different sets of things:

(1) What can be reasonably guessed, with the assurance that the
programmer will see the guess and correct it if it's wrong?

(2) What is so certain to be the programmers meaning that it can be done
without guessing?

Clearly, there are things that fit into set (1) without also fitting
into set (2). Those things should be implemented with IDE auto-complete
features, and not with the language itself. Whether the boundaries are
drawn correctly is another issue, and there are undoubtedly places where
the boundaries could be drawn better... but criticizing the need for
auto-complete entirely doesn't look reasonable.
- adds keywords, like interface, based on
someone else's preconceived notion of good
design. Not keywords like 'virtual', based
on what the hardware will actually do
So you don't like the lack of multiple inheritance. That's fine.
However, the existence of interfaces as a language feature is perfectly
sensible in a language without multiple inheritance. This is not a
separate gripe.
- provides whole new categories of bugs, based
on zombie objects, non-deterministic
destructors, redundant finally blocks, all
under the excuse we are saving you from all
the C++ memory errors that a good standard
library implementation will save you from
anyway
It's not true that, in practice, good libraries have saved C++
programmers from memory management bugs. Results indicate that long-
running applications in C++ tend to benefit when the new operator is
overloaded with a simple conservative garbage collected implementation,
and delete with a no-op. This is because the accidental memory leaks
from the dumb conservative collector and from these "zombie" objects are
substantially less than the memory leaks from typical memory allocation
bugs. Clearly something is wrong. If you wish to embark upon a massive
education campaign to teach C++ programmers how to avoid these bugs,
that would perhaps be a noble undertaking. In the interim, though, it
would be counterproducti ve to interfere with efforts to avoid memory
management bugs that do occur in real-life applications. Modern Java
implementations have non-conservative collectors that are even better.
- GUIs require block closures and dynamic
typing. But what language does your boss
tell you to write the GUI in???


Anonymous inner classes actually tend to solve this problem very easily.
Yes, they are more wordy. If you don't like wordy languages, then you
shouldn't use Java. (And shouldn't take jobs where Java is used, or
should just deal with it when the language is decided on; your choice!)
The only significant lost feature is the ability to modify the values of
local variables. That feature would be nice just for consistency, but
it is probably more dangerous than it's worth in practice.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
May 3 '06 #350

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6821
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what the issue is. Thanks Ravi
1
6923
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
9294
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
0
5662
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to connect to the DB I get the following exception at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2120)
1
9668
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
12
5935
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it says: usage: java fit.FitServer host port socketTicket -v verbose I think this is because I do not understand the jython mechanism for inheritance (yet).
0
3292
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)) and in both cases, get the following list returned from calling getDeclaredFields() on java.lang.ClassLoader via this code snippet: Field fields = loaderClass.getDeclaredFields(); for (int i = 0; i <...
1
4310
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all Im using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in Linux there is an error. The code that fails is the following: SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Im sure that code is ok. In fact, Ive found that in several...
0
3301
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
9870
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10942
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10671
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10310
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5884
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4088
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3142
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.