Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Going to C# from Java?

Discussion in 'Scripting' started by Cryosphere, Oct 29, 2015.

  1. Cryosphere

    Cryosphere

    Joined:
    Mar 3, 2015
    Posts:
    10
    I've been learning Java for a while, and I want to get into Unity. I've been told that they're quite similar, (being object-oriented) and that a lot of things are the same. However, I'm more worried about what's different. To those who have experience in Java and C#, what should I watch out for in C# that doesn't work the same in Java? I've heard that casting is different/not present in C#. Are there fundamental things, things I'm used to in Java, that work differently in C#?

    Hope someone has insight on this stuff.
     
  2. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    Java is totally different than anything used in Unity. I'm assuming you mean JavaScript which is yet another language. If that is what you mean, UnityScript uses some of the same code as JavaScript but there are enough differences to make it a pain.

    I'm not sure of the differences since I skipped past the UnityScript and went straight to C#. I'm still learning, but then again I guess we all are at different levels. In my experience though (at least for me) once you build an understanding of a programming language and development concepts, applying what you know to learn other languages isn't bad at all. I'd recommend C# over UnityScript though since it is ACTUALLY C# that you are using.
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Cryosphere likes this.
  4. Cryosphere

    Cryosphere

    Joined:
    Mar 3, 2015
    Posts:
    10
    Yeah, I've been using actual Java. I was looking more for specific Java habits that might trip me up transitioning to C#, but I guess boldly going forward is really the only way to learn. Thanks for the links though.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Nigey likes this.
  6. Cryosphere

    Cryosphere

    Joined:
    Mar 3, 2015
    Posts:
    10
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Honestly, C# is a LOT like Java... but with extras added on.

    Ignore the library aspect, they obvsly will have different frameworks of code to work with.

    Syntactically though C# was developed out of Visual J++ (an old discontinued dialect of Java implemented by Microsoft). In the early years it was criticized as being a Java rip off even.

    So really... so much is the same. There's code you could directly copy paste from Java to C# and barely have to change a thing.

    Like packages are namespace in C#. You don't spell out 'extends' but rather just use ':'.

    But you can't go back the other way. C# just adds a LOT more syntax because it doesn't strive to be pure object oriented like Java does. So delegates, anonymous functions, extension functions, events, the fact that all types (including prim types like int) are objects, and so many other things that replace certain design patterns you may be used to using in Java.

    Of course you can still use those design patterns you may use in Java, can still be done in C#. Like you can create callbacks the same way... but why would you when C#'s delegates and events are so easy to use.
     
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    also notice a few defaults have changed, method default to being what would be called final in Java, and require the virtual keyword if you want to override them in a subclass. Also the default protection level is private and not protected like Java
     
  9. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    Personally, I think of C# as a friendlier Java. I'd much rather go from Java to C# than vice versa.

    One of the biggest things to remember is that, in C#, basically everything is an object. In Java, primitive types weren't objects, while types like Strings were, but in C#, an int, a string, a bool, etc. are all objects with helper methods built in, so there's no more encapsulating primitives in class wrappers like was necessary with Java.

    C# also introduces properties, which I love and find very useful. Essentially, they encapsulate the functionality of a Java getter/setter into what appears to be a normal public member.

    So while in Java you'd have something like this:

    private int myInt = 0;
    public int getMyInt() {
    return myInt;
    }
    protected void setMyInt(int value) {
    myInt = value;
    }
    public void foo() {
    setMyInt(20);
    int myIntValue = getMyInt();
    }

    ... in C#, you'd do something like:

    public int MyInt { get; protected set; }
    public void foo() {
    MyInt = 20;
    int myIntValue = MyInt;
    }

    Much easier to use, in my opinion. :) C# is just full of convenient little features like that.
     
    Last edited: Oct 30, 2015
  10. Cryosphere

    Cryosphere

    Joined:
    Mar 3, 2015
    Posts:
    10
    Wow, thanks for all the extra advice people, it's good to know that C# is pretty similar to Java. I've started on a tutorial, and it's helpful just to interpret what the scripts are doing.