Java versus C# An introduction to C# and Visual Studio.

Slides:



Advertisements
Similar presentations
COSC 2006 Data Structures I Instructor: S. Xu URL:
Advertisements

1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Introduction to the C# Programming Language for the VB Programmer.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java vs. C# By Abrar Siddiqui.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
1 C# A brief overview by Jack Senechal and Bryan Powell.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
Classes, Interfaces and Packages
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Modern Programming Tools And Techniques-I
C# for C++ Programmers 1.
Basic Introduction to C#
Java Primer 1: Types, Classes and Operators
Methods Attributes Method Modifiers ‘static’
University of Central Florida COP 3330 Object Oriented Programming
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Starting JavaProgramming
Interface.
Java Programming Language
Conditional Statements
Group Status Project Status.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Java versus C# An introduction to C# and Visual Studio

Main Method Java: It is possible to compile a Java program without a main method, provided that class is used as a super class. However the program will execute only when there is a valid main method in the class. C#: It is not possible to compile a C# source file without a valid main method. If it is absent, then the compiler will show an error message like program ‘filename.exe’ does not have an entry point defined.

Primitives Both languages support a number of built-in types which are copied and passed by value rather than by reference. Java calls these types primitive types, while they are called simple types in C#. The simple/primitive types typically have native support from the underlying processor architecture. C# has a few more primitive types than Java, because it supports unsigned as well as signed integer types, and a decimal type for decimal floating-point calculations. Java lacks the unsigned types. In particular, Java lacks a primitive type for an unsigned byte. The Java byte type is signed, while the C# byte is unsigned and sbyte is signed.

Simple/Primitive Types C# alias.NET CLR typeWidth (bits) Range (approximate) Java typeJava wrapper sbyteSByte8-128 to 127byteByte byteByte80 to 255Not available shortInt to 32767shortShort ushortUInt16160 to 65535Not available intInt ,147,483,648 to 2,147,483,647 intInteger uintUInt32320 to 4,294,967,295Not available longInt ,223,372,036,854,7 75,808 to 9,223,372,036,854,7 75,807 longLong ulongUInt to 18,446,744,073,709, 551,615 Not available floatSingle e38 to e38 floatFloat doubleDouble e 308 to e 308 doubleDouble decimalDecimal128 ±1.0 × 10e−28 to ±7.9 × 10e28 Not available charChar16\u0000 to \uFFFFcharCharacter boolBoolean8false, truebooleanBoolean

Constants Java, defined global constants using public static final. In C#, the static keyword does exist (final does not), but you can define constants like this: public const MyConstant = 101; When the constant needs to be initialized at run-time (for example in a constructor), use the readonly keyword instead of const.

Operators and control statements All operators like Arithmetic, logical, increment and decrement etc available in Java are supported by C#. Moreover control flow statements like if, else, if-else, for, do-while, while etc included with Java are also available in C#. However C# reintroduces the popular GoTo statement found in C++.

Objects Java made everything an object. How Java forced you to make classes for everything. This turns out to be partially true. It is necessary to make classes. However, primitives are not objects. In C#, even what were considered primitives in Java are objects in C#. Absolutely everything descends from System.Object. This choice by the C# team allows for greater consistency and for such things as a cleaner implementation of automatic boxing/unboxing

Inheritance In Java, the keyword extends is used to inherit from a parent class, and the keyword implements is used to inherit an interface. In C#, the distinction does not exist and it’s all done in the method signature. public class MyClass : MyParentClass {... }

Overriding In Java, all methods are by default virtual and you can override them. In C#, all methods are non-virtual. To override a method in the parent class, make sure the method of the parent class is defined as virtual using the virtual keyword. class MyParentClass { public virtual void MyMethod() {... } } In the child class, the method must use the override keyword. class MyChildClass : MyParentClass { public override void MyMethod() {... } }

Hello World HelloWorld Java Program class Hello {public static void main(String args[]) {System.out.println("HelloWorld");}} HelloWorld C# Program class Hello {public static void Main() {Console.WriteLine("Hello world");}}

Additional Differences – cs files In C#, the name of a.cs file does not have to match the name of the class defined in it (though this is still a good idea). A.cs file can contain multiple classes. A class can be split across multiple.cs files if declared partial. – Conventions C# generally uses the same naming conventions as Java, except that in C#, method names are generally capitalized (PascalCase). Typical C# code will put the opening curly brace of a block on a line by itself.

Additional Differences – Main method The args parameter to Main is optional in C# The Main method can return either void or int in C# – String comparisons C# lets you compare strings with == and != (case- sensitive). – Inheritance C# uses “base” where Java uses “super”

For Each and 2 dim Array for-each: for-each loop in C# is slightly different; given int[] array = { 1, 2, 3 }; – Java: for (int i : array)… – C#: foreach (int i in array)… Two-dimensional arrays C# distinguishes between square and jagged arrays: int[,] square = new int[3, 3]; square[0, 0] = 1; int[][] jagged = new int[3][]; jagged[0] = new int[3]; jagged[1] = new int[3]; jagged[2] = new int[3]; jagged[0][0] = 1;

C# includes language support for getters and setters. – Java: Class contains private int number; int getNumber() { return this.number; } int setNumber(int number) { this.number = value; } Client code looks like method calls obj.setNumber(5); System.out.println(obj.getNumber); – C# Class contains private int number; public int Number { get { return this.number; } set { this.number = value; } // value is a magic // name for the value being set } Client code looks like using a field obj.Number = 5; Console.WriteLine(obj.Number);

Parameters C# has mechanisms to allow methods to modify their primitive/value parameters – Specifying a parameter as “ref” says that the method gets the current value of the parameter, and can modify the caller’s copy (“reference semantics”) – Specifying a parameter as “out” allows the method to set the value of the parameter (it cannot look at the initial value of the parameter, and it must set the value before returning) – Example: int a = 5; Modify(ref a); // caller needs to specify ref too Console.WriteLine(a); // prints 6 Change(out a); // caller needs to specify out too Console.WriteLine(a); // prints 10 static void Modify(ref int a) { a = a + 1; } static void Change(out int a) { a = 10; }