Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Distinguishing in Java and C.

Options
  • 09-05-2006 5:47pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭


    Ok, have my java exam tomorrow and one or two things are annoying me.

    1) Discuss the similarities and differences in syntax between Java and C, in relation to, variables, operators, expressions and statements.

    I'm pretty crappy at C, the vcariables are all teh same in both except for the string variable which is only in java?
    Operators are practically the same?
    That's all I really know about that.
    What are the similarities and differences for those above?


    2) Distinguish between composition and inheritance as used in object-orientated programming.

    Do you need to do code out for this and try to explain the difference?
    Or can you just try to explain it simply?
    All the things I have seen about this are very long. I know yoo can only use inheritance once and would use composition where you need to use multiple inheritances. What is the difference?

    Thanks.


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Discuss the similarities and differences in syntax between Java and C, in relation to, variables, operators, expressions and statements.

    Does C allow operator overloading, does java allow operator overloading.

    How do you declare a variable in C, how do you declare a variable in java.
    in c:        int a;
    in java:    int a = new int() ?
    

    how do you declare arrays in c/java.
    in c:        int array[]
    in java:    int[] array ?
    
    What is a "statement" in c, what is one in java.
    in c:        ends with ';'
    in java:    ends with ';' ?
    


    As for inheritance vs. composition. It's easy. Take a Human as an example.

    class Arm {}
    class Human{}
    class HigherLifeform{}

    A human HAS a left arm. A human HAS a right arm. Therefore when creating your human class, you'd compose it of Arms...
    class Human
    {
       Arm rightarm = new Arm();
       Arm leftarm = new Arm();
    }
    
    But a human IS a higher lifeform, so you'd inherit from the HigherLifeform class...
    class Human inherits HigherLifeform
    {
       Arm rightarm = new Arm();
       Arm leftarm = new Arm();
    }
    

    Composition is used when something "has a". Inheritance is used when something "is a".


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    The other main difference between Java and C is in relation to pointers. Well Java doesn't have them, it has references instead.
    /* C */
    Button *ptr_button = malloc ( sizeof ( Button ) ) ;
    ptr_button->SetCaption ( "OK" ) ;
    
    /* Java */
    Button button = new Button ( "Ok" ) ;
    


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Hey, thanks guys.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    The main difference between Java and C is that Java is an Object Orientated language, while C is not. In Java, you can overload methods (called functions in C). You can have multiple methods with the same name as long as their parameters are different, however you cannot do this in C. In C, function names are global to a project. This can get difficult in big projects such as the Linux Kernel, where every function has to be different. To make this easier you can use something like Hungarian Notation.

    Also, Java byte-code runs on a Java Virtual Machine. In Java you cannot access memory directly, however you can do this in C with pointers as explained above. This is why C is often chosen when write code for embedded devices as the code will be faster. I don't really want to debate this because that's outside the scope of what Tar needs.

    In C, you can create your own types with typedef.

    A C structure is the equivelant to a Java class with no methods. C structs cannot have functions in them.

    If you need me to expand on anything here just let me know.

    EDIT: I just realised you were asking about syntax. What I said is still useful though.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Hey thanks again guys. Used the exact human example even. :)


  • Advertisement
Advertisement