473,889 Members | 1,409 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 21629
Noah Roberts <ro**********@g mail.com> 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.


Unfortunately, the meaning of the term "reference" and "pointer" is
language-dependent. Java certainly does not have references in the C++
sense. All Java identifiers are used to designate distinct variables;
you never have two different variable names that both mean the same
thing.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
May 5 '06 #401
The Ghost In The Machine <ew***@sirius.t g00suus7038.net > wrote:
ObOffTopic: It appears gcc has a similar problem. I'll have to see if a
"dead" pointer store is mistakenly optimized away in a non-main()
routine; that could lead to some subtle C++ bugs.


Since a store to a NULL pointer results in undefined behavior, it would
not be a mistake to optimize this away, at least according to the spec.
Of course, it would be a mistake for practical reasons.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
May 5 '06 #402
Bent C Dalager <bc*@pvv.ntnu.n o> wrote:
In article <11************ **********@j73g 2000cwa.googleg roups.com>,
Noah Roberts <ro**********@g mail.com> wrote:

The Ghost In The Machine wrote:

For its part Java has its own problems with arrays:

int[] s = new int[]{1,2,3};


Ick, and you say C++ has ugly syntax.

int s[] = {1, 2, 3};


This exact line will also work as expected in Java.


Yes, but it would be bad form in Java. Better to write:

int[] s = {1, 2, 3};

The fact that Java accepts a pseudo-C++ declaration syntax for arrays
for no good reason is not knowledge worth spreading. It was a mistake.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
May 5 '06 #403
JustBoo <Ju*****@BooWho .com> wrote:
As you can guess, I can continue.
Dropping all these choices first - makes programming easier, you have
less things to bother about,


If you were never meant to be a programmer in the first place I
guess.... Hmm, VB and Java programmer are a lot alike. A river in
Egypt, baby, a river in Egypt....


On the other hand, if you think that making trivial choices like
constitutes the greater part of programming, then I'm quite sure I'd not
want to hire you to write code. When making choices about minor matters
of convention, the ONE overriding concern is how to be consistent so
that other programmers are not constantly guessing at what you did.
Save your creativity for things that affect the quality of the resulting
product.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
May 5 '06 #404
In article <%qJ6g.6812$Fg4 .2320@clgrps12> ,
ow***@castortec h.com says...

[ ... ]
<Java>
class NodeToken {
public int beginColumn, endColumn;
}

class Token {
public int startColumn, endColumn;
}
</Java>

I'm trying to merge these two classes together. If they had used accessor
methods, I could have a methods setStartColumn( int) and setBeginColumn( int)
affect the same private field, so that the changes would be seen by either
interfaces.
This seems to be almost entirely orthogonal to the user
of accessor functions.
Unfortunately, they didn't use accessor methods, and instead
used public fields, so now I've got to start by adding the accessor methods
to each seperate classes, mark the fields as deprecated, check for all
access to those fields, change those to invoke the accessor methods, then
merge the class, then simplify the API.


Well, in C++, you could pretty easily do something like
this:

class Mergedtoken {
public:
int beginColumn, endColumn;
int &startColumn ;

MergedToken() : startColumn(beg inColumn) {}
};

This produces essentially the same effect as you'd be
hoping to get via the accessor functions -- but without
the ugly syntax.

I'll admit I've never had to try this with Java, so I'm
not _sure_ its references will support this like C++
does, but offhand I can't think of any particularly good
reason they shouldn't.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 5 '06 #405

Chris Smith wrote:
Noah Roberts <ro**********@g mail.com> 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.


Unfortunately, the meaning of the term "reference" and "pointer" is
language-dependent. Java certainly does not have references in the C++
sense. All Java identifiers are used to designate distinct variables;
you never have two different variable names that both mean the same
thing.


Really?

class Y
{
A a;
public:
set_a(A an_a) { a = an_a; }
};

class Z
{
A a;
public:
set_a(A an_a) { a = an_a; }
};

....
Y y = new Y();
Z z = new Z();
A a = new A();

y.set_a(a);
z.set_a(a);
....

How about a less complex example...

A a = new A();
A a2 = a;

Are you saying all of those a's are different in java? It has been
quite a while since I last used it but it seems to me that they are all
the same instance. That would make C++ references and Java references
pretty close to the same thing.

May 5 '06 #406

"Noah Roberts" <ro**********@g mail.com> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...

Chris Smith wrote:

Unfortunately, the meaning of the term "reference" and "pointer" is
language-dependent. Java certainly does not have references in the C++
sense. All Java identifiers are used to designate distinct variables;
you never have two different variable names that both mean the same
thing.


How about a less complex example...

A a = new A();
A a2 = a;

Are you saying all of those a's are different in java? It has been
quite a while since I last used it but it seems to me that they are all
the same instance. That would make C++ references and Java references
pretty close to the same thing.


I'm going to avoid using the term "reference" and "pointer" in my
explanation here of Java's behaviour, and use a slightly different example
which will hopefully be more clear:

Integer a1 = new Integer(5);
Integer a2 = a1;

"a1", and "a2" are two different local variables which point to some
instance of Integer. They are both pointing to the same instance of Integer.
If, later on, you assign a different instance of Integer to "a1", "a2" will
still point to the old instance of Integer. So, for example, if you add the
following line:

a1 = new Integer(42);

then "a1" will now point to an Integer object which conceptually represents
the platonic concept of the number 42, while "a2" will still point to the
old Integer object which conceptually represents the platonic concept of the
number 5.

My understanding is that in C++, if you changed "a1" in that manner, and
if "a2" were a reference, then "a2" would also be updated to point to 42,
and not to 5.

- Oliver

May 5 '06 #407

"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:MP******** *************** @news.sunsite.d k...
In article <%qJ6g.6812$Fg4 .2320@clgrps12> ,
ow***@castortec h.com says...

[ ... ]
<Java>
class NodeToken {
public int beginColumn, endColumn;
}

class Token {
public int startColumn, endColumn;
}
</Java>

I'm trying to merge these two classes together. If they had used accessor
methods, I could have a methods setStartColumn( int) and
setBeginColumn( int)
affect the same private field, so that the changes would be seen by
either
interfaces.


This seems to be almost entirely orthogonal to the user
of accessor functions.
Unfortunately, they didn't use accessor methods, and instead
used public fields, so now I've got to start by adding the accessor
methods
to each seperate classes, mark the fields as deprecated, check for all
access to those fields, change those to invoke the accessor methods, then
merge the class, then simplify the API.


Well, in C++, you could pretty easily do something like
this:

class Mergedtoken {
public:
int beginColumn, endColumn;
int &startColumn ;

MergedToken() : startColumn(beg inColumn) {}
};

This produces essentially the same effect as you'd be
hoping to get via the accessor functions -- but without
the ugly syntax.

I'll admit I've never had to try this with Java, so I'm
not _sure_ its references will support this like C++
does, but offhand I can't think of any particularly good
reason they shouldn't.


Java doesn't have anything like what C++ calls "references ". You can't
alias a variable in Java.

Also, to make things more interesting, the classes I'm trying to merge
actually keep track of something called "SpecialTokens" . In one case,
they're implemented as a doubly-linked list, in another as a vector. The
Java code looks something like:

<Java>
class NodeToken {
public int beginColumn, endColumn;
public Vector<NodeToke n> specials;
}

class Token {
public int startColumn, endColumn;
public Token nextSpecial, prevSpecial;
}
</Java>

So the problem again comes with two different representations for the
same ideas, and I can't simply take both fields, as their contents need to
be "synchroniz ed" somehow.

- Oliver

May 5 '06 #408

Oliver Wong wrote:
"Noah Roberts" <ro**********@g mail.com> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...

Chris Smith wrote:

Unfortunately, the meaning of the term "reference" and "pointer" is
language-dependent. Java certainly does not have references in the C++
sense. All Java identifiers are used to designate distinct variables;
you never have two different variable names that both mean the same
thing.


How about a less complex example...

A a = new A();
A a2 = a;

Are you saying all of those a's are different in java? It has been
quite a while since I last used it but it seems to me that they are all
the same instance. That would make C++ references and Java references
pretty close to the same thing.


I'm going to avoid using the term "reference" and "pointer" in my
explanation here of Java's behaviour, and use a slightly different example
which will hopefully be more clear:

Integer a1 = new Integer(5);
Integer a2 = a1;

"a1", and "a2" are two different local variables which point to some
instance of Integer. They are both pointing to the same instance of Integer.
If, later on, you assign a different instance of Integer to "a1", "a2" will
still point to the old instance of Integer. So, for example, if you add the
following line:

a1 = new Integer(42);

then "a1" will now point to an Integer object which conceptually represents
the platonic concept of the number 42, while "a2" will still point to the
old Integer object which conceptually represents the platonic concept of the
number 5.

My understanding is that in C++, if you changed "a1" in that manner, and
if "a2" were a reference, then "a2" would also be updated to point to 42,
and not to 5.


Your misunderstandin g is caused by your lack of acknowledgement that
Java hides references and has no such thing as "pointers".

Java:

A a = new A();
A a2 = a;

I now have two references to the same A but I cannot make a variable
that is a reference to the variable a.

Later changing a: a = new A(); as you said makes a and refer to
different data.

C++:

A a;
A & a2 = a;

a = new A() <- invalid.

a = A() <- rewrites the data a refers to. Actually calls a function
that assigns values...not a reference swap.

As you can see the syntax looks the same but the semantics are
completely different.

Lets change the C++ to be more equivelent with Java:

A * a = new A();
A * a2 = a;

Now assigning a a new value (a = new A() for instance) results in the
two pointing to different locations.

So I suppose you are right...Java and C++ references are
different...C++ references are more powerful:

class Y
{
A & a;
public:
Y(A & in) : a (in) {}
};

class Z
{
....same as Y
}

....
A a;
A a2;
Y y(a);
Z z(a);

a = a2;

a, Y::a, and Z::a now share the same data.

Java references are pointers without the benifits of being pointers and
without an explicit notation to label them as such to reduce confusion.

May 5 '06 #409
In comp.lang.java. advocacy, Chris Smith
<cd*****@twu.ne t>
wrote
on Fri, 5 May 2006 09:59:31 -0600
<MP************ ************@ne ws.astraweb.com >:
Bent C Dalager <bc*@pvv.ntnu.n o> wrote:
In article <11************ **********@j73g 2000cwa.googleg roups.com>,
Noah Roberts <ro**********@g mail.com> wrote:
>
>The Ghost In The Machine wrote:
>>
>> For its part Java has its own problems with arrays:
>>
>> int[] s = new int[]{1,2,3};
>
>Ick, and you say C++ has ugly syntax.
>
>int s[] = {1, 2, 3};


This exact line will also work as expected in Java.


Yes, but it would be bad form in Java. Better to write:

int[] s = {1, 2, 3};

The fact that Java accepts a pseudo-C++ declaration syntax for arrays
for no good reason is not knowledge worth spreading. It was a mistake.


I do like the

int[] s

form; it's easier to find the type. :-) Whether 1.6 will produce
copious warnings when encountering

int s[]

is an interesting question best directed at Sun, perhaps. :-)

--
#191, ew****@earthlin k.net
Windows Vista. Because it's time to refresh your hardware. Trust us.
May 5 '06 #410

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

Similar topics

0
6823
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
6926
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
9298
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
5664
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
9675
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
5938
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
3295
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
4314
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
3304
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
9961
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
9805
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,...
1
10887
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
10439
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
9602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6021
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
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.