Minecraft Blogs / Tutorial

Java Text Tutorial 3 - Variables & Strings

  • 378 views, 1 today
  • 1
  • 0
  • 4
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
9
And we're back! Welcome to episode 3 of Java Text Tutorials!



Table of Contents:
3. What is a variable?
     3.1 What is a string?
     3.2 How do you use variables and how will they be useful?
     3.3 How do you use strings and how will they be useful?


3. What is a variable?
A variable is data stored in a program that holds other data that can be used whenever told to. It's difficult to explain but once you learn how they work they should be no problem for any developer. In Java, there are different ways variables can come in. They will become very useful, especially as you get into larger programs.
The most basic variable in Java is this:
int a = 1;
or even simpler
int a;
What does this all mean? It is declaring an integer (which is int) to have the name of a and to be equal to 1. Or in the second one, it is declaring an integer to have the name a with no attributes to it. That means that a doesn't equal anything. So you won't be able to do much. We will touch more on this topic in another section.

3.1 What is a string?
A string is very similar to a variable. Except strings hold letters, numbers and characters. What a string is, its a line of text that can be used for many purposes. Usualy for graphical interface, which means what is on your screen.
A basic string looks like this:
String c = "Hello PlanetMinecraft";
Obviously they can look more complex, but this is a simple one. More on strings will be covered in another section.

3.2 How do you use variables and how will they be useful?
The most important thing in Java is knowing what and where to use a semicolen. A semicolen looks like this ; Make sure to include this at the end of each line of code you do in Java. Now I don't mean every single line but lines that you use and not in method lines (more on this later). So how do you use variables? We will be using integer as an example. First thing you need to do is go out side of your public static void main() area and type int. Then you need to give it a name. You can call this almost anything you want within exceptions. You cannot use spaces. You cannot use symbols at the beginning of a variable name. This includes !@#%^&*() although $ and _ is excluded. It is also traditional of Java developers to use camelCase. What this is - when you type a name such as thisisavariable. It is hard to see what is written. Instead it would be - thisIsAVariable. Basically the first letter of all words in your variable name with the exception of the first word are capitalized. After you have a name, you can either end it there and put a semicolen after your variable name, or set it equal to something. You can only have whole numbers such as 1, 2, 3, 4, 5 and so on. Here is how it would be set up:

int anInteger = 3;

Remember:
- Declare what type of variable it is, we used int.
- The variable name. Make sure to type it in camelCase.
- A semicolen to end the statement or
- An equal sign
- What it will be set to. Integers can only use whole numbers,
- And a semicolen ;

How will they be useful? Variables can be used in a variety of ways. You can use a variable to determine health, ammo, cost of items and much more.You can add, subtract multiply, divide and much more with a variable. This will be covered in the next tutorial however. So using this setup here is our class so far:
undefined
Now we need to print the variable to the console. Ths is easy. You do the same thing as we did for Hello PlanetMinecraft except change "Hello PlanetMinecraft" to anInteger. What this will do, it will print the number to the console. You will need to do some reordering for this to work. Just put the line of code that was outside public static void main() and put it in there like this:
undefined
Run the program and this is our result:
undefined

That is exactly what we wanted! It printed our variable anInteger to the console. Since anInteger was equal to 3, it printed 3!

3.3 How do you use strings and how will they be useful?
Strings are very similar to variables. Technically they are a variable but slightly different than most variables. Strings are using in a similar fashion as to other variables:

String aString = "A string";

What this does is:
- Declares the variable a string
- Calls the string aString
- Sets it equal to "A string"
- Don't forget the semicolen

You don't even need to have the equal to or the "A string".
This is also a string:

String aString;

The string is very similar to other variables but it is equal to a string of text rather than some numbers. So I will not go as in depth on strings.

How will they be useful? They are incredibely useful for setting things like dialog, names and even the word Ammo next to your ammo count ;).Just know that strings are essential for any program, small or large.

In the next tutorial we will talk about other types of variables and how to manipulate them.
CreditImage From Oracle(www.oracle.com)
Tags

Create an account or sign in to comment.

Lambdastic
10/13/2014 10:52 pm
Level 7 : Apprentice Warrior
Lambdastic's Avatar
You should make sure to mention that a String is a class rather than a primitive data type (int, double, char) and that a String is a char array.
1
Cool_Man7
10/14/2014 4:36 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
It is, but due to their very similar syntax I doubted that would be a problem.
1
ErikTheCat
09/26/2014 8:20 pm
Level 1 : New Miner
ErikTheCat's Avatar
These tutorials are great! I remember when I first started Java, and I was looking for the little amount of tutorials like these! Next, you should do voids and seperate classes, or some basic methods, like creating a new JObject. I hope you make more of these, and I will share them with my friend who wants to learn java. Thank you for the great tutorial!
1
Cool_Man7
09/26/2014 9:14 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Thanks! Glad you like it! Some of my ideas for the next tutorial will probably be math with variables and methods ;). I might get into making actual program tutorials once I've covered the basics.
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome