Jump to content

Search the Community

Showing results for tags 'java'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Calendars

  • Community Calendar
  • Takeo Ischi Fans's Chicken Attacks
  • HarlanTV's Events
  • Forge - Ember Official Community's Events
  • RabenKatze Art Studio and PawPals Cuddle Corner's Events
  • Celestial Empire's Stream Schedule

Forums

  • A Test Category
    • A Test Forum
  • AT Slayer's Club's Forum
  • Ember Development's Discussion
  • Ember Development's Patch Notes
  • Ember Development's Channels Feedback
  • Ember Development's Bug Reporting
  • Ember Nintendo Switch Group's Friend Codes
  • Ember Nintendo Switch Group's General Discussion
  • Ember Stardew Valley Farmers's Discussion
  • Cyberpunk Bois's Cyberpunk 2020
  • Cyberpunk Bois's Cyberpunk 2077
  • Cyberpunk Bois's Films
  • Emulation Bois's Old PC emulation (C64 and the like)
  • Emulation Bois's First generation of consoles
  • Emulation Bois's Second generation
  • Emulation Bois's Third generation
  • Emulation Bois's Fourth Generation
  • Emulation Bois's Fifth generation
  • Emulation Bois's Sixth generation
  • Emulation Bois's Seventh Generation
  • Emulation Bois's Eighth generation
  • SoulsBorne Bois's Dark souls 1/Remastered
  • SoulsBorne Bois's Dark souls 2
  • SoulsBorne Bois's Dark souls 3
  • SoulsBorne Bois's Bloodborne
  • SoulsBorne Bois's The tabletop Game
  • HarlanTV's General Chat and News
  • Ring of Elysium's Topics
  • Forge - Ember Official Community's The Forge
  • Game Developers's Post Your Game
  • RabenKatze Art Studio and PawPals Cuddle Corner's RabenKatze Art Studio
  • RabenKatze Art Studio and PawPals Cuddle Corner's PawPals Art Corner

Writers Blocks

There are no results to display.

There are no results to display.


Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Welcome back! In today's post I will be going through some common variable types. Be prepared, it's a pretty long and meaty post! I was originally planning on also introducing if/else cases in this post, but I think it would be a bit too much and will thus introduce that in the next post instead! Variable types Alright! Let me introduce you to some standard variable types: int, String, char, boolean, null and double. These are the variable types that you will be using pretty much all the time while coding. I will explain a bit about each one, starting off with int (and Integer...). But first I have to explain a little about primitive types. As you might have noticed, all of these variable types were written in lower case letter, except for String. This is because every mentioned type, except for String, is a so called primitive type. Primitive types represent raw values, for example a variable of type int will be storing the binary number of whatever value you set the variable to. All primitive types have equivalent wrapper classes, Integer for int as an example, that are inheriting from the Object class (Integer inherits from int) and store references to these objects... A String is actually not a primitive type, but a class referencing a certain object (hint, not a primitive string type!) but will explain this later on. Did you understand that? If yes, good job! If no, no worries... This is nothing you have to focus on. I just wanted to point these things out so that you might have a vague memory of it for the time when this information actually matters. For now though, have a coffee and rest your head! int and basic arithmetic operations The primitive type int actually stands for integer, with a wrapper class called Integer, and you can think of this type as storing non-decimal numbers. It is a bit more complex in reality, but nothing any new programmer need to worry too much about (but if you are curious, I do encourage you to check what an integer actually is). With int you can do basic arithmetic operations such as addition, subtraction and multiplication. How you do that I will show you in the following code snippets. As you can see I removed pretty much all code from our Hello World program and added a couple of new things. First of all, in order to initialize a variable in a method (there are other ways too...) you follow the pattern seen in the code: int number = 15; , which basically means that you set a variable named number of type int to 15. Don't forget ; ! Secondly, adding comments in the code that are explaining what you are doing is a good practice! Comments does not affect the program and will only be visible for the person reading the code and you add comments by using //. There is another way to comment, but we don't need that yet. Thirdly, I added 3 different ways to increase a number with 1. Not that writing number++2; will only make the compiler angry and it will also be grumpy if you would set an int to a decimal number, like 15.5. Feel free to check for yourself! Lastly, you may have noticed something different with out System.out.println (this method is going to be your best friend btw). In the previous post you were introduced to this lovely method which will print out whatever you need to the console and is a great way to make sure that the code is doing what you expect it to do (hence all the prints in the code snippet). However, this method will only accept text, or String objects, which means that we cannot simply write System.out.println("This is number " + number + "\n"); , but we will have to parse the int to a String object. There are a few methods available, but I chose to use Integer.toString(number) in this case. The + is how we put together String objects ("This is number" Integer.toString(number) will also result in an angry compiler) and the "\n" tells the compiler to start a new line (which can sometimes make the prints a lot more readable). Here I simply demonstrated how to subtract and multiply. Some division... I do recommend that you play around with this yourself for a bit before moving on to the next variable type(s), String and char! String and char A String is actually an array of char, and you can think of a variable of type char (or character) to be any single unit of textual representation (such as letters, commas, digits etc). An int can never be set to a letter, while a char can. You can initialize a char by simply following the following syntax: char c = 'E'; (you set this to any single unit, such as '1' or '*'. 'Em' will result in an error though). In order to initialize a String you will follow a slightly different syntax: String s = "Name"; (the "" tells the program that a sequence of characters will follow). It is perfectly fine to set a String to anything, even a so called empty string, but you cannot set it to a character (String s = 'E' will result in an error, but String s = ""; is fine). In this code snippet I wanted to show you what a String and char look like in code, but also what an array of char would looks like (as I mentioned, this is basically what a String is). As you can see here, there is no need to parse characters if you want to print them out, unlike int, but if you try to print out an array of char you will see something unexpected in the console. Simply printing the array will not produce any error, because what is shown is in fact a String... But it's a String of the reference to the array object and not what the object contains ('E', 'm', 'm', 'a'). In order to print out the content of the array in the example you either will have to print each element on it's own (myNameInChar[0]...myNameInChar[3]) or use the very handy String.copyValueOf(), using a toString() will not do what you want. This is a little bit too complicated to dig in right now though and will not explain the nature of arrays in this post, but it will be explained in a future post. Note that the first letter in the array was at 0 and not 1! double, null and boolean Almost done with explaining some of the basic types! You might be glad to know that this section will be a bit shorter than previous ones, since explaining everything about these types is a bit too advanced. A boolean is an algebraic notation used to represent logical propositions by means of the binary digits 0(false) and 1(true). A boolean can only be set to true or false. Using variables of this type is common if you are about to check logical expressions or statements (examples in the next post). A double is, very simplified, a decimal number and all numbers with decimals in Java are assumed to be of type double. The last type I will mention is null, which is a kind of a special flake. You cannot set a variable to null, but you can check in logical representations that a variable is/is not null. Wait... What now? You see, null indicates that a variable does not refer to any object or array... What exactly this means will be shown in the next post, since it's easier to explain it using if/else cases. As you can see you can use basic arithmetic expressions with a variable of type double too! Also, you would need to convert both boolean and double to String if you want to print out a variable of any of these types. Up next Whew....! That was a very informative post! In the next post, hopefully up in a few days, I will be getting into if/else... And maybe some loops! I hope you found this post informative and if you have some suggestions on what to add in the future, or if I should have longer/shorter posts, feel free to add that in a comment! Cheers!
×
×
  • Create New...