views:

134

answers:

3

I'm a C# programmer writing Java (for Android) and have a few technicalities of Java I'm still not sure about, and worried I'm approaching from a C# angle:

  • Are parameters in methods passed in the same manner as C#? (Copied for reference types)
  • Why has the @Override attribute suddenly appeared (I think it's Java 1.5+?)
  • How is possible to compile an application, if you are missing a dependency for one of the libraries you're using?
  • Do I need to worry about using += for large string concatenation (e.g. use a StringBuilder instead)
  • Does Java have operator overloading: should I use equals() or == by default. Basically is object.equals roughly the same as C# (reflection for value types, address reference for reference types)
+9  A: 

Answering in order:

  • Yes, arguments are passed by value in Java, always. That includes references, which are copied (rather than objects being copied). Note that Java doesn't have any equivalent of ref or out
  • @Override allows you to catch typos at compile-time, just like the "override" modifier in C#
  • You compile an application only against the libraries you're using. There's no transitive dependency checking, just like there isn't in .NET.
  • Yes, you should avoid concatenating strings in a loop, just like in .NET. Single-statement concatenation is fine though, just as it is in .NET - and again, the compiler will perform concatenation of constant expressions at compile time, and string literals are interned.
  • No, Java doesn't have user-defined operator overloading. There are no custom value types in Java; == always compares the primitive value or the reference. (If you compare an Object reference and a primitive value, autoboxing/unboxing gets involved, and I can never remember which way round this works. It's a bad idea though, IMO.)
Jon Skeet
The particular issue I had from compiling (which may be Dalvik-related), was my application was missing a reference to javax.activation which a GData library required for its MediaStreamSource classe. The application compiled and ran, but then broke when that class was first used by the GData library.
Chris S
@Chris: Sure... but the same thing can happen in C#. (In fact there was a question about it just this morning... http://stackoverflow.com/questions/3819060/)
Jon Skeet
With a public type, I would've sworn that it would generate a compile error in C#. I haven't had many times I've run into the problem in C# though, I'll have to try it out now.
Chris S
@Chris S: It would generate an error if the type in the dependent library was used in a public API you're using from the intermediate library, I believe. Basically if you use a class, the compiler needs to understand everything in that class.
Jon Skeet
+4  A: 

Are parameters in methods passed in the same manner as C#? (Copied for reference types)

All primative types are copied, all objects are actually pointers to objects, the pointer is copied, but the actual object isn't copied.

Why has the @Override attribute suddenly appeared (I think it's Java 1.5+?)

It hasn't, since Java 1.6 you can also use it to show a method is implementing an interface. The @Override allows you to indicate to the compiler that you think you are overriding a method, the compiler will warn you when you aren't (this is very useful actually, especially if the super class changes)

How is possible to compile an application, if you are missing a dependency for one of the libraries you're using?

I don't think it is.

Do I need to worry about using += for large string concatenation (e.g. use a StringBuilder instead)

In some cases. The java compiler + VM is very good at automatically using StringBuilder for you. However it will not always do this. I wouldn't optimize for this (or anything) beforehand.

Does Java have operator overloading: should I use equals() or == by default. Basically is object.equals roughly the same as C# (reflection for value types, address reference for reference types)

No it doesn't have operator overloading.

Thirler
A: 

About String concatenation I think it depends if you add static strings together or if you concatene variables. With variables it's better to use StringBuilder. :).

So :

String s = "A";
s += "B";
s += "C";

is fine.

But here it's better with StringBuilder

String s = "A";
s += variable_b;
s += variable_c;
ykatchou
Even your first example creates many objects... I mean if the strings are static you should do `s = "A" + "B" + "C"`.
Petar Minchev
@Petar No, use either `s = "A".concat("B").concat("C")` or `s = new StringBuilder().append("A").append("B").append("C").toString()` (although the latter is probably equivalent to what the compiler creates for your code)
seanizer
`String s = "A" + "B" + "C"` creates the bytecode equivalent of `String s = "ABC"` (fastest). The example he gave with `+=` will create a new StringBuilder for each `+=` (a lot slower obviously).
Andrei Fierbinteanu
@seanizer AFAIK the compiler optimizes "A" + "B" + "C" to "ABC" as Andrei said.
Petar Minchev
OK, sorry I did of course mean `s = a.concat(b).concat(c)` where a b and c are not compile time constants. Yes, I know I missed the *static* part
seanizer