First you must decide what kind of data you want stored in the variable. This is called to declare the variable. If you want to store an integer, and you declare the variable as that data type, then you can only store integers. If you want a decimal number too, you must declare another variable to hold that number. The value of the variable can be altered later, as long as the data type is the same.
I am making a program to keep track of how many goldfish there are in my aquarium. I name a variable numberGFish, and since all my fish are in one piece, I use the integer data type. To declare this variable in Java, I would write the following piece of code:
int numberGFish;
Now I have an integer variable called numberGFish. That's how easy it is to declare a variable! Take notice of the semicolon at the end, in Java semicolons always mark the end of statements.
I want my Java program to know how many goldfish I have as well, and not just have an empty variable. To do this I write:
numberGFish = 400;
When I declared the variable numberGFish, I could have assigned a value in the same statement. This is called initializing, because I give a value to the variable at the same time as I declare it. If you know the starting value off the bat, it's often a good idea to do it like this.
int numberGFish = 400;
I also have customers that are interested in my goldfish. Every day goldfish are being sold to new homes, and at the end of the day I like to keep a record over how many fish are left. This means that I will need to declare another variable, one for the number of fish sold. Since I don't like to waste space, I would like to put this variable in the same statement as the number of fish that I have. I don't sell half of a fish, so this will also be a whole number, which means it matches the data type. Luckily for me, Java makes it very easy to do just this, just separate the two variables with a comma.
int numberGFish = 400, numberSold = 30;
This is just how easy it is to declare an integer variable in Java!
Published by Cloudage
I am a student studying and tutoring in math, chemistry and physics. View profile
Java Programming Tutorials: The Online Give and TakeIrrespective of whether you are an experienced programmer or just a rookie in the domains of Java programming, the Internet offers a wealth of Java tips and tricks and tutorials...- Beginner Java Programming: How to Read Java CharactersIt's so easy to get confused about how to read characters in Java. Here you'll learn the easiest way to read characters.
- Java Programming Language Tutorial: From Oak to JavaThis article covers the main points of Java, as a language.
- JAVA - What You Need to Know About ItJava is the first pure object oriented language that takes into account the object oriented theory. The best feature of Java is that it is platform independent. Java programs can run on mobile phone, computers, chips,...
- Access Specifiers in JAVAClasses enable an object to access data variables or methods of another class. For example, in a banking application you might need to hide information, such as customer balance, from unauthorized access by other clas...
- A Short Introduction to Java Programming Language
- Netbeans.org A Basic Review
- Your First Java Program
- Columba: A Multi-Platform Email Client for Windows, Mac and Linux (Written in Java)
- Java Software: A Wonderful Robust Language, Able to Do Important Tasks
- A Guide to Start Programming in Java
- Bridging the Gap Between Java and XML
- Learning how to declare variables is very important.
- Java programming takes patience
- Java tutorials are great means of learning!



