03 Java Fundamentals | Variables and Data types in Java | By Dummy for Dummies

 


VARIABLES

Monkey loves bananas. Monkey collects bananas. But monkey can collect and deal with only a limited number of bananas. To collect and utilize more bananas, Monkey needs pockets or bags. If Monkey finds a bag, it will put all the bananas inside.

But wait, bananas are not the only thing Monkey likes. Monkey also likes mangoes and apples. So Monkey finds 2 more bags. Now Monkey has each bag for each fruit. And each bag has different sections where it can put the fruits one by one.

                    Monkey = You (the programmer)
                    Fruits = Information (numbers, words, etc.)
                    Bags = Variables (where you store the info)

Now, why do you even need bags?
Because sometimes you deal with numbers like 2 + 2, easy stuff. But sometimes you’ll face a monster number like:

 1234567890123456789  

Imagine writing that everywhere in your code again and again. Painful, right?
Instead, you just give it a name like 
 number  or simply  n .

Now, whenever you use  n  in your program, Java will treat it as 1234567890123456789. Easy, clean, and way more powerful when data grows big.


TYPES OF VARIABLES

Monkey was smart. Monkey put each fruit in a separate bag. You are smarter, you put each type of information in different variables as well. The different types or classes of variables are called "Data Types"

Consider you have 3 types of data/information:

                    an integer →  123  
                    a decimal → 
 12.002  
                    a word →  "Coding"  

Each type of data has its own class/type in Java:

                      123  → Integer (whole number)

                      12.002  → Double or Float (decimal numbers)

                      "Coding"  → String (words, text)

In Java, there are many types of variables, and each specific type of data has a name according to its nature. Some important ones are:

Common Data Types in Java

  1. int → Stores whole numbers, like  5  or  -99 

  2. double → Stores decimal numbers, like  3.14159 .

  3. float → Also for decimals, but less precise (used when memory matters).

  4. char → Stores a single character, like  'A'   or  'x' .

  5. String → Stores text, like  "Hello 123" .

  6. boolean → Stores only  true  or  false .

  7. long → Stores very big whole numbers (Big int)

  8. short → Stores smaller whole numbers (less memory).

  9. byte → Even smaller whole numbers (used for efficiency).


THINGS TO REMEMBER

→ All datatype names are lower case except String, in which first letter is capital

→ String must be enclosed in double quotes like  "Word" 

→ char must be enclosed in single quote like  'A'  

→ char can store just a single character  'L'  or  '@'  

boolean values are both in lowercase i.e.  true  or  false  




CREATING VARIABLE INSIDE CODE

When creating a variable inside a code you need to first write it's datatype, then a name for it then assign it to a value. Let's store the word  Skibidi  in a variable.
  1. It is a word so Datatype will be String
  2. Name can be anything but.... must start with a small letter and must not contain characters other than  _  (underscore)
  3. Use equality  =  to assign the value
  4. Then write the value  "Skibidi"  
  5. After end of each line of code in java you write semi colon  ; 



 public class Main{

    public static void main(String[] args) {

        String name = "Skibidi";

        System.out.println(name); //This will print Skibidi

    }

}


OUTPUT:

Skibidi 

CREATING ALL IN ONE CODE



 public class VariablesExample {

    public static void main(String[] args) {

        int bananas = 123;

        double price = 12.002;

        String hobby = "Coding";

        char grade = 'A';

        boolean isHappy = true;


        System.out.println(bananas); // This will print 123

        System.out.println(price); // This will print 12.002

        System.out.println(hobby); // This will print Coding

        System.out.println(grade); // This will print A

        System.out.println(isHappy); // This will print True

    }

}

OUTPUT:
123
12.002
Coding
A
true

EXTRA TIPS.
If you just create a variable and do not assign any value it is called "Decalaration"
        int num;  
When you assign it a valute later it is called initiation
         num = 199; 


CLOSING

That’s it for Variables and Data types

In the next blogMonkey won’t just store fruits, Monkey will start playing with them 🍌➕🍎 (yes, I’m talking about operations and expressions).