Skip to main content

3 posts tagged with "Java"

View All Tags

Part-1: C++, C#, Java Syntax Differences

· One min read

C++ Main Function

  1. Main function with out any arguments
void main() { 
cout<<"Hello World !!!";
}
  1. Main function with commandline Arguments
int main(int argc, char* argv[]) {
cout<<"Hello World !!!";
return 1;
}

C# Main Function

  1. Main function with out any arguments
using System; 
namespace CSharpSample {
class Program {
static void Main() {
Console.WriteLine("Hello World !!!");
}
}
}
  1. Main Function with commandline Arguments
 using System; 
namespace CSharpSample {
class Program {
static int Main(string[] args) {
Console.WriteLine("Hello World !!!");
return 1;
}
}
}

Java Main function

  1. Main function with out any arguments Not possible Compilation error java.lang.NoSuchMethodError: main Exception in thread "main" Main function with commandline Arguments

  2. Main Function with commandline Arguments

public class Program { 
public static void main(String args[]) {
System.out.println( "Hello, World !!!" );
}
}

Part-2: C++, C#, Java Syntax Differences

· 2 min read

[Part-2: Basic Input/Output] C++, C#, Java Syntax Differences

Basic Input/Output

Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

1. C++

  • Input    - cin>>
  • Output - cout<<
// Print Greetings Program
// Basic Input/Output
void PrintGreeting(char name[])
{
cout<<"Hello " << name <<endl;
}

int main(int argc, char* argv[])
{
char name[100\];

cout<<"Enter Your Name:"; //cout -> Standard Output
cin>>name; //cin -> Standard Input

PrintGreeting(name);

return 1;
}

/* OUT PUT
Enter Your Name:nagvbt
Hello nagvbt
*/

2. C#

  • Input  - Console.WriteLine()
  • Output - Console.ReadLine()
// Print Greetings Program
// Basic Input/Output
namespace CSharpSample
{
class Program
{
static void PrintGreeting(char[] name)
{
Console.WriteLine("Hello "+ new string(name));
}

static int Main(string[] args)
{
char[] name;

Console.WriteLine("Enter Your Name:"); //Console.WriteLine() -> Standard Output
string sName = Console.ReadLine(); //Console.ReadLine() -> Standard Input
name = sName.ToCharArray();

PrintGreeting(name);

return 1;
}
}
}

/* * OUTPUT * *
Enter Your Name:
Nag VBT
Hello Nag VBT
* * * * * * * */

3. Java

Input - System.out.println()

Output - System.in.read()

import java.io.IOException;

class Program
{
public static void PrintGreeting(char[] name)
{
System.out.println("Hello "+ new String(name));
}

public static void main(String[] args)
{
char[] name;

System.out.print("Enter Your Name:"); //System.out.println -> Standard Output

String sName = "";
int tmp;
try
{
while((tmp = System.in.read ()) != 'n') // System.in.read -> Standard Input
{
char c = (char) tmp;
sName = sName + c;
}

}
catch (IOException e)
{
e.printStackTrace();
}

name = sName.toCharArray();
PrintGreeting(name);
}
}

/* OUTPUT
Enter Your Name:Nag VBT
Hello Nag VBT
*/

Part-3: C++, C#, Java Syntax Differences

· One min read

Comments, Methods, Class, objects Syntax Differences

Comments Same for C++, C#, Java

  1. Single line comments  - //
  2. Multi line comments

/ This is a a multiline comment /

Method/Function declarations

Same, except that in C# and in Java, function must always be part of a class, and must prefix with access specifier - public/private/protected

check main function for reference

Class declarations

Same but c# and Java does not require a semicolon after closing bracket '}'

_ C++ _

class myMath {
//Methods
public: int Add(int i, int j)
{
return i + j;
}
};

_ C#/Java _

 class myMath {
//Methods
public int Add(int i, int j)
{
return i + j;
}
}

Object declaration/creation C++

Object creation on Stack

myMath obj; //on stack
int result = obj.Add(1,2);

Object creation on Heap

myMath \*pobj = new myMath(); //on heap
int result = obj->Add(1,2);
delete pobj;

Java/C#

myMath obj = new myMath();
int result = obj.Add(1,2);