What are Similarities and differences, written in concise form?
Thanks!
Stefano Mtangoo 455 Senior Poster
darkagn 315 Veteran Poster Featured Poster
I'm afraid I'm no C++ guru, but I can probably compare C# and Java:
Some similarities:
- both have system garbage collection (C++ does not - you have to allocate and deallocate memory for objects).
- both have strict compiler conditions which are checked such as array indexes, initialisation of variables before use etc (I believe that C++ only checks syntax, resulting in many run-time errors).
- both have large API's and documentation (Java - http://download.oracle.com/javase/6/docs/api/ , C# - http://msdn.microsoft.com/en-us/library/hfa3fa08.aspx).
- both can perform reflection.
- both have excellent exception handling.
Some differences:
- C# (and C++ for that matter) allows operator overloading, Java does not.
- Java has primitive types such as int, char, double etc. Although C# has a similar syntax, the "primitive types" are actually objects and behave in the same manner in terms of memory usage and referencing.
- In Java, all method parameters are called by value, that is that the values of the parameter variables are copied into method local variables. C# allows call by value by default, but can also pass a parameter by reference, allowing the properties or values of parameters to be mutated. C# also allows an "out" variable, which is a means of returning more than one value from a method.
There are more similarities and differences between the languages. C# and Java are very similar in their syntax, C++ is more like C in syntax. Hopefully my post has given you somewhere to start in your comparison...
ddanbe commented: Good answer! +7
Stefano Mtangoo commented: Well explained! +6
Stefano Mtangoo 455 Senior Poster
Hopefully my post has given you somewhere to start in your comparison...
Very concise and clear!
Thanks. I have another question concerning Java and C#. How do they differ in terms of making a class, defining methods, private/public stuffs and imports? I might have something like this in Java, how is it written in C#? Also do Java rules of packages, file names vs class names apply in C#?
Thanks.
import javax.swing.*; //first type of import
import java.sql.ResultSet; //2nd type of import
class MyFrame extends JFrame{
private String name = "C# the son of Java";
public MyFrame(){
//.......
}
public void printMe(String name){
//.....................
}
private String getName(){
return this.name;
}
}
darkagn 315 Veteran Poster Featured Poster
In C# there is the concept of namespaces, which (roughly) correlates to a package in Java, although in Java the naming convention matches the folder structure of the compiled .jar file whereas the C# namespace does not have to. Similarly, there is no hard and fast naming rule in C# class-file relationships, but most conventions follow this trend for ease of use. One big difference is the way get__ and set__ methods are written in C# - often properties are used instead. Another difference in syntax (but not really behaviour) is the way in which interfaces are implemented or base classes extended.
Your Java example might look something like this in C#:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Common;
namespace Example
{
// the : indicates extends and/or implements
public class MyFrame: Form
{
private string name = "C# the son of Java";
// the : here indicates that the constructor "inherits" the base class constructor
public MyFrame() : base()
{
// ...
}
// C# convention - methods and properties start with capital letter
// instead of Java's camel case of lowercase first letter but capitalises second word, third word etc
public void PrintMe(string name)
{
//...
}
private string GetName()
{
// although this is legal, in C# you would probably use a property (see below)
return this.name;
}
// Property - outside this class use MyFrame.Name to access the name variable.
public string Name
{
get
{
return this.name;
}
private set
{
this.name = value;
}
}
}
}
Hope this example helps.
Stefano Mtangoo 455 Senior Poster
Now I see C# is halfway C++ and Java. The : operator is borrowed from C++ as you can see this below
class JavaLang{
//......
};
class CSharp: public JavaLang{
//......
};
but only I cannot see public/private/protected specifier after :
Now my question is, did they dropped access specifier?
Now I cannot understand your statement One big difference is the way get__ and set__ methods are written in C# - often properties are used instead and I hope you are referring to code below which I don't get it well. would you explain a little bit?
// Property - outside this class use MyFrame.Name to access the name variable.
public string Name
{
get
{
return this.name;
}
private set
{
this.name = value;
}
}
and lastly, can you point me to a good tutorial that can get me up with C# and Mono because I love X-platform?
Thanks again!
Edited by Stefano Mtangoo because: n/a
darkagn 315 Veteran Poster Featured Poster
In Visual Studio, when you add a class file for your project the access identifier is not specified, but you can (and should) add it. If it isn't specified, it defaults to internal access, which is public within the namespace but private outside it. Java actually does something similar when access modifiers aren't explicitly specified - it's called package access and it makes the class/method/variable public throughout the package but private outside it.
In C# we often use properties for our instance variables that require getter and/or setter methods to give a consistent way of accessing a class's underlying data. There isn't really an equivalent in Java, although other languages, such as Delphi, do have properties. Properties are basically shortcuts to accessing member variables, and there are a number of ways in which they can be used. Consider the following code:
public bool Visible
{
get;
set;
}
This is the "pure" form of a property, with public getter and setter method. The equivalent code in Java would be something like:
private bool visible;
public bool isVisible()
{
return this.visible;
}
public void setVisible(bool isVisible)
{
this.visible = isVisible;
}
And in fact, that is exactly what the C# code does behind the scenes, but for readablility and conciseness of code I think you would agree that C# is better.
Another way we can use Properties:
private bool visible;
public bool Visible
{
get
{
return visible;
}
set
{
// value is the key word here, it's special because it is the hidden parameter to the set function
visible = value;
}
}
This code is equivalent to the above example. But here's where the real power of properties comes in...
Imagine we have a form with a button on it called btnSave. We can "open up" the button's properties (for example, Enabled property which defines whether the button can be pressed) to the outside world like so:
public bool SaveEnabled
{
get
{
return btnSave.Enabled;
}
set
{
btnSave.Enabled = value;
}
}
I hope these examples help your understanding of properties.
Stefano Mtangoo 455 Senior Poster
Thanks darkagni.
Though I mark it solved, I would be happy to hear from someone who have done C# and C++
Edited by Stefano Mtangoo because: n/a
darkagn 315 Veteran Poster Featured Poster
It might be worthwhile re-posting your question in the C++ forum, someone there may have the answers you seek...
Stefano Mtangoo 455 Senior Poster
I will consider doing that!
Thanks
mayur_5 0 Newbie Poster
C++ compiled binaries run natively on OS level but Java compiled "bytecode class" files run inside a virtual machine named JVM. Thus, C++ compiled binaries need to be compiled against a particular OS to port but Java compiled classes can be easily ported between different OS's as long as they have JVM. This is a great feature but also has its own downsides since every single feature that Java has needs to be applicable on each OS. Most important lack of feature caused by this is "Raw Sockets".
C++ is a OOP language like Java but it is possible to write non-OOP code with C++, it allows you to do it but Java doesn't. Java forces you to write OOP code.
C++ has pointer arithmetics, Java does not want you to deal with pointers directly. This is because, it is possible to hack OOP logic by using pointers and Java does not want users to do it. Many developer find it as a downside but it makes your code more secure and bug free.
Java has a garbage collector to free unused resources.
Edited by happygeek because: spam removed from post (which itself is a comment to a SEVEN year old thread)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.