Java == C#.Net

Programming in different languages and studios is a matter of course. In my opinion, recruitment tests are the only way to ensure that programming languages and work environments take a backseat to the recruitment process and the focus can set on the talent of the developers. It is possible to get outstanding teams from numerous applicants using suitable procedures (which I know from my own experience).

For now, however, I'm only dealing with some C# + MS Visual Studio .Net and Java + Eclipse. I did a lot of programming in C# and knew that Java is very similar, but when I then programmed something in Java, I quickly realized that it was even more similar than I remembered. Here I want to show a few program excerpts, how they look in Java and in C#, using a small example program. I'll also dive into the studios and what seems more powerful at such a quick glance.

Mainly unnoticed are the possibilities to improve the range of functions by extensions, but I will have a look at a few. The performance, speed and spread is not a topic of this entry.

The example program

To illustrate the lesson, I programmed a work hours program in Java with the following functions:

  1. management of employees
  2. registration of hours
  3. reading employee hours from a file
  4. saving employees' hours

Other properties are taken into account:

  1. file operations are performed in the background, on a separate thread
  2. a file is regularly searched for from which further hours should be entered
  3. there is a cap on daily hours
  4. hours are totaled
  5. dates are only used for the current and last entry

I want to use this program to show how similar programming in C# is.

Libraries and Functions

Thread programming

What was common with Winforms in thread programming is very similar to that in Java and has also remained similar in WPF applications.

So we find the following notation in C# for the same thing in Java to change the display of a user interface from a thread.
For this all necessary libraries of Visual Studio were or were automatically integrated.

public voidSetStatus(String status)
{
SetLabelString(lblStatus, status);
}

2 Verweise
private voidSetLabelString(Label lbl, String str)
{
if (!Dispatcher.CheckAccess())
{
DelegateLabelString d = new DelegateLabelString(SetLabelString);
try { this.Dispatcher.BeginInvoke(d, new object[] { lbl , str }); return; catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Invoke Error: " + ex.ToString()); }
// or without defined delegate (more easily)
try { this.Dispatcher.BeginInvoke(new Action(() => { lbl.Content = str ; })); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Invoke Error: " + ex.ToString()); }
}
else
{
lbl.Content = str ;
}
}
delegate voidDelegateLabelString(Label lbl, string str);

The SetStatus function can be called from other classes to change the display.
You can also see the small lettering 2 references, which is Clicking immediately lists where the function is used. In Eclipse, on the other hand, you can find the same thing in the right-click context menu under References or with Ctrl+Shift+G.

public void setHours(float allHours) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setHours(allHours);
}
});
return;
}
txtpnSummeAllerMitarbeiter.setText("Stunden aller Mitarbeiter: "+Float.toString(allHours));
}

The setHours function can be called from other classes, across threads, to update the hour count on the UI.
In Eclipse, however, Swing(Utilities) first had to be integrated in order to enable the graphical user interface design using javax.swing(.JFrame) in the designer.

Synchronization

I found the semaphores in Java to be more practical than those in C#.Net because they are more flexible.

java.util.concurrent.Semaphore javaSemaphore = new Semaphore(0);
javaSemaphore.tryAcquire(); // false
javaSemaphore.release();
javaSemaphore.acquire(); // true
javaSemaphore.drainPermits(); // javeSemaphore stayes at zero
javaSemaphore.release();
javaSemaphore.release();

System.Threading.Semaphore dotNetSemaphore = new(0, 1);
dotNetSemaphore.WaitOne(MillisecoundsToMaxWaitOn); // false
dotNetSemaphore.Release();
dotNetSemaphore.WaitOne(); // true
dotNetSemaphore.Release();
dotNetSemaphore.Release(); //throws an error

The limit on dotNet can be useful for reviewing erroneous and too frequent releases.

Include data

Data sources can be integrated with practical components. In Visual Studio this is e.g. the Entity Framework and a BindingSource component with which the display of a table runs as if by itself. In WinForms this was still a BindingSource object. In WPF a display is possible using DataView (see the following example). In Java, I quickly found the DefaultTableModel and the TableModelListener for displaying tables, so there was a little more programming work in Java, because the table structure had to be created without a designer. However, there are probably open source components for it that have remained untested.
(Mehr auf https://learn.microsoft.com/en-US/dotnet/desktop/wpf/data/?view=netdesktop-6.0)

myDataSetClassname myDataSetVar = new myDataSetClassname();
myDataSetVar.Init;
mDataGrid.AutoGenerateColumns = true;
mDataGrid.ItemSource = myDataSetVar?.Tables["DataTable1"]?.DefaultView;

In Java I wrote the class MitarbeiterTable.java for the sample program. And for the display it needed the following code:

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
mTable = MitarbeiterTable.getInstance();
mTable.getTable().setShowGrid(true);
...
panel_1.setLayout(new BorderLayout(0, 0));
panel_1.add(mTable.getTable(), BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(mTable.getTable());
panel_1.add(scrollPane, BorderLayout.CENTER);

However, I consider such differences to be negligible. Of course, it's faster if you have less programming to do. However, it doesn't matter how it is implemented, because once you know the implementation from one language, you will find an equivalent in every other.

Studios and Extensions

Studios

Visual Studio

I always enjoyed the Visual Studio the most, for a number of reasons.

  1. It ran even with partly defective hardware, without data loss and without damaging project data (Android Studio always crashed the project). The damage experienced was faulty RAM.
  2. Searching for and installing extensions is very clearly arranged.
  3. My git repository was fast and easy to use.
  4. The intelligent programming support was often helpful.
  5. Inline documentation contributed to the overview very early on by means of parameters and descriptions.
  6. Easy-to-use code and performance analyzes have often helped with troubleshooting.
  7. The device (smartphone) emulator is set up with just a few clicks.
  8. Without a deeper understanding, you can develop your own controls quickly and easily.
  9. The Entity Framework relieved you of a lot of work in data connection.
  10. The designers are very intuitive to use.

I have never tested many more options that the studio offers. In fact, I would rather check whether "Visual Studio Code" might already be sufficient for my needs.

Eclipse

I like Eclipse much more today. Having developed the sample program, I acknowledge that my memory of the Eclipse, how I knew it, is from the past. I find the same components there a little less intuitively than in Visual Studio. In Eclipse, for example, you can find it under Help – Eclipse Marketplace, which you can find in Visual Studio under Extensions – Manage Extensions. Most things can be found somewhere and somehow.

I was a little less enthusiastic about the designer, because the arrangement didn't work quite as well as in Visual Studio, but the result was ultimately what I wanted, I think it's part of getting used to it.

Despite the faulty memory, Eclipse also left the workspace intact and ran smoothly.

Extensions

Entity Framework

There are tutorials for the Entity Framework. After going through a 200-page tutorial, I was convinced that it is almost intuitive to use and should be used whenever using databases.

Under Eclipse, the two open source extensions SQL DAL Maker and KPA Diagram Editor may perform the same or almost the same. The scope of the tutorial shows why I've saved this experience of testing for a later date.

Swing

I found JDialog and JFrame in the project explorer under right click - New - Other... or Ctrl + N as the objects I needed for a designer supported interface in the Swing Designer folder, subfolder of WindowBuilder using the search function.

 

Launch4j

To get an executable exe file, the Eclipse jar file can be converted into one using Lounch4j.

I selected JavaSE 9 in Eclipse and as a minimal version I entered 9.0.0 in the launch4j tool, that's how it worked. By selecting the output file and the jar file and the icon and saving the config by clicking on the gear. However, the file should be signed, otherwise it may be deleted by an antivirus.

(See also https://de.wikihow.com/Aus-Eclipse-eine-ausf%C3%BChrbare-Datei-erzeugen)

Under Properties - Java Build Path - Libraries - Modulepath - JRE System Library - Edit... - Execution environment, JavaSE 9 (jre) had to be selected, which actually seemed a bit hidden at first.

Fazit

Java != C#, but whether C#, Java or Kotlin, everything is programmed in the same way and if a program already exists, it becomes child's play to transfer it to other languages, such as C++. There is probably hardly anyone who knows the libraries by heart, just like hardly anyone who knows the encyclopedia or the Duden by heart. It's always just a little bit of syntax, because you look up everything else anyway.
Structure is another issue that needs to be recognized in order to navigate. Maybe it suits some people better or worse, maybe the freelancer can do it better because he adapts more often to changing work environments.
Or in other words:

 

It can what lives and weaves
only thrive in freedom
the image of the Creator
can only be the free one.
August Heinrich Hoffmann von Fallersleben (1798-1874)

Comments

No Comments

Write comment

* These fields are required