Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,218,472 members, 8,038,030 topics. Date: Friday, 27 December 2024 at 05:58 AM

What Makes C++ Better Than Java? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / What Makes C++ Better Than Java? (2123 Views)

What Makes Us The No.1 Web Solutions Development Company In Nigeria / 10 Professions/jobs That Pay Better Than Programming (2) (3) (4)

(1) (Reply) (Go Down)

What Makes C++ Better Than Java? by Nobody: 6:57am On Jun 17, 2016
you would like to know all the factors, advantages, etc. that makes C++ better than Java.

I am a c/c++ expert for 8+ years now based in owerri. I sometimes travel to Ibadan, abuja, Lagos for contract work. I work as backend engineer.


Firstly, I'd be cautious to make a claim that C/C++ is better than Java. There are pros and cons to both sides. C/C++ is basically more powerful to the programmer (since one can break past all sorts of abstractions), and runs natively instead of on a virtual environment (and hence is also platform dependent). A C++ application can manage its own memory in more finer ways than a Java app can. In C++, one can overload operators, access physical addresses and override values there. One can throw and catch any type (...) and do some nasty stuff around all that. On the other hand, in Java, one can sit back and basically just deal with OO abstractions and code them up in one-way, regardless of the underlying platform. Fine-grained memory management is harder. One can play around with GC params for optimizations and get really far this way, but anything more is not really achievable. In C++, you can easily get as deep as you want to. But, also remember, with great power comes great responsibility . An irresponsible Java developer would be less harmful to your software, than an irresponsible C++ developer would be. In that sense, Java is a relatively easier and simpler language to work with, than C++ is. With C++, a lot of responsibility and burden is on the shoulders of the programmer and with Java, a lot of this, is taken care of, by the system. There is also a popular performance myth
with C++ and Java. Since C++ runs natively, it used to run faster in most platforms than Java does, given the VM is basically emulated. However, this is increasingly being rendered moot, as JVMs end up being very powerful, and processor speeds and architectures have evolved a lot over time.


1. Import vs the C preprocessor

import java.lang.String;

#include <string>
using namespace std;

2. Memory Allocation
Java handles memory for you, automatically freeing memory when it is no longer used (garbage collecting it). In C and C++, you must deallocate memory that was allocated:
in c++
int *p_int = new int ;
// use p_int
delete p_int;

in java
int [] values = new int [ 10 ];
values[ 20 ] = 2 ; // error! You get an ArrayIndexOutOfBoundsException

2 Likes

Re: What Makes C++ Better Than Java? by wisemania(m): 9:38am On Jun 17, 2016
That language is very thick.Tried it before though (up to the class level) and it was cool.What really discouraged me is that to create a gui app with c++ is one hell of a pain in the neck.what exactly do you do with c++ as a backend engineer?
Re: What Makes C++ Better Than Java? by Nobody: 2:45pm On Jun 17, 2016
wisemania:
That language is very thick.Tried it before though (up to the class level) and it was cool.What really discouraged me is that to create a gui app with c++ is one hell of a pain in the neck.what exactly do you do with c++ as a backend engineer?

thanks for asking. I Develop of core functionalities within a UMTS/LTE Core Network Involvement in the full product life cycle and also I do research, design, coding, testing and maintenance on a contract based.
Re: What Makes C++ Better Than Java? by KazukiIto(m): 4:18pm On Jun 17, 2016
Please op I need a mentor. I've been with C++ for a while. Care to help?
Re: What Makes C++ Better Than Java? by Nobody: 6:11pm On Jun 17, 2016
Nice post @ OP... Your explanations are well drafted...


However, I'd love a good Java programmer to list some factors that makes Java worth learning over C++....
Re: What Makes C++ Better Than Java? by Fulaman198(m): 7:53pm On Jun 17, 2016
Djangocode:
Nice post @ OP... Your explanations are well drafted...


However, I'd love a good Java programmer to list some factors that makes Java worth learning over C++....

To a new learner, like the OP mentioned, you don't have to worry about de-allocating memory in the heap since the JVM takes care of that for you whereas in C++, with pointers, you have to delete the allocation. C++ is less forgiving than Java for someone who is just starting out. It makes sense though as C++ is an older language and is just an Object-oriented version of C. Java is also based on C as well.

Java is more similar to C#. Both are very similar in object creation and object de-allocation. One thing is that with newer OOP languages is that memory in the heap is often taken care of for you (Swift, Java, C#, etc.).
Re: What Makes C++ Better Than Java? by stack1(m): 11:45pm On Jun 17, 2016
Uhm, not to be off topic, but i thought i should bring it to everyones notice about a new social platform created by a Nigerian that really rivals the big boys in a lot of features... need i say more, head over to www.greenhouze.xyz
Re: What Makes C++ Better Than Java? by Nobody: 7:30am On Jun 18, 2016
Fulaman198:


To a new learner, like the OP mentioned, you don't have to worry about de-allocating memory in the heap since the JVM takes care of that for you whereas in C++, with pointers, you have to delete the allocation. C++ is less forgiving than Java for someone who is just starting out. It makes sense though as C++ is an older language and is just an Object-oriented version of C. Java is also based on C as well.

Java is more similar to C#. Both are very similar in object creation and object de-allocation. One thing is that with newer OOP languages is that memory in the heap is often taken care of for you (Swift, Java, C#, etc.).

Thanks Bro...


So the Memory thing is the major difference between the two..? Are there any other differences, Like speed, ease of learning, how they implement OOP concepts etc

1 Like

Re: What Makes C++ Better Than Java? by Fulaman198(m): 7:49am On Jun 18, 2016
Djangocode:


Thanks Bro...


So the Memory thing is the major difference between the two..? Are there any other differences, Like speed, ease of learning, how they implement OOP concepts etc

When you create a new object in Java, you use the new keyword. Let's say for instance you have a class called Square in Java. You would create an object like this:


Square square1 = new Square(5); //create a new object of type square with a side length of 5


To create an Object in C++ from the class Square, all you would do is this:


Square square1(5); //Create a new square object of type square with a side length of 5


In C++ there is also the concept of Destructor which deletes the object. In Java, the JVM takes care of Objects in the heap for you. In C++ you do it by yourself. Destructors are often created after the constructor in C++ and are represented with a tilde (~)
Re: What Makes C++ Better Than Java? by Nobody: 8:04am On Jun 18, 2016
Fulaman198:


When you create a new object in Java, you use the new keyword. Let's say for instance you have a class called Square in Java. You would create an object like this:


Square square1 = new Square(5); //create a new object of type square with a side length of 5


To create an Object in C++ from the class Square, all you would do is this:


Square square1(5); //Create a new square object of type square with a side length of 5


In C++ there is also the concept of Destructor which deletes the object. In Java, the JVM takes care of Objects in the heap for you. In C++ you do it by yourself. Destructors are often created after the constructor in C++ and are represented with a tilde (~)

Thanks mate...


So Java takes care of some underlying actions for you whereas in C++, the programmer codes all those actions by himself...
Re: What Makes C++ Better Than Java? by Fulaman198(m): 8:12am On Jun 18, 2016
Djangocode:


Thanks mate...


So Java takes care of some underlying actions for you whereas in C++, the programmer codes all those actions by himself...

Correct, Java in fact isn't the only language that does that, Swift created by Apple does it as well as C#. The newer OOP languages seem to be able to take care of underlying actions.
Re: What Makes C++ Better Than Java? by Nobody: 10:10am On Jun 18, 2016
Fulaman198:


Correct, Java in fact isn't the only language that does that, Swift created by Apple does it as well as C#. The newer OOP languages seem to be able to take care of underlying actions.

Thanks for the response...

Now to the Java part, diving into OOP as a newbie programmer can be a daunting task as Java is OOP based.... What advise would you give someone who wants to delve into Java programming??
Re: What Makes C++ Better Than Java? by Fulaman198(m): 4:01pm On Jun 18, 2016
Djangocode:


Thanks for the response...

Now to the Java part, diving into OOP as a newbie programmer can be a daunting task as Java is OOP based.... What advise would you give someone who wants to delve into Java programming??

I would say take it slowly, Java isn't meant to be mastered in a month's time. Review areas they are not strong in and ask questions pertaining to the difficult areas that they don't fully understand.
Re: What Makes C++ Better Than Java? by Nobody: 6:09pm On Jun 18, 2016
Fulaman198:


I would say take it slowly, Java isn't meant to be mastered in a month's time. Review areas they are not strong in and ask questions pertaining to the difficult areas that they don't fully understand.

Thanks for your answers...
Re: What Makes C++ Better Than Java? by mexzony: 1:19am On Jun 19, 2016
The problem I have with c++ is the tendency to drift from concentrating on your main programs logic and start dealing with memory allocation and deallocation especially as one writes complex programs.
Sometimes this can or could (I stand to be corrected) make a programmer struggle to remember where he was in the main programs logic once he has dealt with deallocating some memory.
Basically I feel its a huge distraction as you really don't want to be disturbed especially when that idea on how to solve your program is flowing and you just want to out it down in code.
Cheers
Re: What Makes C++ Better Than Java? by guru01(m): 9:50am On Jun 19, 2016
C++ is a low level language which java is the opposite.
C++ is good for building network app, cmd app, console games, etc
Java app can run on all platforms where JVM is present.
Re: What Makes C++ Better Than Java? by Fulaman198(m): 6:26pm On Jun 19, 2016
guru01:
C++ is a low level language which java is the opposite.
C++ is good for building network app, cmd app, console games, etc
Java app can run on all platforms where JVM is present.

C++ is a high-level language just like Java. Assembly for instance is a low-level language very similar to machine code.

Java is also great (if not better) for building network based apps, command apps, etc.

C++ is better for building drivers (firmware) or different products like Routers, Graphics card drivers, etc.

For console games, I would say that C++ does indeed have the advantage as most games are still built in C/C++
Re: What Makes C++ Better Than Java? by Fulaman198(m): 6:28pm On Jun 19, 2016
mexzony:
The problem I have with c++ is the tendency to drift from concentrating on your main programs logic and start dealing with memory allocation and deallocation especially as one writes complex programs.
Sometimes this can or could (I stand to be corrected) make a programmer struggle to remember where he was in the main programs logic once he has dealt with deallocating some memory.
Basically I feel its a huge distraction as you really don't want to be disturbed especially when that idea on how to solve your program is flowing and you just want to out it down in code.
Cheers

That's true, as aforementioned, C++ is less-forgiving than its sibling languages like Java and C#. You don't have to worry about memory de-allocation in newer OOP languages.

(1) (Reply)

Are There Any Top Programming School(s) In Lagos / File From Torrent Sites Legal Or Illegal? / The Only Thing I Know About Programming Is Its Spelling, How Do I Start?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.