Basic Differences in syntax of Java and C#

Introduction

I have seen many programmers who are very good in one specific programming language and want to know the basic difference in syntax on other languages the three languages which I mention above C++, Java and C# all are object-oriented languages and have many common functionalities and syntax.

So if you want to become an expert in all these three languages you should have some basic know, how about any one of them so that this tutorial will be helpful for you and you can easily learn many new things and the small differences between them so let start.

Syntax difference between Java and C#

Header file or libraries:

As we know all these languages need some built in libraries to execute their programs and they must be in the program otherwise the code will not execute so we will start from them.

C++: in c++ we have to use #include keyword with the library name

Like #include “iostream.h”;

JAVA: like in C++ in Java we have to import the library with import keyword

Like import java.util.*;

C#: in c# we have to use libraries using “”using”” keyword

Like using System;

Main Function:

As in all these three languages, there is the main function from where the execution of the program starts and we call all other methods from this function so it’s a little difference in the main function of all these three languages.

C++: void main () { } OR int main () {            return 0 ;}

JAVA: public static void main (String [ ] args) {   }

C#: static void Main () { }

Syntax Similarities:

Operators: Some things like operators =, +, -, \, <= >= % are equal in all these languages.

Conditional statements: if, if else and switch also have the same syntax in all these languages.

Loops: loops like for, while and do while is also having the same syntax in all these languages.

Arithmetic Operations: & || ! Are also having the same meanings in all these three languages.

 

Print values on output screen:

To print the output on the console all these three languages shave different keywords.

C++: cout<< “hello world”;

JAVA: System.out.println(“hello world”);

Short Cut to write System.out.println(); Write, sout and press Tab button.

C#: Console.WriteLine(” hello World”);

 

Concatenate string and variable for output

To display a variable as output in + sign is used within the output brackets, whereas in c++, << Is used after output string.

For a variable: 
int value=10;

C++: cout<<“Value is “<<value;

 

For displaying variable only:
cout<<value;

Java: System.out.println(“Value is “+value);

for displaying variable only:

System.out.println(value);

C#: Console.writeLine(“Value is “+value);

Console.writeLine(value);

Input value of user:

To take input from user C++ and C# have little bit simple, but Java is some complicated method for getting values from user.

C++: cin>>a;

C#: Console.ReadLine();

Java: Java use scanner to read value from user input.

import java.util.Scanner;

Scanner in = new Scanner (System.in);

s = in.nextLine();

First, we have to import until.Scanner library, then has to define a Scanner object and then using that object get value from user using nextLine for string nextInt() for integers and so on.

 

Abstract class:

In C# and Java the method to create an abstract class is so simple, but in c++ we must have one pure virtual function in our class to make it abstract class.

C++: class person{

virtual void func()=0;

}

Java: abstract class person { }

C#: abstract class person { }

Inheritance:

While inheriting a class these languages use different key words.

C++:

class a{ };

class b:a { };

Java:

class a{ }

class b extends a { }

C#:

class a{ }

class b: a{ }

Leave a Reply

Your email address will not be published. Required fields are marked *