C# Variables, Comments & Data Types

Advertisements

Hello everyone,

Michael here, and in today’s post, we’ll explore C# variables, comments (notes that developers leave on code to explain it to other developers), and data types.

For many of the other programming tools we’ve covered on this blog’s journey like Python and Java, C# has its own ways of dealing with variables, comments, and data types.

In Python, here are some common variables, comments, and data types:

Variables

a = 3
b = 2

print(a+b)

5

Comments (single hashtag)

# This will print out the sum of 3+2

a = 3
b = 2

print(a+b)

5

Data types

complex
int 
str
bool 
dict

Likewise, in Java, here are some common variables, comments, and data types

Variables

String name = "Michael";
int year = 2024;

Comments

// This is a code comment
System.out.println("Hello world")

Data types

int
float
String
char

The variables, comments, and data types (C# version)

Now that we’ve reviewed variables, comments, and data types in Java and Python, let’s explore how these concepts work in C#.

Take this simple C# script, filled with variables, comments and data types:

// This code is writing a simple sentence with both string and int variables
int year = 2024;
string name = "Michael";
string month = "December";

Console.WriteLine(name + " is writing this post in " + month + " " + year);

Let’s examine this script line-by-line:

  • The top line with the double backslashes // signifies a comment being added to the script. Like Java and Python, C# comments are single-line deals.
  • int year = 2024; indicates that the int value in this script will be 2024
  • The two string variables in this script indicate that the two string values I will use in this script are Michael and December, respectively.
  • The Console.WriteLine() section of the script will print out my output to a command line-in this case, the string I want to output is Michael is writing this post in December 2024.
  • Another thing worth noting-to concatenate values in C#, use the + (plus) sign to do so. As you can see, I used the plus sign several times to concatenate my string values with other text in order to generate the following line of code: name + " is writing this post in " + month + " " + year.

Now that we’ve explored some basic examples of variables, data types, and comments in C#, what are some other data types in C#:

  • long-think of this as the 8-bit version of the int data type in C# (int is a 4-bit data type) that stores a broader range of integers
  • float-a 4-bit decimal type that stores values up to 6-7 decimal places
  • double-an 8-bit decimal type that stores values up to 15 decimal places
  • bool-the C# boolean type which stores true or false values
  • char-the C# character type which stores single characters
  • string-the C# string type which stores string values

Variables, Data Types, and Comments in C#: Things to Note

What are some important things to note about variables, data types, and comments in C# that are either similar to or perhaps quite different from variables, data types, and comments in other languages we’ve covered throughout this blog’s run?

  • In C#, comments are always indicated with the // (double backslash) symbol.
  • Just like in Java, you must specify a variable’s data type in the script (like I did with int year = 2024; for example).
  • Unlike Python, C# has no data types to handle complex/imaginary numbers (like 3+5i for example).
  • The boolean truth table rules that apply to languages like Java and Python also apply to C# (e.g. individual statements A & B must be true for a boolean statement to be true).

Also, here’s my script from this lesson on my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Java Lesson 2: Variables and Data Types

Advertisements

Hello everybody,

It’s Michael here, and today’s post will be on variables and data types in Java. Variables are pieces of memory in Java that store information necessary for your program to run. Take a look  at this statement:

  • int gamesWon = 2

This assignment statement (which is what you call the line of code where you create a variable and give it a value) displays the variable’s name (known in Java jargon as an identifier), value, and data type (int). Data types, such as int, indicate what kind of value the variable is. In the above example, the data type int indicates that the value is an integer.

  • When naming your variables, remember that you can only use letters, the numbers 0-9, and the underscore character. Also, the first character of your variable name can’t be a number.
    • The variable names 4twenty, go-Browns, and Linkin....Park won’t work, and the complier will complain if you try to use any of them.
  • One thing I didn’t mention in the previous Java post is that Java is case-sensitive, so that variables like southPark, Southpark, and SouthPark would be treated as 3 different variables rather than the same variable. Having 3 same-named but differently-capitalized variables is perfectly acceptable in Java, though it will get confusing for you the programmer.
  • A peculiar Java syntax convention indicates that, with multi-word variable names like gamesWon, the first word (in this case games) should start with a lowercase letter and any subsequent words should start with an uppercase letter. It’s just good practice though; after all, the Java compiler won’t display error messages if you use GamesWon or gamesWon instead of gamesWon.

There are two types of variables in Java-primitive types and class types. Class type variables belong to a certain class (which can either be a built-in class such as String or Array or a class you create in your program). On the other hand, primitive type variables don’t belong to a certain class but are rather the eight basic types of variables in Java, which consist of:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

The first four variable types mentioned (byte, short, int, long) are all of type int. The only difference between them is the integer ranges they represent. Byte has the smallest range, spanning from only -128 to 127, while short goes from approximately -32,000 to 32,000, int from roughly negative 2 billion to positive 2 billion, and long from negative 9 quintillion to positive 9 quintillion.

Float and double are floating-point numbers, which is a more technical way of saying that float and double are decimals. Numbers like 3.5, -9.99, and 1.042 would qualify as either float or double. Like the four integer variable types mentioned in the previous paragraph, the only difference between float and double are the ranges they represent, with double having the wider range of decimals.

The last two variable types, char and boolean, don’t store numbers. Rather, char stores single characters, which can consist of any of the letters of the alphabet, the numbers 0-9, and symbols like the pound sign (or hashtag for the millennial crowd) or percent sign. Char can only be 1 character, so the variables g^ksf and dipppk can’t be of type char since they have multiple characters (String would be a more appropriate type). And yes, even though I said char doesn’t store numbers, a variable like char = 0 wouldn’t be regarded as a number. Boolean stores the values true and false and is used when dealing with logic statements (more on those in a future post). For instance the statement (41 > 55) would qualify as a logic statement by Java and thus would be boolean. If you’re wondering, this statement would return false (which can be figured out easily because 41 is less than 55).

Now, if you’d like to see how variables work, here’s a little demonstration:

Inside the main method, I declared an int variable-currentYear-and gave it a value-2019 (well, because that’s the current year). Remember to put the assignment statement inside the main method, because the compiler will complain if you try to do otherwise. I then asked the program to print out the currentYear, which it did.

One thing to know with System.out.println and variable names is that you can simply put the name of your variable in the parentheses and the value will print out.

For those that would like to see a video demonstration of how variables work, click this link-Variables Demo Java.

Thanks for reading,

Michael