Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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)
like image 266
Chris S Avatar asked Dec 09 '22 13:12

Chris S


2 Answers

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.)
like image 140
Jon Skeet Avatar answered Dec 13 '22 22:12

Jon Skeet


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.

like image 28
Thirler Avatar answered Dec 13 '22 23:12

Thirler