Chris S
Chris S

Reputation: 65436

Advanced Java topics for a C# programmer

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:

Upvotes: 4

Views: 302

Answers (3)

ykatchou
ykatchou

Reputation: 3727

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;

Upvotes: 0

Thirler
Thirler

Reputation: 20760

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.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1501163

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.)

Upvotes: 10

Related Questions