Similarities between C/C++ and Java

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

jac50
Posts: 27
Joined: Tue Mar 23, 2010 8:00 am UTC

Similarities between C/C++ and Java

Postby jac50 » Wed Dec 15, 2010 10:34 pm UTC

Hi,

I have learnt C for about half a year now, and wanting to learn Java for a placement i am going on next year.

After reading through a book I have found in the library, the syntax is very similar to C. Only differences i have found so far that it is OOP, and that declaring arrays are slightly different.

Therefore, what im asking is that is there a point in me picking up Java, even though the language is so similar?

Or should I learn another language instead? I was also wanting to learn Python, as ive heard that it can help with your programming style in other languages once python is learnt.

Any comments would be appreciated.

James

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Similarities between C/C++ and Java

Postby TheChewanater » Wed Dec 15, 2010 10:38 pm UTC

Unless a job or class demands it, you can't can't gain much from learning Java. If you like C syntax with OOP, you'll find that C++ is generally less annoying (and more C-ish).

Of course, everyone here is going to recommend Python.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Similarities between C/C++ and Java

Postby Sc4Freak » Wed Dec 15, 2010 10:54 pm UTC

The syntax is similar, but honestly the syntax is the least of your worries. The syntax for a language can be picked up in a couple of days, the hard part is learning how to write idiomatic code. If you learn Java, you'll (probably) be learning OOP along with it, which remains a very common paradigm in software today. I think ultimately the actual languages you know aren't as important as understanding the concepts, so either language is probably fine.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Similarities between C/C++ and Java

Postby Emu* » Thu Dec 16, 2010 1:28 pm UTC

You're gonna spend a long time browsing the JavaDoc to avoid reinventing the wheel. Plenty of good googlable examples in Java though.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

keeperofdakeys
Posts: 658
Joined: Wed Oct 01, 2008 6:04 am UTC

Re: Similarities between C/C++ and Java

Postby keeperofdakeys » Sat Dec 18, 2010 1:28 am UTC

It may not seem to be very different when you haven't done any major coding in both, but you'll soon learn about the differences. For example, you never have to touch pointers in java, just understand that a variable does point to an object. Also, you don't need to de-initialise objects, as the garbage collector will clean them up for you. So you should get around to using it, to really learn the differences.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Similarities between C/C++ and Java

Postby qbg » Sat Dec 18, 2010 5:47 am UTC

Another big advantage of Java is the platform. There are many useful classes that are part of the standard, and a large amount of libraries available. JVMs are also very nice pieces of technology.

User avatar
TNorthover
Posts: 191
Joined: Wed May 06, 2009 7:11 am UTC
Location: Cambridge, UK

Re: Similarities between C/C++ and Java

Postby TNorthover » Sat Dec 18, 2010 3:03 pm UTC

Sc4Freak wrote:The syntax is similar, but honestly the syntax is the least of your worries.

A very good general principle. Everything from lisp to perl has reasonably usable syntaxes, it's the semantics that make a language.

kmatzen
Posts: 214
Joined: Thu Nov 15, 2007 2:55 pm UTC
Location: Ithaca, NY

Re: Similarities between C/C++ and Java

Postby kmatzen » Sun Dec 19, 2010 12:15 am UTC

What are people's thoughts on teaching someone to program with an object-oriented approach in mind before teaching someone how to think through a task procedurally? I think OOP is important, but it seems like when people take their into to programming class in Java, the OOP concepts are forced upon them before they even understand what a loop is. I think of tasks as being first I have to do (A) which provides the inputs for (B) and finally goes to (C) where something comes out in the end.

My one roommate got stuck with an intro Java course for his TA position this semester and the way they teach things seems ass backwards. First you learn what a class is with some misleading analogies, how to subclass them, how to modify them, add methods, etc. THEN you learn recursion and after 14 lectures, you learn loops. How does this make any sense at all? It's not even clear to me that as humans we think about categorization of objects in strict hierarchical ontologies. A simple example of this is when you end up with the dreaded-diamond design in C++. So, first we force students to learn this concept of a strict hierarchy which may or may not be valid. Then we provide faulty analogies with what a class and an instantiation is. We make sure they are sufficiently disorientated. Then we teach them procedural basics, by which time they don't care.

I think Java is a great tool for businesses when they have to hire individuals and need to mitigate any potential damage buggy code might produce. With the people coming out of CS programs I've talked to, there is no way I would trust them to write C or C++ code for me. valgrind, what's that? It's like they just run their code and if it doesn't seg fault, they think it's safe. No static analysis, no runtime analysis, nothing. I don't think it is a great teaching tool, however. I do believe that people who study only native languages and do not implement or study garbage collection in one of those languages are deficient when it comes to common managed language design patterns. Both have their strengths.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Sun Dec 19, 2010 1:20 am UTC

In terms of learning programming languages in a structured manner (e.g. from colleges/universities), I think it should go in this order:

1st course:
The basics of imperative programming (e.g. data types, control structures, functions, recursion) in an easy to use language like Python.

2nd course:
C++ using procedural programming techniques starting with the basics of C++ (stdio, vectors, strings), then going on to pointers and memory, giving a rundown on legacy C libraries, templates, structures and classes, and a brief introduction to inheritance.

3rd course:
Java or C# focusing on Object Oriented programming methods and techniques.

From there you can move onto assembly, pure C, functional programming, web design, etc.

I think OOP should be learned purely because it is used so extensively, not because I think it's the best way to write code (procedural programming with objects when necessary is my preference).
Summum ius, summa iniuria.

User avatar
jendral_hxr
Posts: 44
Joined: Wed Jan 21, 2009 1:57 am UTC
Contact:

Re: Similarities between C/C++ and Java

Postby jendral_hxr » Sun Dec 19, 2010 3:04 am UTC

I'd recommend C++, it's more portable (in my opinion) and robust.

One of several things I hate from Java is that it doesn't do pointer works. There's no such

Code: Select all

((void(*)())screwMe)();
which is to some extent is tersely fun. :D
I wish I weren't so stupid

archeleus
Posts: 240
Joined: Wed Sep 29, 2010 1:49 pm UTC
Location: Valenvaryon
Contact:

Re: Similarities between C/C++ and Java

Postby archeleus » Sun Dec 19, 2010 5:59 am UTC

Well I think that you should learn a language like Perl/Python first because its beginner friendly to a certain extent and you can do a lot of OOP in python anyway. Then probably you should learn C++/C and then Java. I like C and C++ the most because of the pointers and well, it was my first language anyway. Java is okay enough, but every single thing is an object in it. Some function calls can become annoying (eg: System.out.printf(); compared to printf(); in C) and yeah you'll be stuck in the JavaDoc for a long time. Even though almost everything from lists to bigInt's are there in the Java libraries, I still don't like it much. My preference goes something like C, Perl, C++, Python, (anything else that I know except Scheme), Java, Scheme.

Similarities between C++ and Java: a lot. C, not so much.
I write a blog rant here.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Sun Dec 19, 2010 2:29 pm UTC

I personally wouldn't consider Perl to be beginner friendly, and I wouldn't teach it as a required part of a CS course.
Summum ius, summa iniuria.

distractedSofty
Posts: 253
Joined: Tue Jul 06, 2010 5:29 pm UTC

Re: Similarities between C/C++ and Java

Postby distractedSofty » Sun Dec 19, 2010 8:00 pm UTC

I don't think the point of a CS course is to educate you on many languages. A course that teaches Java, C++ and Python, as languages, doesn't seem like a good course to me. Really, I think that a CS course should use a language like Java/Python/C# as it's "base" (ie, the language that it is assumed you know, and is used in units where code is expected or needed for demonstration). That language is what should be taught in the intro unit (I would lean towards Python or C#, since they support lambdas. And it needs to be memory managed: The last thing you want when trying to teach data structures is having your students tied up in chasing down memory leaks or accidental frees). Further languages should be taught only when they teach something interesting: you could teach C or assembly to demonstrate the memory model or other hardware like things, you could teach Scheme (or common Lisp) to introduce functional programming.

Having tutored introductory programming units, I can definitely see the appeal of leaving iteration and recursion for a while(hehe). (Though I'm not sure how long 14 lectures is, my university usually only had 12 a semester) The issue is that for many people, the kind of abstract thinking needed to write good programs does not come easily. Without teaching repetition, you could teach programs as a series of steps, and you could teach message passing as an idiom, and you could teach a whole bunch of the abstract concepts, and still have your students write gobs of useful programs. (Which is also why you'd demonstrate recursion first: if you already know how to call a method, the idea of calling the same method to do a smaller amount of work is an easier leap) Ie, faced with a shittonne of content to teach to inexperienced students, the unit coordinator decided that repetition was non-essential and could be left out for a while. (In the class that first taught me Java, repetition wasn't mentioned for at least 2 weeks)

keeperofdakeys
Posts: 658
Joined: Wed Oct 01, 2008 6:04 am UTC

Re: Similarities between C/C++ and Java

Postby keeperofdakeys » Sun Dec 19, 2010 10:14 pm UTC

distractedSofty wrote:I don't think the point of a CS course is to educate you on many languages. A course that teaches Java, C++ and Python, as languages, doesn't seem like a good course to me. Really, I think that a CS course should use a language like Java/Python/C# as it's "base" (ie, the language that it is assumed you know, and is used in units where code is expected or needed for demonstration). That language is what should be taught in the intro unit (I would lean towards Python or C#, since they support lambdas. And it needs to be memory managed: The last thing you want when trying to teach data structures is having your students tied up in chasing down memory leaks or accidental frees). Further languages should be taught only when they teach something interesting: you could teach C or assembly to demonstrate the memory model or other hardware like things, you could teach Scheme (or common Lisp) to introduce functional programming.

My University is bringing in new cs courses this year, I'll be some of the last to do the previous ones. Whereas mostly everything was in java, the new courses are a little different. In the first semester course, they are going to start with something like javascript, or similar, then jumping into C++ in the second semester.

Interestingly, one of the reasons for the change, was that their industrial partners didn't like the reliance on java in the courses.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Sun Dec 19, 2010 11:05 pm UTC

I don't like Java or C# as a first language, personally, they're just too big, and pretty much force you to explain classes early on. I also think you shouldn't be worried about memory management too early, so C++ isn't really the way to go. Python works well to teach the basics without having to get too far off track, and I also like that it forces you to indent properly. In an intro to C++ class I took, there was someone who kept asking me about why his code wouldn't compile, every time I told him to fix his indenting and the problem was immediately visible.

I also believe that memory management should be taught early on, once you have a grasp of the basics. This is why I think C++ should be taught second. It allows you to teach more advanced topics like memory management and introduce things like objects. I also think C++ is a really important language to learn.

While you could teach object oriented programming in C++ or Python, I think it's easier to teach in Java or C#. This is mainly because they are built primarily for OOP, and I think it's nicer if you have static typing allowing you to have an IDE that supports code completion, and you don't have to worry about memory management like you would in C++ which can distract from the purpose of the course.

I think it's also good because you teach the differences between interpreted, compiled, statically typed, dynamically typed, unmanaged, and managed lanaguages.
Summum ius, summa iniuria.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Similarities between C/C++ and Java

Postby TheChewanater » Sun Dec 19, 2010 11:25 pm UTC

Memory management might not necessarily need to be one of the first thinks they learn, but IMHO it should be taught as soon as data structures are taught. You don't want people going out there not knowing a thing about memory allocation and freeing.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: Similarities between C/C++ and Java

Postby poohat » Mon Dec 20, 2010 9:07 am UTC

distractedSofty wrote:I don't think the point of a CS course is to educate you on many languages. A course that teaches Java, C++ and Python, as languages, doesn't seem like a good course to me. Really, I think that a CS course should use a language like Java/Python/C# as it's "base" (ie, the language that it is assumed you know, and is used in units where code is expected or needed for demonstration). That language is what should be taught in the intro unit (I would lean towards Python or C#, since they support lambdas. And it needs to be memory managed: The last thing you want when trying to teach data structures is having your students tied up in chasing down memory leaks or accidental frees). Further languages should be taught only when they teach something interesting: you could teach C or assembly to demonstrate the memory model or other hardware like things, you could teach Scheme (or common Lisp) to introduce functional programming.)

I partially agree. The point of using different languages in a teaching context that it forces people to learn new paradigms. If a C programmer doesnt know OO, then there is a risk that when he tries to learn C++, he will just continues to write the basic C code he knows without ever really getting to grips with the object system or taking advantage of it. But if someone made him learn Java instead then he'd forced to learn OO because he cant just fall back on procedural design, since Java doesnt support it. Similarly, if you try to teach a python programmer functional programming in his own language, then he'll most likely end up falling back on what he knows rather than actually learning it properly. But if you force him to learn Scheme/Haskell then he has nowhere to hide, and he will have to actually learn proper functional stuff.

In my opinion everyone who does computer programming should know at least one functional language, at least one OO language, and then whatever scripting/general-purpose/prototyping language they feel most comfortable in for general purpose programming. Probably C too

Language evangelism is silly but I really cant think of any reason why everyone shouldnt learn Python as their first language, or why CS courses shouldnt teach it as the main language (with a separate language for learning other paradigms). There are other great general-purpose languages (Lisp) but they dont have the same libraries, or community.

distractedSofty
Posts: 253
Joined: Tue Jul 06, 2010 5:29 pm UTC

Re: Similarities between C/C++ and Java

Postby distractedSofty » Mon Dec 20, 2010 10:11 am UTC

poohat wrote:I partially agree.

Funny, looks like you completely agree :)
poohat wrote:Language evangelism is silly but I really cant think of any reason why everyone shouldnt learn Python as their first language, or why CS courses shouldnt teach it as the main language (with a separate language for learning other paradigms). There are other great general-purpose languages (Lisp) but they dont have the same libraries, or community.

I can think of 2: static typing (could be important for beginners, since without it, the concept of types at all can be hard to grasp), and unretarded block structure(while I think that you should indent consistently, sematic whitespace? ick (and good luck explaining that to people who've never programmed before))

But, like you say, it's much of a muchness what the language is.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Mon Dec 20, 2010 2:03 pm UTC

distractedSofty wrote:I can think of 2: static typing (could be important for beginners, since without it, the concept of types at all can be hard to grasp),


That's pretty much the only hesitation I have about saying that Python is good for a first language.

distractedSofty wrote:and unretarded block structure(while I think that you should indent consistently, sematic whitespace? ick (and good luck explaining that to people who've never programmed before))


I don't get why so many people complain about this. Coding in a C style language without knowing how to indent properly can cause you to spend more time looking for syntax errors than coding. I think for a new programmer, indenting in Python is a pretty easy concept to grasp.
Summum ius, summa iniuria.

distractedSofty
Posts: 253
Joined: Tue Jul 06, 2010 5:29 pm UTC

Re: Similarities between C/C++ and Java

Postby distractedSofty » Mon Dec 20, 2010 5:48 pm UTC

Thesh wrote:
distractedSofty wrote:and unretarded block structure(while I think that you should indent consistently, sematic whitespace? ick (and good luck explaining that to people who've never programmed before))


I don't get why so many people complain about this. Coding in a C style language without knowing how to indent properly can cause you to spend more time looking for syntax errors than coding. I think for a new programmer, indenting in Python is a pretty easy concept to grasp.

Normally, I'd just say "That's a bit weird", and write it off as an idiosyncracy of the language, but, for your beginner unit (and especially if your course uses it for later units: practical data structures, algorithms, image processing, whatever...) you expect that the code will come from many sources: they'll type some, but some will come from the internet when they frustratedly google how to do something, some will come from the textbook where they'll need to transcribe it, some will be copied and pasted out of the lecture slides. I've heard the argument that even with C-style languages, you still need to fix up the indentation after you do something, but that doesn't really fly: in C, the code will still work before you fix the indentation, and more than that, the correct indentation is inferable from the code, instead of part of it.

And having had to explain, on several occasions, why the ; is needed as a statement seperator (do these people never use full stops in their writing?), explaining why code doesn't work unless it lines up seems slightly harder.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Similarities between C/C++ and Java

Postby Emu* » Mon Dec 20, 2010 6:32 pm UTC

he cant just fall back on procedural design, since Java doesnt support it


You can be more or less objectionable in Java but an almost procedural style is possible.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Mon Dec 20, 2010 6:54 pm UTC

distractedSofty wrote:: in C, the code will still work before you fix the indentation, and more than that, the correct indentation is inferable from the code, instead of part of it.

And having had to explain, on several occasions, why the ; is needed as a statement seperator (do these people never use full stops in their writing?), explaining why code doesn't work unless it lines up seems slightly harder.


The main problem I have seen with improper indentation in C is having an extra or missing brace. It's difficult to find unless you properly indent the code. I think the fact that it works a lot of the time makes it much easier to develop bad habits. Really though, I don't think many people have problem learning that scope is determined by braces in C or indentation in Python.

There are pros and cons of both, and I don't really believe that they are enough to really make one noticeably easier to learn than the other.
Summum ius, summa iniuria.

User avatar
jendral_hxr
Posts: 44
Joined: Wed Jan 21, 2009 1:57 am UTC
Contact:

Re: Similarities between C/C++ and Java

Postby jendral_hxr » Mon Dec 20, 2010 10:26 pm UTC

Thesh wrote:The main problem I have seen with improper indentation in C is having an extra or missing brace. It's difficult to find unless you properly indent the code. I think the fact that it works a lot of the time makes it much easier to develop bad habits. Really though, I don't think many people have problem learning that scope is determined by braces in C or indentation in Python.

Then just use some IDEs, with colored braces! :D
I wish I weren't so stupid

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Similarities between C/C++ and Java

Postby Thesh » Mon Dec 20, 2010 10:31 pm UTC

In the C++ class where this became a problem, we were using Visual C++ 6.0 which already had auto-indent and syntax highlighting enabled. According to one person whose code was never properly indented, he had previously taken a Java class in high school.

Granted, the majority of the students didn't have a problem.
Summum ius, summa iniuria.

User avatar
cerbie
Posts: 934
Joined: Sat Jul 05, 2008 5:14 am UTC
Location: USA

Re: Similarities between C/C++ and Java

Postby cerbie » Tue Dec 21, 2010 3:59 pm UTC

distractedSofty wrote:sematic whitespace? ick (and good luck explaining that to people who've never programmed before))
Write said code on paper. Draw rectangular blocks around the code in each indented block. Draw into, out of, and looping arrows from key spots in those blocks (usually left corners). Walk through the code a bit. All done.

It's annoying for some code, but it's not hard to grasp at all.
DSenette: (...) on the whole, even a trained killer cow is kind of stupid.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests