Java, C++, C# and PHP. The Differences.

Many of the students that come to learn in my courses already know other programming languages. In order to assist my students I chose to create a summery for all the differences between the four main programming languages I teach. If there is a topic in which a comparison is required please don’t hesitate and let me know.

Support for Structs
While C++ and C# allow us to define structs and instantiate them, Java and PHP donot allow it. In Java and PHP creating new objects is feasible by instantiating a class only. In C++ and C# new objects can be instantiated both from classes and from structs.

Classes Type Variables
When creating a class type variable in PHP, C# and Java, it is just a variable that can hold a reference for a specific object. When creating a class type variable in C++ it is already an object. In the following code, rec is an object (in C++). In the other three programming languages rec can hold a reference for an object.

Arrays
Unlike C#, C++ and Java, when creating an array in PHP we get an array of elements. Each element is a key value pair. The keys can be either of the type int or string. The values can be of any type. In PHP the array is value type. In C++, Java and C# the array is a reference type. When using an array in PHP it automatically grows when needed. In C++, C# and Java the array cannot change its size.

Memory Access
PHP and Java don’t provide us with the capability to access specific addresses. C# and C++ allow us to access specific addresses (using pointers).

Memory Management
PHP, Java and C# provide us with the garbage collector that takes care after the release of un-necessary areas in the memory. C++ doesn’t. When writing code in C++ we should pay attention to the need to release un-nucessary areas of objects we no longer need them.

Polymorphism
In PHP and Java all functions are virtual by default. This assist us with the implementation of polymorphism in our code. The functions in C++ and C# are not virtual by default. It is up to us to decide whether to turn them into virtual ones or not.

Functions Overloading
Java, C++ and C# allow us to overload our functions with others. They allow us to define the very same function more than one. Each time with a different signature. PHP doesn’t.

Functions Overriding
Java, C++, C# and PHP allow us to override a function. In C# we must add the override keyword to the definition of the new function that overrides the existing one. In Java we can add the @Override annotation as a replacement for the override keyword. C++ and PHP don’t require us to add any specific keyword when overriding a function.

Operators Overloading
C++ and C# allow us to overload operators. Java and PHP don’t allow us to overload operators. The only operator that already has more than one meaning is +. When applying it on two numbers we will get their sum. When applying it on two strings or a string together with a value of another type we will get a concatenation of the two values.

Inner Classes
PHP doesn’t allow us to define inner classes. C++ and C# allow us to define inner classes. However, their meaning is far from the one we have in Java.

Lambda Expressions
C++ doesn’t allow us to define lambda expressions. C# and Java do allow us to define lambda expressions. However, lambda expression in Java is just a simple shortcut for defining an anonymous inner class. PHP doesn’t allow us to define lambda expressions.

Inheritance
Java, C# and PHP don’t allow us to define a class that extend more than one class. C++ does allow us to do it. When defining a class that extends another class in PHP and in Java we should use the ‘extends’ keyword. When defining a class that extends another class in C++ and in C# we should place the column ‘:’ before the name of the class from which our class extends.

Interfaces
Java, C# and PHP allow us to define interfaces. Class can implements as many interfaces as we want. C++ doesn’t allow us to define interfaces. Java allows us to define interfaces with default implementation for some of its methods. C# and PHP don’t allow us doing so. The syntax for defining a class that implements an interface in Java and in PHP involves with using the ‘implements’ keyword. In C# we will use the column ‘:’ character for showing that a class implements an interface.

Traits
PHP allows us to define traits. Java, C# and C++ don’t. Nevertheless, Java allows us to define a trait with default implementations for one (or more) of its methods.

Constants
In order to create a class member as a constant one in PHP, C++ and C# we will use the ‘const’ keyword. In Java we will use the ‘final’ keyword.

The Main Function
In PHP there is no such thing a main function. In C++ we will have a global main() functions as the entry point of our program. In Java and C# we will have a static main function (in C# we will call it ‘Main’). In Java its signature must be a returned value of the type void and one parameter its type is array of strings. In C# its signature doesn’t have to be with a returned value of the type void. It can also be with a returned value of the type in. In addition, in C# it doesn’t have to be with parameters.

Final Functions
In PHP, C++ and Java we will use the ‘final’ keyword in order to mark those functions we want to prevent others from overriding them. In C# we will do the same using the ‘sealed’ keyword.

Program Modularity
In PHP we can have our program composed from PHAR files. In Java we can do the same with JAR files. In C# and in C++ we can split our program into DLL files.

Namespaces
In PHP, C++ and C# we can define namespaces and have our classes (as well as the global functions, in the case of C++ and PHP) allocated to separated namespaces. In Java we can achieve the same with packages.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences