473,842 Members | 1,838 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 21553
Noah Roberts wrote:
Mishagam wrote:
I don't
think you can work without using references in big program.
(I assume again that you mean pointers here.)You surely can. Modern C++
uses almost no raw pointers. Instead it uses objects called smart
pointers and these could well rely on reference counting. I, unfortunately, not very fluent in C++ smart pointers. I know that COM
objects used reference counting, and it was major pain in ass. However I
didn't use smart pointers there - mey be they would alliviate problem?
If you use reference counting you cannot be sure when you object gets
destructed, you cannot use RAII, you can get memory leak because
somebody forgot to delete reference. You are in the same position as in
Java, except that GC works with cyclic references.


That comparison is quite a stretch. GC is, as far as I know and as far
as Java implements it, totally unaccessable to the programmer. Can you
force the GC to delete anything?

You can call something like System.gc(). But if you want to be 100% sure
that something happen you don't rely on gc, you use finally blocks.
There may be certain times when you can expect the GC to do some cleanup but you cannot guarantee it nor
can you control it. On the other hand, reference counting using smart
pointers is 100% programmer controlled. If you have valuable resources other that memory, with reference
counting they get called only when all pointers to it in all parts of
program are explicitly closed. You have no help here from RAII, smart
pointers or anything.
You have help from C++ only with one reference in one place in program
(and in RAII object). Then you don't need reference at all you can use
value object. Otherwise there is no help, it becomes worse than finally
block.
You _can_ force something to get deleted and you know for certain that the object will get deleted
the instant the last reference to it leaves scope or is destroyed.
This is a totally dependable action that is 100% guaranteed. With the
GC you can have no references to an object but it hangs out until who
knows when and then gets destroyed sometime after the last reference to
it leaves scope or is destroyed..../sometime/ after. You can close object in finally block and be 100% sure that non-memory
resources are freed. Then you can be 100% sure that GC will do it's best
to provide you with memory. You also can be sure (not so in C++) that
this memory isn't pointing to other active objects. (of course last part
happen in C++ only if programmer made some error somewhere in program).
Does that mean it works if the programmer doesn't work? No. But it
means that RAII _can_ be depended on to perform the actions it has been
described so long as the programmer does their job. Requiring correct
code is not unreasonable and in reality RAII is quite effective at
keeping a lot of bugs out of code so long as it is followed.
For places where
value-based objects don't fit (your example with a container that
should hold elements of a type or descendants of that type), you simply
store a smart pointer.

I never actively used smart pointers. I looked on Web - there are
actually a lot of different types of smart pointers in C++ (as usual,
Java has one close to optimal choice).


Java has no such choice. Java doesn't need smart pointers as it has no
pointers to begin with. Nor could it support a construct remotely
resembling a smart pointer. Half of the benefits of smart pointers are
the fact that they look like any other pointer and can be used anywhere
a normal pointer would be...this requires operator overloading.

Ok, Java has references, which resemble C++ pointers, but are better.
Java doesn't need smart pointers because references provide most of
advantages of smart pointers.
A smart pointer is nothing else but an RAII wrapper around a gatherable
resource that must be released. If you have several pointers (you insist on pointers, however C++ HAS
references which are almost the same as pointers from this point of
view) to the same object/resource, you cannot speak about RAII. Logic of
releasing resource when going out of block doesn't work.
I can imagine you can hope that if you made all pointers to your object
smart, and if you didn't pointed to your object from non RAII objects.
Then you can be sure than when you stop your program (and went out of
all blocks) you released all objects. If you had only one thread . But
OS will release all your resources anyway.
If you have only one pointer you can just as well use value object.
Most of the time we are speaking of memory and some dynamically allocated object. There are several types
because there are several targets. One pointer does not fit all
situations. For instance, should it check to be sure the object exists
before dereferencing it? Does it need some sort of shared process
locking mechanism? Not everyone wants these things....choic e is good
in this case as it allows one to choose the correct mechanism for their
needs. There is no "most optimal" choice here.

I can't parse the rest of the post.

Apr 29 '06 #231
You made me think you code directly in binary machine instructions. At
least that's your favorite language.

As for me, I'd dream to program in English, but someone is yet to invent
an English compiler for me.

Regards,
Ben
Apr 29 '06 #232
Andrey Kuznetsov schrieb:
I think applets are much worse than Flash. They take a long time to load,
their abilities to communicate with the browser (Javascript) is even worse
than Flashs.


you should try Opera.
While Firefox needs up to 5 second for applet on my page
(http://reader.imagero.com),
with Opera it starts immediately!


Some browsers handle the initialisation better than others. But for most
users, applets just take a long time to appear. Flash is generally much
quicker, regardless of platform or browser.
Timo
Apr 29 '06 #233
benben wrote:
You made me think you code directly in binary machine instructions. At
least that's your favorite language.

As for me, I'd dream to program in English, but someone is yet to invent
an English compiler for me.

Regards,
Ben


already done - google Domain Specific languages
Apr 29 '06 #234
Tor Iver Wilhelmsen wrote:
- write once debug everywhere


Less of an issue now than back in the horrid days of applets.


Ooh, I love it when they fight back.
- arrays aren't really arrays. But they really
are. Kinda.


They are. But they're not C/C++ "syntactic sugar for pointers".


There is no language C/C++.

Arrays in C++ convert to pointers very easily, but they are still
first-class objects. They have a complete type, including a length, and all
these can be manipulated by ... generics.
- when a smart editor like Eclipse can finish
every line for you, it makes you wonder
what the language is _there_ for!


Noone prevents you from using vi to write Java. Well, maybe the Emacs
tribalists do.


You missed the point. If an editor can parse your code and predict what you
are trying to do, then the language could too. The language itself could
finish these lines, and all I'd have to do is start them.

Read:

http://c2.com/cgi/wiki?RubyVsJava

log4j's src folder has 31,764 lines of code.

log4r's src folder has 2,071 lines of code.

That's not just fewer lines; it's an order of magnitude fewer, for the exact
same feature set.

Please don't say "that's because Java programmers write more professional
comments..."
- 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


Language defines what is virtual or not. It's up to the (virtual)
hardware to comply.


Again you missed the point. 'virtual' is a weak, primitive keyword that I
can use to assemble a class or an interface. I can control how concrete and
how abstract the class is. Just because I _should_ create interfaces doesn't
mean the language should _force_ me to.

You cannot legislate morality. (You _can_, however, legislate immorality!)
- at least C++ makes some of the benefits
of dynamic typing available. Java instead
enforces such a narrow view of static
typing that you can't even simulate
those benefits


Is that the "let's pretend this block of memory is something else"
stuff "inherited" from C's union and void*?


Write an Any or Variant class in Java.
- GUIs require block closures and dynamic
typing.


Whatever for? I've written GUIs in Java, and the Swing MVC system is
decent enough to use. Where do you need either of those?


You have insufficient experience with block closures.

A GUI should be event-driven. Block closures make those as easy as falling
off a log. Most of the code you write in a static-typing OO language, for a
GUI, is excess plumbing to route events to handlers, and then excess
plumbing to convey state variables into handlers. Block closures simply turn
that problem inside out. The language does the plumbing, and all you need to
do is add the custom behaviors.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 29 '06 #235
Alex Buell wrote:
[A spanking] to all VB programmers and the buggers that dreamed up the
language! Take a nap, it saves lives.


A 6-hour work day to all VB buggers.

As the mental clarity takes effect, they will ask "why am I debugging all
the time instead of thinking?"

;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 29 '06 #236
Andrew McDonagh wrote:
Ian Collins wrote:
Phlip wrote:
Ian Collins wrote:
You can do the same with a number of C++ compilers, whether or not they
support the 'export' keyword.

Newbie: Why do I have to define classes twice?

Guru: You don't define them twice. You declare them in header
files, define them in header files, and define their methods
in implementation files.

Newbie: Why do I have to de...scribe classes three times?
Because it makes then easier to read?

I've been doing a lot of PHP lately and the biggest problem I have is
not being able to see all of the methods on one page. So I end up
creating an interface for each class as a documentation tool.


This is very easily over come by using a decent IDE which shows all the
method signatures - all the time. eclipse, intellj, etc...


And allow folding of areas you currently don't want to look at.
but then again some strange people still think vi is good.....

Apr 29 '06 #237

"Timo Stamm" <ti********@arc or.de> wrote in message
news:44******** *************** @newsread4.arco r-online.net...
Andrey Kuznetsov schrieb:

[Timo wrote:]
I think applets are much worse than Flash. They take a long time to
load, their abilities to communicate with the browser (Javascript) is
even worse than Flashs.


you should try Opera.
While Firefox needs up to 5 second for applet on my page
(http://reader.imagero.com),
with Opera it starts immediately!


Some browsers handle the initialisation better than others. But for most
users, applets just take a long time to appear. Flash is generally much
quicker, regardless of platform or browser.


FWIW, in my experience, Flash is not "much quicker".

- Oliver

May 1 '06 #238
*SNIP*
That comparison is quite a stretch. GC is, as far as I know and as far
as Java implements it, totally unaccessable to the programmer. Can you
force the GC to delete anything? There may be certain times when you
can expect the GC to do some cleanup but you cannot guarantee it nor
can you control it. On the other hand, refrence counting using smart
pointers is 100% programmer controlled. You _can_ force something to
get deleted and you know for certain that the object will get deleted
the instant the last reference to it leaves scope or is destroyed.
This is a totally dependable action that is 100% guaranteed. With the
GC you can have no references to an object but it hangs out until who
knows when and then gets destroyed sometime after the last reference to
it leaves scope or is destroyed..../sometime/ after.
Which is a problem because? You dont have to point out continuously that the
GC doesnt allow exact control over cleaning up discarded objects. We all
know that and consider that a good thing. What'd be more interesting is to
finally hear a valid reason why it's actually a problem rather than an
advantage.

Does that mean it works if the programmer doesn't work? No. But it
means that RAII _can_ be depended on to perform the actions it has been
described so long as the programmer does their job. Requiring correct
code is not unreasonable and in reality RAII is quite effective at
keeping a lot of bugs out of code so long as it is followed.


Why are you so stuck on wanting control over these kind of things? Are you
under the impression you do a better job at manually alloc/deallocing memory
and cleaning up objects than the latest generation Java GC will do? I'm
getting a bit tired of all these unsupported assumptions. Either come with
practical examples/facts or just agree to disagree.
May 1 '06 #239

"Remon van Vliet" <re***@exmachin a.nl> wrote in message
news:44******** *************** @news.xs4all.nl ...
[...]
There may be certain times when you
can expect the GC to do some cleanup but you cannot guarantee it nor
can you control it. On the other hand, refrence counting using smart
pointers is 100% programmer controlled. You _can_ force something to
get deleted and you know for certain that the object will get deleted
the instant the last reference to it leaves scope or is destroyed.
This is a totally dependable action that is 100% guaranteed. With the
GC you can have no references to an object but it hangs out until who
knows when and then gets destroyed sometime after the last reference to
it leaves scope or is destroyed..../sometime/ after.
Which is a problem because? You dont have to point out continuously that
the GC doesnt allow exact control over cleaning up discarded objects. We
all know that and consider that a good thing. What'd be more interesting
is to finally hear a valid reason why it's actually a problem rather than
an advantage.

[...]
Why are you so stuck on wanting control over these kind of things? Are you
under the impression you do a better job at manually alloc/deallocing
memory and cleaning up objects than the latest generation Java GC will do?
I'm getting a bit tired of all these unsupported assumptions. Either come
with practical examples/facts or just agree to disagree.


Even if (whoever "you" refers to here) does believe that they are better
at manually allocating and deallocating memory than a modern GC, there's
still the issue of whether the average programmer is better as well, or if
("you") is simply gifted in that respect. If it's the latter, non-garbage
collected programming languages may be suitable for ("you"), but not
suitable for anyone else.

- Oliver

May 1 '06 #240

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 I´m 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); I´m sure that code is ok. In fact, I´ve 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
9867
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10669
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,...
1
7853
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7028
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
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
4086
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3140
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.