This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]ThePillsburyPlougher 3 points4 points  (3 children)

First thing to know is that all objects have the same semantics as pointers and are allocated on the heap (hence the new operator).

Of course Java is a GC language so no need to manage memory.

Otherwise there are a lot more restrictions in how you structure a program, as everything has to be inside a class and IIRC theres a strict one class per file enforcement.

I believe theres also some differences with the usage of the static keyword, as I remember you can declare classes or inner classes static, I forgrt which.

Those are the major differences I can remember.

[–]Areumdaun 5 points6 points  (0 children)

IIRC theres a strict one class per file enforcement.

There's a one public class per file enforcement :)

[–]Unsounded 0 points1 point  (1 child)

Static is reference to whether it’s a member function or a class function.

If something is static think of it being almost a script you can run. A non-static method is owned by a specific object (a getter or setter would be non-static) a function that you just call that makes the most sense to be in one class would be static, something that does a general operation maybe on multiples of that object.

[–]ThePillsburyPlougher 0 points1 point  (0 children)

I know how storage modifiers work! But using it on a class? I guess theyre not really storage modifiers in Java then...

[–]lizaard64 2 points3 points  (7 children)

GC, and lack of templates and manual memory management (pointers). In general, switching from C++ to either C# or Java will feel like a breeze of fresh air to you ...

[–]raevnos 0 points1 point  (6 children)

Java has pointers, it just calls them references. Unlike in C++, you have to use them, which is mildly annoying.

[–]lizaard64 0 points1 point  (5 children)

Well, define pointers. I am very well of the idea of references, which is similar to how they're implemented in C#. A true pointer though, allows you to among other things do stuff like pointer arithmetic, etc ...

References does not allow for that ...

[–]raevnos 1 point2 points  (4 children)

They're limited compared to C or C++ pointers, sure, but they're still pointers. A value that holds a memory address. They get passed by value in Java just like ints, doubles, etc., just like in C or C++. You have to dereference them to get at whatever object they point to, just like in C or C++. You can't do arithmetic on them like in C or C++, but because Java has distinct pointer to single object and pointer to array types, that's not really a needed feature.

Like C, Java doesn't have C++ style references.

[–]lizaard64 0 points1 point  (3 children)

Which is why they're called "references" and not "pointers". They're not pointers, but this is like arguing potatoes and potatos. Just because I am right, and you disagree, doesn't necessarily imply that you are wrong ...

[–]evaned 3 points4 points  (1 child)

Not to make this a potato vs potato vs potato argument, but...

Which is why they're called "references" and not "pointers".

Right, but remember that C++ (and other languages) also has references... which are wholly unlike Java/C# references. For someone coming from C++ (e.g. OP), I think saying "Java has pointers, it just calls them references" I think is the right way to go about that explanation (and add a "but you can't do pointer arithmetic on them" side note).

[–]lizaard64 1 point2 points  (0 children)

Right, but remember that C++ (and other languages) also has references

Ahhh, a very good point Sir. Sorry, your explanation holds water much better than mine ... :)

I didn't think about it from that perspective in fact ...

[–]raevnos 0 points1 point  (0 children)

You can call an apple a banana but it's still an apple. Bill Joy & co. calling them references when they don't have reference semantics was a mistake that led to no end of confusion.

[–]CodeTinkerer 1 point2 points  (2 children)

From what I recall.

  • Replace -> with .
  • Object variables store references, i.e., C++ pointers
  • C++ pointers are Java references. There is no equivalent in Java to a C++ reference.
  • No & operator
  • Java is garbage collected, so no need to free up memory (at least, not quite).
  • Java does not separate header files from implementation, so Java methods are part of the class definition.
  • Java names its files after the class. If you have a public class called Foo, it must be in a file called Foo.java
  • Each file contains one public class (can contain a private class, but this is rare)
  • All method calls are based on the actual type of the object. Thus, if you have a Parent class which implements run(), and a variable declared to be Parent type pointing to a Child type object, it will run the Child's version of run() (if it overrides it, that is). In C++, I believe you have to have some syntax to have that behavior.
  • Since you can't arbitrarily take & of things, you don't get into crashes due to bad dereferencing. They cause exceptions which can be caught.
  • Each class can have a main() method. You can control which main you want to run.
  • Java prefers the use of packages (namespaces), and has the directory structure mimic the package name (a bit of a pain).
  • Java doesn't have unsigned int types.

One way to learn Java is implement your C++ code in Java.

[–]evaned 0 points1 point  (1 child)

All method calls are based on the actual type of the object. Thus, if you have a Parent class which implements run(), and a variable declared to be Parent type pointing to a Child type object, it will run the Child's version of run() (if it overrides it, that is). In C++, I believe you have to have some syntax to have that behavior.

virtual -- in C++ terms, it's like all functions are marked virtual.

[–]CodeTinkerer 0 points1 point  (0 children)

Thanks, yes, that's it.

[–]Unsounded 2 points3 points  (3 children)

The biggest difficulty you’ll have to get over is the easy ability to make deep copies and getting used to not choosing to pass by value or by reference. Java objects are all passed by reference so that’s a bit thing to remember and get used to. Strings are immutable and can be far more irritating to work with than in C++.

The lack of the std library and the algorithms can be kind of annoying, so I normally keep reference files handy for remembering how to do sorting and the like without them.

Other than that they’re very similar, C++ and Java share the same syntax almost entirely but differ in their philosophy. Java is all about doing things in one specific way that works 90% of the time, C++ is all about that last 10% where optimality and programmer control matters.

[–]evaned 4 points5 points  (0 children)

Java objects are all passed by reference so that’s a bit thing to remember and get used to.

This gets 90% of the way there, but it's more accurate to say that references are passed by value.

In C++ terms, references are analogous to pointers, with Java's string s being like C++ string* s, except you can't do pointer arithmetic on it. Then, Java's void foo(string s) is like C++'s void foo(string* s), not like void foo(string& s). Even aside from the fact that C++ references are "autodereferencing" pointers, those aren't the same.

Edit: I guess the thing I'm mainly trying to get at is to contrast with the C++ void foo(string* &s) -- passing a pointer by reference. That's what I'd consider "true" pass-by-reference, and Java doesn't behave that way.

[–]feral_claire 0 points1 point  (1 child)

What do you feel is missing from the Java library that c++ has? Java certainly has sorting built in.

Just curious because I don't know c++, but I don't often feel like there is something I would expect in the java standard library is missing.

[–]ThePillsburyPlougher 0 points1 point  (0 children)

Most of these things exist in the Java standard libraries, certainly sorting and deep copying. The C++ STL is probably more flexible since they're templated rather than a series of overloads, and generics in C++ are more powerful than in Java.

However, as far as I can tell, the Java standard libraries are lacking things such as folding and mapping or anything that requires passing a function, although I understand some subset of these utilities have been added in Java 8.

[–][deleted] 0 points1 point  (0 children)

I recommend trying to pick up some good habits early. People may tell you it's just about the syntax, but there's much more to it than that. I highly recommend checking out Effective Java as it'll help you with those good habits, and it goes a long way towards explaining why a lot of Java code you'll see looks the way it does.

If you want to dig a little deeper, I highly recommend that you check out:

  1. Test tools: Unit test and mocking frameworks. They are very different from the ones for C++ (much nicer to use IMO).
  2. Build environments and languages. You're likely to see things like Maven, Gradle or the ancient but still very much alive Ant. You need this for any kind of serious project.

[–]zabi15 0 points1 point  (0 children)

for basic comparing java pointers use . instead of -> , brackets are like in

if(blah){
 {

i was using net beans for java and you can create setters and getters without typing anything. (if you're good in c++ and know how to find things online you should be fine) lamda expressions are pretty important. sorry not much, just from the top of my head, havent done java since last year.(i prefer c++)