Some programming languages are not this strict about types, but if you're dealing with Java or C#, you can bet the compiler is going to complain about the types. They are often referred to as type safe or strongly typed. These may sound like important phrases, but really, you should simply be aware of them and the effect on what you can do in your code.
In the helloworld class below, we have a method. In a Java application, there must be a public static method that returns void and accepts an array of strings. I know that was a mouthful, but some of those are keywords that we've already heard about. The "public" keyword just tells us that this method is not hidden away so that only methods in this class can use it. The "static" keyword means we don't need to create an instance of the helloworld class in order to use the main method. All of this is really important because you have to start your application somehow in order to use any particular instance of any of your classes. With "public" and "static" out of our way, let's discuss for a moment the keyword "void". There are a number of other things that could go here if this wasn't the main method. This is because "void" is the return type of the main method, which is a fancy way of saying that when the main method finishes, nothing will be returned. Of course, we could have a method that adds two whole numbers, "int" was a keyword and what we call a primitive type. If we're calling a method to add numbers together, we would want that method to return the answer, an int. The code for that would look like the following: In this case, our parameters have changed from an array of strings to a pair of integers. We also use the "return" keyword to return the sum of our integer variables. It is very important that the return type matches the type that follows the "return" keyword. Otherwise the code will break. Because both a and b are integer types, summing them produces an integer.
Some programming languages are not this strict about types, but if you're dealing with Java or C#, you can bet the compiler is going to complain about the types. They are often referred to as type safe or strongly typed. These may sound like important phrases, but really, you should simply be aware of them and the effect on what you can do in your code.
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
Disclaimer: The opinions expressed herein are my own. I do not represent my employer, coworkers, mentor, friends, or family in any way on this website or any of its connected social media accounts.
Categories
All
Archives
September 2021
|