Comparing Java and C/C++ for Data Structures

In summary, both Java and C/C++ are equally good for data structures as they both have bitwise logical operators and support for templates/generics. However, learning C++ may be beneficial for understanding how the computer works at a low level and for achieving optimal speed in certain applications, such as computer games. Flat memory refers to a memory model where memory is accessed as a flat array, while kernel memory refers to the memory specifically used by the operating system. In the past, memory models were segmented and not protected, but modern systems have a protected memory model to prevent programs from accessing each other's memory.
  • #1
pairofstrings
411
7
Which is better Java or C/C++ with data structures and why?
 
Technology news on Phys.org
  • #2
pairofstrings said:
Which is better Java or C/C++ with data structures and why?

Java and C/C++ let you defined data down to the bit level.

Both have bitwise logical operators. Since this is the case you can extract and modify individual bits of larger data words, which means that both are just as good as one another to modifying data in any way you want to do so.

If Java didn't have the logical operators, I would have said C/C++ but since both have these features, you can do literally whatever you want with data retrieval and modification with ease.

Upon searching for template support in Java, I have found that Java added support for templates (they are called Generics). I was going to say C++ had an edge (it's been many many years since I've worked with the original Java), but as a result, again there is no difference between the two.

Based on the above, I can't really see any advantage in terms of data structures for using C++ or Java, but there are other reasons why you may want to use Java or C++ depending on what you are trying to do.
 
  • #3
It depends on what you want to do. However, C++ was the first programming language I learned. At my University, Computer Science and Engineering students would start with C++. The first day of lecture, my professor said to learn C++ before Java. I don't recall the reasons why.
 
  • #4
Ivan92 said:
It depends on what you want to do. However, C++ was the first programming language I learned. At my University, Computer Science and Engineering students would start with C++. The first day of lecture, my professor said to learn C++ before Java. I don't recall the reasons why.

Learning C++ for Java is good for different reasons. One is that you understand what is going on. In C/C++ you have to deal with managing memory and working with pointers. This is good because it corresponds more or less with what is happening inside the computer (flat memory in your RAM that is not kernel memory).

That aspect alone helps you out immensely.

The other is speed. Although Java Runtime Environments are getting faster, sometimes you really have a need for speed. Java is compiled to an intermediate interpreted byte code representation which has to be executed by the Java Runtime. In C++ you compile your code for the platform in the most optimal way possible. So a windows EXE, DLL, and so on will have your windows program headers and data and then the machine code that is your program (in a basic sense).

Some things have a need for speed and are also huge in terms source code, complexity, and demand for resources. One area that this is true is computer games. Computer games have to have real-time performance. Gamers get angry when they are running their games on 20 frames per second.

In the above case, you need good compilers, optimal code, or both.

Now I know there are environments that use a mix of interpreted and optimal compiled code (most modern games use this kind of platform), but again the whole code base is not interpreted.
 
  • #5
chiro said:
Learning C++ for Java is good for different reasons. One is that you understand what is going on. In C/C++ you have to deal with managing memory and working with pointers. This is good because it corresponds more or less with what is happening inside the computer (flat memory in your RAM that is not kernel memory).

What is flat memory in RAM and Kernel memory?
 
  • #6
pairofstrings said:
What is flat memory in RAM and Kernel memory?

Flat memory is a memory model. It's basically a huge array of memory.

In the old days of DOS the memory model was segmented. In the flat model, you just use a word as a pointer. So basically your address would something like 0x3625371A (just an example), or just a machine word.

In the segmented model your memory was referenced using the segment:address model. In the DOS days if you wanted to get access to the screen buffer you would have to write to the address A000:0000. So to do stuff you put A000 in the segment register and 0000 in the index register, instead of just using one word to point to something.

Also back in the DOS days, everything was free gain for the programs. You could basically read and write anything in memory.

As windows became popular, the protected memory model became standard. Protected memory just means that memory is protected by other programs. We take it for granted now that programs can't access other programs memory, but back in time, this wasn't the case.

Kernel memory just refers to the memory specifically used by the kernel (think Operating System). Back in the DOS days, people could write their own device drivers for their programs and access anything. Typically what the DOS games had to do is write keyboard, sound, and graphics interrupt routines (think device driver) so that it would be smooth and responsive.

When windows 95 and above came along, the kernel memory was protected from other programs so that you couldn't do stuff like this. What happens is that the kernel provides an interface to get information on hardware and you can't just read or write to ports at will.

So yeah basically when you hear kernel think operating system specific stuff at a low level in terms of device drivers and device memory and so forth.
 

1. What are the main differences between Java and C/C++ when it comes to data structures?

Java and C/C++ both have similar data structures such as arrays, linked lists, and trees. However, Java has built-in data structures in its standard library while C/C++ require manual implementation. Java also has automatic memory management through garbage collection while C/C++ requires manual memory allocation and deallocation.

2. Which language is better for data structure manipulation, Java or C/C++?

This depends on the specific needs of the project. Java is generally considered to be easier to learn and use, making it a better choice for beginners or projects with strict deadlines. However, C/C++ offers more control and efficiency, making it a better choice for performance-sensitive projects.

3. Can you use the same data structures in both Java and C/C++?

Yes, you can use the same data structures in both languages. However, the implementation may differ as Java has built-in data structures while C/C++ requires manual implementation.

4. Are there any significant performance differences between Java and C/C++ for data structures?

In general, C/C++ is considered to be faster than Java due to its manual memory management and direct access to hardware. However, the performance differences may not be significant for most applications and can vary depending on the specific implementation.

5. Which language is more suitable for handling large datasets and complex data structures?

Both Java and C/C++ can handle large datasets and complex data structures. However, Java's automatic memory management may make it more suitable for handling large datasets as it helps prevent memory leaks and other memory-related errors. C/C++ may be better for handling complex data structures that require low-level control and optimization.

Similar threads

  • Programming and Computer Science
Replies
2
Views
839
  • Programming and Computer Science
Replies
7
Views
681
  • Programming and Computer Science
Replies
2
Views
623
  • Programming and Computer Science
Replies
1
Views
649
  • Programming and Computer Science
2
Replies
39
Views
5K
  • Programming and Computer Science
Replies
1
Views
975
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
22
Views
1K
Back
Top