You are on page 1of 23

Comparative Study

Language Fundamentals

LEVEL – LEARNER
Icon Used

Hands on Exercise Reference Questions Summary

Check Your
Test Your Understanding Overview Objective Understanding

2
Overview

Hi guys !!
You are already aware of the language fundamentals
(Use Primitive Data Types Define, declare & initialize
variables ,Identify Literals, Convert & Cast Primitives
and Use Operators) in your Primary Track (c#/Java).

This session provides and insight towards


fundamentals of the various modern programming
languages like java and c#.

3
Objective

After completing this session, participants will be


able to:
• Understand keywords supported in JAVA
& C#.NET
• Understand the similarities and
dissimilarities between JAVA and C#.NET
supported keywords

4
Check Your Understanding

Hey guys!!!
Let me check your understanding on Keywords that you learnt in your
primary language (Java/C#).

• What is Keyword?
• Why it is being used?
• What is the function of enum key
word?

5
Just a Minute

1. Which of the following statements is a custom data type in c# ?


a. Enum .
b. Putchar .
c. float.
d. int.
2. For a variable of Reference type it stores the address in:
a. Memory Heap.
b. Stack.
c. RAM.
d. None.
3. What does the meaning of var keyword used in the Implicit
local variables?
a. It instructs the compiler to infer the type of variable.
b. Insufficient Memory allocation.
c. The variable scope in the program.
d. None.

6
Key words

• Keywords are reserved identifiers that are predefined in a language.


• Keywords cannot be used as names for variables, methods and classes.
Common Keywords :

abstract default protected Throw

assert do public try

boolean double return void

break else short float

byte interface static for

case while new if

catch finally int while

7
Comparing Keywords in C# & Java

C# Java C# Example Java Example


keyword Keyword
base super public class Person public class Person {
{ protected String name;
protected string name = "John"; Person()
public virtual void GetInfo() {
{ name ="John";
Console.WriteLine("Name: {0}", name); System.out.println(" Name is
} "+name);
} }
class Employee : Person }
{ public class Employee extends
public string id = “123"; Person
public override void GetInfo() {
{ public String id ="123";
base.GetInfo(); Employee()
Console.WriteLine("Employee ID: {0}", {
id); super();
} System.out.println("From Derived
} Class Name is " + name);
System.out.println("emp id" +id);
}

8
Comparing Keywords in C# & Java

C# Java C# Example Java Example


keyword Keyword
static void Main() public static void main(String
bool boolean { args[]) {
bool b = true; boolean b;
if (b) { b = true;
Console.WriteLine(“Boolean value is If(b)
true"); } {
else System.out.println("b is " + b);
{ }
Console.WriteLine(“Boolean value is
false"); }
}
is instanceof Employee e = new Employee(); Employee e= new Employee();
if (e is Employee ) if(e instanceof Employee)
{ {
//executed System.out.println(" yes..");
} }

9
Comparing Keywords in C# & Java

C# keyword Java Keyword C# Example Java Example


const final public const double Pi = public final  double Pi
3.14159; = 3.14159;

//legal definition //legal definition


sealed final public final class A
public sealed class A
{} {}
//illegal attempt to //illegal attempt to
//subclass - A is //subclass - A is
//sealed //final
public class B: A {} public class B extends A
{}
using System; import
using import package.JavaClass;

private private private void method() private void method()


{ {
} }

10
Comparing Keywords in C# & Java
C# keyword Java Keyword C# Example Java Example
//A is a subclass of B public class A extends B
: extends {}
public class A :B
{}
//A implements I public class MyClass
: implements implements
public class A :I
{} MyInterface1
 {  }

11
Data Types
Data types Java Supports C# Supports
Yes; Yes;
8 bit – byte, 8 bit – sbyte
Signed integers 16 bit – short 16 bit – short
32 bit – int, 32 bit– int
64 bit –long 64 bit – long
Yes;
8 bit - byte
Unsigned integers NA 16 bit – ushort
32 bit – uint
64 bits – ulong
Character char char
Date/time reference type - java.util.Date reference type – System.DateTime
float(IEEE 754 binary32 floating point float (32 bit) float (32 bit)
number)
double(IEEE 754 binary64 floating point double (64 bit) double (64 bit)
number)
Boolean type boolean bool
Strings Immutable reference type, Immutable reference type,
Unicode – java.lang.String Unicode - string
Single, Multi-dimensional, Jagged
Arrays Single & Multi-dimensional Arrays
Enumerated types Yes; reference type (enum) Yes; scalar (enum)

12
Lend a hand
Please answer the questions below :

 Write a program to demonstrate class and object in


C#.NET and the same in Java

13
Lend a hand Solution in C#
//Write a program to demonstrate class and object in C#.NET
Using System; //Commonly used data types are included in system directives
namespace LanguageFundamentals
{
public class Person
{
private string name; // Field is declared as a private variable

public Person() // Constructor assign the value for name


{
name = "unknown";
}
public void SetName(string newName) // Method used to set the
//passing value
{
name = newName;
}
//OutPut
} Person Name: unknown
class TestPerson Person Name: Johan Smith
{
static void Main(string[] args)
{
Person person1 = new Person();
Console.WriteLine(“Person Name:”+person1.name);
person1.SetName("John Smith");
Console.WriteLine(“Person Name:”+person1.name);
14 } }
Lend a hand Solution in Java
//Write a program to demonstrate class and object in Java
import java.io.*; //import all the classes from io packages
public class Person
{
private string name; // Field is declared as a private variable

public Person() // Constructor assign the value for name


{
name = "unknown";
}
public void SetName(string newName) // Method used to set the
{ //passing value

name = newName; //OutPut


}
}
Person Name: unknown
class TestPerson Person Name: Johan Smith
{
static void main(string[] args)
{
Person person1 = new Person();
System.out.println (“Person Name:”+person1.name);
person1.SetName("John Smith");
System.out.println (“Person Name:”+person1.name);
}
}
15
Lend a hand
Please answer the questions below :

What are the differences you have noted between java


and C# code?
What are the keywords you have learnt in Java and .NET?

16
Best Practices for Language fundamentals in C#

• Keyword cannot be used as identifiers in your program unless


they include @ as a prefix.
For example, @if is a valid identifier but if is not because if is a
keyword.
• Define a local variable as close as possible to its first use.
• Do not hardcode numbers. Use constants instead.
• You can assign only a Boolean value to a bool variable .
• Use a single space before and after each operator and
brackets.
• Use Meaningful, descriptive words to name variables. Do not
use abbreviations.

17
Best Practices for Language fundamental in Java

• Variables should be initialized where they are declared and


they should be declared in the smallest scope possible.
• Use a single space before and after each operator and
brackets.
• All keywords are in lowercase letters and incorrect usage
results in compilation errors.
Ex : public void someMethod()
throws SomeException
{
}

• Class variables should never be declared public.


• Negated boolean variable names must be avoided.
boolean isError; // NOT: isNoError
boolean isFound; // NOT: isNotFound
18
Questions

19
Summary
Summarizing the topic in the below following:

 Keywords are predefined.


 Keywords cannot be used as names for
variables, methods and classes.
 It should be noted that some keywords are
context sensitive. For example,
the new keyword in has different meanings
that depend on where it is applied. It is not
used only as a prefix operator creating a new
object on the heap.

20
Source

• http://javatheory.blogspot.in
• http://msdn.microsoft.com/en-us/library/seyhszts.aspx
• http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java

Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and books
listed above. The materials that can be accessed from linked sites are not maintained by Cognizant Academy and
we are not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are
the marks of the respective owner(s).

21
Language fundamentals

You have successfully completed -


Language fundamentals comparative
study
Change Log

Version Changes made


Number
V1.0 Initial Version

V1.1 Slide No.


Changed By Effective Date Changes
Effected
         
         

23

You might also like