Head First C# Logo

Converting Head First Java Concepts and Code to C#

Chapter 1

Page 2:  JVM vs. .NET Framework

Java: JVM
C#: .NET Framework
Note: Here is a brief comparison  of the "processing" differences in the architecture of Java vs. C#.

 

Page 5:  Printing to the Console

Java: System.out.println("Hello");
C#: Console.writeline("Hello");

 

Page 7:  Source Files

Java: source file = file.js
C#: source file = file.cs

 

Page 11

C#: While loop statement reference

 

Page 11

C#: If-else statement reference

 

Page 14:  BeerSong Example Code

Java: BeerSong.java
C#: BeerSong C#
BeerSong C# - Fixed

 

Page 16-A:  Generating Random Numbers

Java: Math.Random()
C#: C# does not have a Math.Random() method (e.g Math class with Random() method).  Thus, you must use the Random class and the .next()  method.  In the code conversion from Java to C# below you will actually use the .nextDouble() method.
Note: c-sharpcorner.com has a good overview of this functionality.


Page 16-B:  Simple Casting

Java: int rand1 = (int) (Math.random() * oneLength);
C#: The above code is an example of casting (converting one variable type to another - here from a double (e.g 12.123) to an integer (e.g. 12).. There are many rules about casting that will be reviewed later in the book.  For now, recognize that you use the following code to cast a double to an integer in C# (which is is happening in  Java above).
int rand1 = Convert.ToInt32((Math.random() * oneLength));
Note: c-sharpcorner.com has a good overview of this functionality.

Page 16-C:  Phrase-O-Matic

Java: Phrase-O-Matic.java
C#: Phrase-O-Matic.cs
Note: The two new things in this C# code that are not in the Java code are:
1) casting, which will be covered more in Chapter 3.
2) declaring objects and using methods, which will be covered more in Chapter 4.
Skim the code now and then go back to it later and it will make more sense.