473,889 Members | 1,350 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 21648
"Noah Roberts" <ro**********@g mail.com> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.com.. .

char * p = "Another rainy day in Georgia";

void routine(const char * p, char * q)
{
for(;*p;p++, q++) *q = (*p) + 1;
}


Well that function is bad for numerous reasons, not the least of which
is its use of char* instead of string. There are numerous ambiguities
that need be established that can only be so by looking at that code.
For one, who owns q?

Also, even a java programmer should see that it blows up. If you are
familiar with pointers enough to even know what that does you can see
that it doesn't work.


I think to grok the above code, you have to know certain things that you
might never learn if you programmed only in Java, such as the idea that char
strings are terminated by 0.

- Oliver

May 4 '06 #381

Oliver Wong wrote:
"Noah Roberts" <ro**********@g mail.com> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.com.. .

char * p = "Another rainy day in Georgia";

void routine(const char * p, char * q)
{
for(;*p;p++, q++) *q = (*p) + 1;
}


Well that function is bad for numerous reasons, not the least of which
is its use of char* instead of string. There are numerous ambiguities
that need be established that can only be so by looking at that code.
For one, who owns q?

Also, even a java programmer should see that it blows up. If you are
familiar with pointers enough to even know what that does you can see
that it doesn't work.


I think to grok the above code, you have to know certain things that you
might never learn if you programmed only in Java, such as the idea that char
strings are terminated by 0.


If you don't understand that you never would have written it.

You might not understand the following if you programmed only in
QBasic:

class X implements Y {}

May 4 '06 #382
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.

Cheers
Bent D
--
Bent Dalager - bc*@pvv.org - http://www.pvv.org/~bcd
powered by emacs
May 4 '06 #383
Noah Roberts wrote:
Wouldn't it be nice to have local type inference here?

defÂ*lÂ*=Â*ne wÂ*ArrayList<En try<String,Â*In teger,Â*Object> >();


You mean like this?:

typedef ArrayList<Entry <String, Integer, Object> > AL;

AL l1;
AL l2;

VERY commonly done.


By the programmer instead of the compiler.

Computers exist to automate rote tasks.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 4 '06 #384
In article <mj************ *************** *****@4ax.com>,
my************* *************** **@munged.invalid says...

[ ... ]
I wonder if someday we will have a language that lets you write like
Ruby, but that does all kinds of inferencing to tell you additional
info like types, potential bounds etc. but only when you want to see
it.


Yes. It'll be called APL.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 5 '06 #385
In article <1146755237.231 059.87100
@g10g2000cwb.go oglegroups.com> , ro**********@gm ail.com
says...

[ ... ]
Don't know if you meant to imply that C++ works that way but it
doesn't. "public" is not the default inheritance mode, private is.


In C++, private inheritance is the default for classes,
but public inheritance is the default for structs. Lots
of people routinely write:

class X : public Y {
public:
// ...
};

which is equivalent to:

struct X : Y {
// ...
};
Getters and Setters are another good example. Sure, the IDE can generate
them. But C#s properties are a lot more elegant. You can start with
simple public members and introduce getters and setters later without
any need to change the clients of the class.


Getters and Setters are just poor design indicating that perhapse a
class is not the best data type to represent your data or that your
classes are lazy.


Or that your variables aren't really of the correct type
to start with. From what I've seen, the majority of
getters and setters don't really do anything, and are
exactly equivalent to public data with a really ugly
syntax.

Of the (small) minority that really do something, most do
nothing more than enforce the variable being within a
fixed range (e.g. an integer restricted to the range
0..1024). Some languages (e.g. Ada) provide that
capability directly, and exposing the data publicly works
just fine, because the compiler enforces the constraint
without explicit help beyond the definition of the
variable's range.

Other languages (e.g. C++) have the flexibility to allow
the programmer to do the job by defining the correct type
for the variable in question, and enforcing its
constraints explicitly (but still centralizing the
enforcement). For the simple range constraint, for
example, you can write a small template like:

template <class T, T lower, T upper, class less=std::less
<T> >
class bounded {
T val;

static bool check(T const &value) {
return less()(value, lower) ||
less()(upper, value);
}

public:
bounded() : val(lower) { }

bounded(T const &v) {
if (check(v))
throw std::domain_err or("out of range");
val = v;
}

bounded(bounded const &init) : val(init.v) {}

bounded &operator=(T const &v) {
if (check(v))
throw std::domain_err or("Out of Range");
val = v;
return *this;
}

operator T() const { return val; }

friend std::istream &
operator>>(std: :istream &is, bounded &b)
{
T temp;
is >> temp;

if (check(temp))
is.setstate(std ::ios::failbit) ;
b.val = temp;
return is;
}
};

With this, we can make a data member public, keep (even
tighter) encapsulation, and still get nice syntax that's
easy to read and use. At least as it stands right now, I
don't see a way to do this in Java, but Java is close
enough now that I can imagine enough being added at some
point to support it...

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 5 '06 #386
In article <1m************ @sirius.tg00suu s7038.net>,
ew***@sirius.tg 00suus7038.net says...

[ ... ]
However, I have no idea what

{

std::ifstream ifs1("pathname" );
std::ifstream ifs2(ifs1);
doSomething(ifs 1);
doSomething(ifs 2);
}

or

{

std::ifstream ifs1("pathname" );
std::ifstream ifs2("path2");

ifs1 = ifs2;

doSomething(ifs 1);
doSomething(ifs 2);
}


What they do is produce compiler errors -- nothing more
and nothing less. streams cannot be either copied or
assigned. Many years ago (pre-standard) there were
versions of C++ iostreams that included an
iostream_withas sign, but that's ancient history.

You can make two iostreams refer to the same external
file if you wish, but the code isn't anything like the
above, and it leaves little room for question for what it
would mean. The most obvious is simply:

std::ifstream ifs1("pathname" );
std::ifstream ifs2("pathname" );

At least if memory serves, you can also tell two
iostreams to use the same stream buffer. In the iostreams
model, the iostream itself deals primarily with
formatting. The stream buffer is what deals with things
like the storage of the data.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 5 '06 #387
Most of the messages in this thread appear to be written by people who
only know one programming language. Have you not discovered that most
programming languages have their strengths as well as their weaknesses?
I occasionally do some maintenance in F77 and every time I am surprised
at some of the number crunching power that has been built into this
version of Fortran. I am currently writing software to control
equipment in real time and, even though I llike Java a lot, it is not
the appropriate language because of the rapid respnses required to
control equipment. Consequently C++ is more useful. You haggle about
things that are not worth haggling about. Kind regards, Willem Ferguson.

May 5 '06 #388
In article <MP************ ************@ne ws.sunsite.dk>,
Jerry Coffin <jc*****@taeus. com> wrote:

Or that your variables aren't really of the correct type
to start with. From what I've seen, the majority of
getters and setters don't really do anything, and are
exactly equivalent to public data with a really ugly
syntax.


The primary advantage to encapsulating otherwise public data into
accessors that don't actually add anything is that access to this data
can later be easily changed to do something interesting.

It is a poor man's encapsulation, but it's still better than just
having public data fields.

Cheers
Bent D
--
Bent Dalager - bc*@pvv.org - http://www.pvv.org/~bcd
powered by emacs
May 5 '06 #389
> Most of the messages in this thread appear to be written by people who
only know one programming language.


in chronological order:

C64 BASIC
ARM BASIC
ARM Assembler
Motorola Assembler
Pascal
C
Modula 2
Opal (ugly)
VisualBasic
Java.

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
May 5 '06 #390

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
6927
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
5939
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
11188
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
10889
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
10441
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
9606
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...
1
7993
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
6028
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3255
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.