Java : ใน Java เรียก Primitive Data Type มีอยู่ด้วยกัน 8 ตัว byte , short , int , long , float , double , char , boolean
C : ใน C เรียก Primary Data Type มีอยู่ด้วยกัน 4 ตัว ได้แก่ Integer, Floating-point (float), double, string. ทำหน้าที่เหมือนกันกับ data type ใน C# ยกเว้น string
Example :
usingSystem; classVTK { staticpublicvoidMain () { byte a =47; bool b =true; double c =0.42e2;float d =134.45E-2f;decimal e =1.5E6m;;char g ='G';int h =10;long i =15000000000L;Console.WriteLine("The byte of a is: {0}",a);Console.WriteLine("The bool of b is: {0}",b);Console.WriteLine("The double of c is: {0}",c);Console.WriteLine("The float of d is: {0}",d);Console.WriteLine("The decimal of e is: {0}",e);Console.WriteLine("The char of g is: {0}",g);Console.WriteLine("The int of h is: {0}",h); Console.WriteLine("The long of i is: {0}",i); } }
is_true = True
is_false= False
print(is_true, is_false)
a = None
b = 5
print(a , b)
class Main {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag); // prints true
byte bite;
bite= 124;
System.out.println(bite);
short temperature;
temperature = -200;
System.out.println(temperature);
int ins = -4250000;
System.out.println(ins);
long loon = -42332200000L;
System.out.println(loon);
double doub = -42.3;
System.out.println(doub);
float floa= -42.3f;
System.out.println(floa);
char letter = '\u0051';
System.out.println(letter);
}
}
#include <stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
char c = 'b';
printf("The character value is: %c \n", c);
float f = 7.2357;
printf("The float value is: %f \n", f);
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
Output :
The byte of a is: 47
The bool of b is: True
The double of c is: 42
The float of d is: 1.3445
The decimal of e is: 1500000
The char of g is: G
The int of h is: 10
The long of i is: 15000000000
True False
None 5
true
124
-200
-4250000
-42332200000
-42.3
-42.3
Q
The integer value is: 5
The character value is: b
The float value is: 7.235700
The double value is: 71.235745
using System;
namespace MyApplication
{
// Interface
interface IAnimal
{
void animalSound(); // interface method (does not have a body)
}
// Pig "implements" the IAnimal interface
class Pig : IAnimal
{
public void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: wee wee");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
}
}
}
# define a class
class Employee:
# define a property
employee_id = 0
# create two objects of the Employee class
employee1 = Employee()
employee2 = Employee()
# access property using employee1
employee1.employeeID = 1001
print(f"Employee ID: {employee1.employeeID}")
# access properties using employee2
employee2.employeeID = 1002
print(f"Employee ID: {employee2.employeeID}")
import java.io.*;
class Demo {
int x = 10;
int display()
{
System.out.println("x = " + x);
return 0;
}
}
class Main {
public static void main(String[] args)
{
// create instance
Demo D1 = new Demo();
// accessing instance(object) variable
System.out.println(D1.x);
// point 3
// accessing instance(object) method
D1.display();
}
}
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output :
The pig says: wee wee
Employee ID: 1001
Employee ID: 1002
10
x = 10
Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2
using System;
class Geeks {
class Mod
{
// using public modifier
// keyword
public int n1;
}
// Main Method
static void Main(string[] args) {
Mod obj1 = new Mod();
// access to public members
obj1.n1 = 77;
Console.WriteLine("Value of n1: {0}", obj1.n1);
}
}
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
def _display(self):
print("Name:", self._name)
print("Age:", self._age)
class Student(Person):
def __init__(self, name, age, roll_number):
super().__init__(name, age)
self._roll_number = roll_number
def display(self):
self._display()
print("Roll Number:", self._roll_number)
s = Student("John", 20, 123)
s.display()
// abstract class
abstract class Main {
public String fname = "John";
public int age = 24;
public abstract void study(); // abstract method
}
// Subclass (inherit from Main)
class Student extends Main {
public int graduationYear = 2018;
public void study() { // the body of the abstract method is provided here
System.out.println("Studying all day long");
}
}
#include <stdio.h>
int main() {
int a;
// Modify the int using short
short int b;
// Print the size of a.
printf("Size of a: %d", sizeof(a));
// Print the size of b.
printf("\nSize of b: %hd", sizeof(b));
int num;
long int long_num;
long long int long_long_num;
double dub_num;
long double long_dub_num;
printf("\nSize of num: %d", sizeof(num));
printf("\nSize of long_num: %d", sizeof(long_num));
printf("\nSize of long_long_num: %d",
sizeof(long_long_num));
printf("\nSize of dub_num: %d", sizeof(dub_num));
printf("\nSize of long_dub_num: %d",
sizeof(long_dub_num));
int num1 = 4294967295;
unsigned int num2 = 4294967295;
printf("\nnum1: %d", num1);
printf("\nnum2: %u", num2);
int num3 = 248;
signed int num4 = 23124;
printf("\nsize of num3: %d", sizeof(num3));
printf("\nsize of num4: %u", sizeof(num4));
return 0;
}
Output :
Value of n1: 77
Name: John
Age: 20
Roll Number: 123
Name: John
Age: 24
Graduation Year: 2018
Studying all day long
Size of a: 4
Size of b: 2
Size of num: 4
Size of long_num: 8
Size of long_long_num: 8
Size of dub_num: 8
Size of long_dub_num: 16
num1: -1
num2: 4294967295
size of num3: 4
size of num4: 4
C : มีการแบ่ง statements ออกเป็นกลุ่มย่อยหลาย ๆ กลุ่ม ขึ้นอยู่กับ การใช้งาน ดังนี้ if Statement, if-else Statement, Nested if Statement, if-else-if Ladder switch Statement, Conditional Operator, Jump Statements(break, continue, goto, return)
Example :
// C# program to illustrate the statement keywords
using System;
class demoContinue
{
public static void Main()
{
// using for as statement keyword
// GeeksforGeeks is printed only 2 times
// because of continue statement
for(int i = 1; i < 3; i++)
{
// here if and continue are keywords
if(i == 2)
continue;
Console.WriteLine("GeeksforGeeks");
}
}
}
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else {
if (i == 20) {
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
}
}
return 0;
}
C: ใน C method parameter จะต้องประกอบไปด้วย returnTypefunctionName(parameter1, parameter2, ...){}
Example :
// C# program to illustrate the// concept of the named parameters
using System;
public class DNT {
// addstr contain three parameters
public static void addstr(string s1, string s2, string s3)
{
string result = s1 + s2 + s3;
Console.WriteLine("Final string is: " + result);
}
// Main Method
static public void Main()
{
// calling the static method with named
// parameters without any order
addstr(s1: "Dot", s2: "Net", s3: "Tricks");
}
}
class PassValue
{
public static void modifyValue(int no)
{
no = no + 10;
}
public static void main(String args [])
{
int x = 5;
System.out.println("Original value of x: " + x);
modifyValue(x);
System.out.println("Modified value of x: " + x);
int y = 20;
System.out.println("Original value of y: " + y);
modifyValue(y);
System.out.println("Modified value of y: " + y);
int z = -15;
System.out.println("Original value of z: " + z);
modifyValue(z);
System.out.println("Modified value of z: " + z);
int largeNo = 1000;
System.out.println("Original value of largeNo: " + largeNo);
modifyValue(largeNo);
System.out.println("Modified value of largeNo: " + largeNo);
}
}
Original value of x: 5
Modified value of x: 5
Original value of y: 20
Modified value of y: 20
Original value of z: -15
Modified value of z: -15
Original value of largeNo: 1000
Modified value of largeNo: 1000
C : ไม่มี namespace แต่มี scope ที่มีแนวคิดใกล้เคียงกับ namespace
Example :
using System;
using First;
using Second;
namespace First {
public class Hello
{
public void sayHello() { Console.WriteLine("Hello Namespace"); }
}
}
namespace Second
{
public class Welcome
{
public void sayWelcome() { Console.WriteLine("Welcome Namespace"); }
}
}
public class TestNamespace
{
public static void Main()
{
Hello h1 = new Hello();
Welcome w1 = new Welcome();
h1.sayHello();
w1.sayWelcome();
}
}
# global_var is in the global namespace
global_var = 10
def outer_function():
# outer_var is in the local namespace
outer_var = 20
def inner_function():
# inner_var is in the nested local namespace
inner_var = 30
print(inner_var)
print(outer_var)
inner_function()
# print the value of the global variable
print(global_var)
# call the outer function and print local and nested local variables
outer_function()
// In file MainClass.java
package com.mycompany.project;
public class MainClass {
public void display() {
System.out.println("Inside MainClass");
}
}
// In file ModuleClass.java
package com.mycompany.project.module;
import com.mycompany.project.MainClass;
public class ModuleClass {
public void show() {
MainClass mainClass = new MainClass();
mainClass.display();
System.out.println("Inside ModuleClass");
}
}
void myFunction() {
// Local variable that belongs to myFunction
int x = 5;
// Print the variable x
printf("%d", x);
}
int main() {
myFunction();
return 0;
}
C : C มี Operator ที่ค่อนข้างคล้ายกับ Java เพียงแต่ C จะไม่มี Shift Operator และมี Sizeof , Comma, Member Selection Operator เพิ่มเข้ามา
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
Console.WriteLine("sizeof(char) : {0}", sizeof(char));
Console.WriteLine("sizeof(byte) : {0}", sizeof(byte));
Console.WriteLine("sizeof(sbyte) : {0}", sizeof(sbyte));
Console.WriteLine("sizeof(float) : {0}", sizeof(float));
Console.WriteLine("sizeof(ushort) : {0}", sizeof(ushort));
Console.WriteLine("sizeof(double) : {0}", sizeof(double));
Console.WriteLine("sizeof(int) : {0}", sizeof(int));
Console.WriteLine("sizeof(bool) : {0}", sizeof(bool));
Console.WriteLine("sizeof(short) : {0}", sizeof(short));
}
}
}
print(not 5 == 5)
print(1 or 2 and 3)
print(4 or 5 + 10 or 8)
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);
System.out.println(~b);
System.out.println(!c);
System.out.println(!d);
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
System.out.println(10*10/5+3-1*4/2);
}
}
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
Java : มีการ Conversion 4 รูปแบบ - Widening (Automatic), Narrowing (Explicit), Type Promotion in Expression และ Explicit Type Casting in Expressions
C : C มีรูปแบบการ Conversion อยู่ 2 รูปแบบ เช่นเดียวกันกับ Python คือ Implicit และ Explicit Conversion
Example :
using System;
namespace MyApplication {
class Program {
static void Main(string[] args) {
int numInt = 500;
// get type of numInt
Type n = numInt.GetType();
// Implicit Conversion
double numDouble = numInt;
// get type of numDouble
Type n1 = numDouble.GetType();
// Value before conversion
Console.WriteLine("numInt value: "+numInt);
Console.WriteLine("numInt Type: " + n);
// Value after conversion
Console.WriteLine("numDouble value: "+numDouble);
Console.WriteLine("numDouble Type: " + n1);
Console.ReadLine();
}
}
}
num_string = '12'
num_integer = 23
print("Data type of num_string before Type Casting:",type(num_string))
# explicit type conversion
num_string = int(num_string)
print("Data type of num_string after Type Casting:",type(num_string))
num_sum = num_integer + num_string
print("Sum:",num_sum)
print("Data type of num_sum:",type(num_sum))
// Java Program to Illustrate Conversion of
// Integer and Double to Byte
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Declaring byte variable
byte b;
// Declaring and initializing integer and double
int i = 257;
double d = 323.142;
// Display message
System.out.println("Conversion of int to byte.");
// i % 256
b = (byte)i;
// Print commands
System.out.println("i = " + i + " b = " + b);
System.out.println(
"\nConversion of double to byte.");
// d % 256
b = (byte)d;
// Print commands
System.out.println("d = " + d + " b= " + b);
}
}
#include <stdio.h>
int main() {
// Automatic conversion: float to int
int myInt = 9.99;
printf("%d", myInt);
float sum = 5 / 2;
printf("\n%f", sum);
// Manual conversion: int to float
float sun = (float) 5 / 2;
printf("\n%f", sun);
int num1 = 5;
int num2 = 2;
float sue = (float) num1 / num2;
printf("\n%.1f", sue);
return 0;
}
Value: 124.23
Data Type: <class 'float'>
Data type of num_string before Type Casting: <class 'str'>
Data type of num_string after Type Casting: <class 'int'>
Sum: 35
Data type of num_sum: <class 'int'>
Conversion of int to byte.
i = 257 b = 1
Conversion of double to byte.
d = 323.142 b= 67
C++ : ใน C++ มี Keyword ที่ใช้ในการ Access เช่นเดียวกันกับ Java เพียงแต่จะไม่มีตัว default เท่านั้น
Example :
using System;
public class Animal{
public string color = "white";
}
public class Dog: Animal
{
string color = "black";
public void showColor()
{
Console.WriteLine(base.color);
Console.WriteLine(color);
}
}
public class TestBase
{
public static void Main()
{
Dog d = new Dog();
d.showColor();
}
}
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name,float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Sonoo", 890000f);
Employee e2 = new Employee(102, "Mahesh", 490000f);
e1.display();
e2.display();
}
}
class Geek:
# constructor
def __init__(self, name, age):
# public data members
self.geekName = name
self.geekAge = age
# public member function
def displayAge(self):
# accessing public data member
print("Age: ", self.geekAge)
# creating object of the class
obj = Geek("R2J", 20)
# finding all the fields and methods which are present inside obj
print("List of fields and methods inside obj:", dir(obj))
# accessing public data member
print("Name:", obj.geekName)
# calling public member function of the class
obj.displayAge()
class Data {
private String name;
// getter method
public String getName() {
return this.name;
}
// setter method
public void setName(String name) {
this.name= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();
// access the private variable using the getter and setter
d.setName("Programiz");
System.out.println(d.getName());
}
}
#include<iostream>
using namespace std;
// class definition
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public datamember outside class
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}
using System;
class Geeks{
// Main method
public static void Main(String[] args)
{
// decimal-form literal
int a = 101;
// Hexa-decimal form literal
int c = 0xFace;
// binary-form literal
int x = 0b101;
Console.WriteLine(a);
Console.WriteLine(c);
Console.WriteLine(x);
}
}
x = 0b10100 #Binary Literals
y = 100 #Decimal Literal
z = 0o215 #Octal Literal
u = 0x12d #Hexadecimal Literal
#Float Literal
float_1 = 100.5
float_2 = 1.5e2
#Complex Literal
a = 5+3.14j
print(x, y, z, u)
print(float_1, float_2)
print(a, a.imag, a.real)
public class Test {
public static void main(String[] args)
{
// single character literal within single quote
char ch = 'a';
// It is an Integer literal with octal form
char b = 0789;
// Unicode representation
char c = '\u0061';
System.out.println(ch);
System.out.println(b);
System.out.println(c);
// Escape character literal
System.out.println("\" is a symbol");
}
}