Where we take good care of both your front and back-end needs

Great place to take your code to the next level

Checkout my work and get in touch

RastcodeS Logo
Go Back

Java Break - Declare and Initialize Variables - Java Core Part - VI


Written by Rastko

date - 13/Sep/18 | views - 2525

Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter in the Java Break series, where we explore core Java features. Check out the previous articles:


Declaring Variables


     Let's do a quick recap of what variables are. Variables are specified names, connected to a memory part, where Java stores our data. The first rule of declaring variables in Java is that you must provide the type. Next you have to give it a name. Check out this example:

String firstName;
int age;


     Here, we declare two variables. First one is of type String and is named firstName. The second, an int with a name - age;


Initializing Variables


     The moment we give our declared variables some value is called initialization. The simplest way of initializing a variable is to use the name, equals sign and a value:

firstName = "John";
age = 30;


     Often you'll want to initialize the variables as soon as they are declared. You can do this on a single line:

String firstName = "John";
int age = 30;


Multiple declarations / initializations


     You can also declare multiple variables of the same type on a single line:

String firstName, lastName;


     Make sure these are the same type, as this would not work:    


String firstName, int age; // DOESN'T COMPILE


     You should also note that, when declaring multiple variables of the same type and on the same line, you only need to provide the type at the beginning. This wouldn't compile

String firstName, String lastName; // DOESN'T COMPILE


     What about declaration? You can also do this on the sam line:

int age = 30, numberOfPets = 2;


     You can also mix it up like this:

int age = 30, numberOfPets, dailyCodingHours;


     Here, we've declared 3 variables, but we've only initialized 1 - age.

     Be aware, these single line declarations are separated by a coma, not a semicolon. This would also be illegal:

int age; numberOfPets; // DOESN'T COMPILE


     On the other hand, this would compile just fine:

int age; int numberOfPets;


     Even though these are on the same line, Java sees them as separate statements, because of the semicolons.


Identifier Rules


     When we name our variables, we have to follow a few rules:

     Legal Java identifiers must start with a letter, the $ symbol, or an underscore  _.

     Other than the first character, others can also be numbers.

     Finally, when naming variables, you are not allowed to use the reserved words. Reserved words are those special keywords, we've used before (class, public, import, void...).

     Let's check out all of those reserved words in a table bellow:

abstract assert boolean break byte case
catch char class const continue default
do double else enum extends false
final finally float for goto if
implements import instanceof int interface long
native new null package private protected
public return short static strictfp super
switch synchronized this throw throws transient
true try void volatile while  


     That might seem overwhelming, but don't worry about it, you'll learn most of these soon enough.

     Now that we have covered the rules, let's check out some illegal examples:

info@rastcodes // remember only letters, undesrcores, $ symols - also numbers after the first character
class // reserved word
native // reserved word
4thVariable // no numbers at first position


     Those were all illegal. The example bellow is legal, even though you should avoid writing code like this:





     We've shown both legal and illegal examples, but now we're going to talk about the good examples. Keep these two rules in mind, like they are the law:

     Variable and method names should start with a lowercase letter. Then follow with CamelCase. Here are some good examples:



     On the other hand, when naming classes, start with a uppercase letter and follow with CamelCase:



     One word of advice before we wrap up. Be descriptive and concise - int age is better than int a, firstName is better than firstNameOfPersonFromPersonList. You get the point.


     That's all for this chapter. Stay tuned. In the next Java Break, we'll talk about default initialization and variable scope. See you then!

View Post/Backend
Login to comment


No Comments yet. Be the first!

Currently working on a Laravel project

Check it out
Java Break - Declare and Initialize Variables - Java Core Part - VI
Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter...