Advanced Java Themes for a C # Programmer

I am a C # programmer writing Java (for Android), and I have several technical specifications for Java that I’m still not sure about, and worry that I’m getting close to the C # corner:

  • Are the parameters in the methods passed in the same way as C #? (Copied for reference types)
  • Why did the @Override attribute appear (I think this is Java 1.5+?)
  • How can you compile an application if you lack the dependency for one of the libraries that you use?
  • Do I have to worry about using + = for large string concatenation (e.g. use StringBuilder instead)
  • Does Java have operator overloading: should equals() or == be used by default. Basically this is object.equals about the same as C # (reflection for value types, reference for reference types)
+4
source share
3 answers

Responding to a request:

  • Yes, arguments are passed by value in Java, always. This includes links that are copied (not objects copied). Please note that Java does not have the equivalent of ref or out
  • @Override allows you to capture typos at compile time, just like the override modifier in C #
  • You compile the application only for the libraries you use. There is no transitive dependency check, just like in .NET no.
  • Yes, you should avoid string concatenation in a loop like in .NET. Concatenation with a single statement is excellent, as in .NET, and, again, the compiler will concatenate constant expressions at compile time, and string literals are interned.
  • No, Java does not have a custom operator overload. There are no user-defined value types in Java; == always compares a primitive value or reference. (If you compare the Object reference and a primitive value, autoboxing / unpacking is involved, and I can never remember how this works. It's a bad idea, although IMO.)
+10
source

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

All primitive types are copied, all objects actually point to objects, the pointer is copied, but the actual object is not copied.

Why did the @Override attribute suddenly appear (I think this is Java 1.5+?)

This is not the case since Java 1.6 you can also use it to show that a method implements an interface. @Override allows you to tell the compiler that you think you are overriding a method, the compiler will warn you when you do not (this is very useful, especially if the superclass changes)

How can you compile applications if you lack the dependency for one of the libraries you are using?

I do not think so.

I need to worry about using + = to concatenate large strings (e.g. using StringBuilder)

In some cases. The Java + VM compiler is great for automatically using StringBuilder for you. However, this will not always be so. I will not optimize this (or anything) in advance.

Does Java have operator overloading: equals () or == by default should be used. Basically this object.equals is about the same as C # (reflection for value types, address link for reference types)

No, he has no operator overload.

+4
source

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

So:

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

excellent.

But here is better with StringBuilder

 String s = "A"; s += variable_b; s += variable_c; 
0
source

All Articles