Home > DeveloperSection > Blogs > Comparison of java

Comparison of java


Java Java 
Ratings:
0 Comment(s)
 87  View(s)
Rate this:

Comparison of java syntax with other language


As java and c# is the advance version of C++. So, most of the syntax is same as C++. But there is some variation which we are dealing in this article.

Following are the difference in the syntax for the java:


How to see the output in java?


Java syntax   :        System.out.println("Hello Java")

C# syntax     :        console.writeline(“Hello C#”)      

C++ syntax   :        cout << “Hello C++”

C syntax       :        printf(“Hello”)


How to take input from the user?


Java:

Taking a string input

 

str1=System.console().readLine();

How to take the integer input

int a=Integer.parseInt(System.console().readLine()

C#

Taking a string input

 

String str1 =console.readline();

 

Taking an integer input:

 

Int num1= Convert (ToInt32 (console.readline());

 

 C++

Taking a string input

 

For a single word.

string str1;
cin >> str1;

but for C++ spaces means extraction hence for entire line we need to write following codes.

 

For this we need to declare the standard header.

#include <string>

 

String str1;

getline (cin,mystr);
Converting to INT or Float
Int num1 ;
stringstream(str1) >> num1
float num2;
stringstream(str1) >> num2

Taking integer as an input

Taking a string input

 

int num1;
cin >> num2;
C programming
Char s = getchar();
 
Taking a interger input
Int num1;
scanf(“/d”,&num1);
 
Here is the small program for the taking an input from the user and output
           
import java.io.*;
 
class Demo1 {
 
public static void main(String args[])
{
int a, b,c;
String str1;
System.out.println("Program start") ;
System.out.println("Enter the string");
str1=System.console().readLine();
System.out.println("Enter the 1st number \n");
a=Integer.parseInt(System.console().readLine());//converting char input to int
 
System.out.println("Enter the 2nd number \n")  ;
b=Integer.parseInt(System.console().readLine()); //converting char input to int
c=a+b;
System.out.println("output of string is : "+str1);
System.out.println("output of number is : "+c);
}
}




Array input

           

Java

Declaration for an  array can be done as follows:

 

dataType[] array1;

dataType array1[];

Giving limit to the array, by this syntax

dataType[] array1 = new dataType[arraySize];

 

C#

Declaration for an array can be done as follows:

 

datatype[] arrayName;   

 

Giving limit to the array, by this syntax

dataType[] array1 = new dataType[arraySize];

 

Note : dataType array1[]; (Note : This method is not used in C# it will give compiler error.)

 

C++

Declaration for an array can be done as follows:

datatypetype arrayName [ arraySize ];

 

datatype[] arrayName; (Note : This method is not used in C++ it will give compiler error.)

 

C

Declaration for an array can be done as follows:

datatypetype arrayName [ arraySize ];

 

datatype[] arrayName; (Note : This method is not used in C++ it will give compiler error.)

 

Here is the small program in java to access array list.


import java.io.*;

class Demo2 {

public static void main(String args[]) 
		{
			int[] num1=new int[5];
			int i;
			
			System.out.println("Program start") ;
			for (i=0;i<5;i++)
			{
			System.out.println("Enter the number ");			
			num1[i]=Integer.parseInt(System.console().readLine());
			}
			for (int real : num1 )
			{
			System.out.println(real);			
			}
		}
}




Don't want to miss updates? Please click the below button!

Follow MindStick