Programming in Java: Data Types and Variables

0 32
Avatar for Raven_2510
2 years ago

The first thing that beginners should learn in every programming languages are data types and variables. If you have knowledge about algebra, you probably seen 'x'and 'y' letters. These are one of the fundamentals of programming that handle the data being gathered or passing data to the memory. Hi, I'm Raven and I will teach you some basics of programming in Java.

What are variables?

Variables are containers of information that are being stored in the memory, they're used for data manipulation like passing values from one to another. Memory location also known as variable address is represented by hexadecimal symbols, commonly used for referencing the values such as pointers.

How to declare a variable?

Sintax:

Datatype Name = Value;

int number = 1;

Rules of naming variables

Variables should start with a letter or underscore.

Valid:
Ant
ant
_ant

Invalid:
1Ant

Variable names should not contain other special characters other than underscore. In addition to that, variable names must not end with an underscore.

Valid:
home_address
phone_no123

Invalid:
homeAddress_
home-address
home-address*/!

Variable names are case sensitive.

Valid:
int num;
num = 1;

Invalid:
int num;
Num = 1;

Variable names should not be a programming keywords.

Valid:
result
str

Invalid:
new
public
void
null

Types of variables

public void method(){
    int variable;
}

Local variable - a variable that is declared and only accesible inside a method.

int globalvar;

public void method(){
    int localvar = globalvar;
}

Global variable - a variable that is not limited by block, or method scope, It is a global scope variable that is being declared outside a method. Granting accessibility across the program.

class ClassVar {
    Random instavar = new Random();
    
    public void method(){
        int num = instavar.nextInt(10);
    }
}

Instance variable - A type of variable declared outside of methods, constructors but inside a class, and access modifiers can be assigned. Example is object instantiation.

class Classvar {
    public static int statvar;
    
    public Classvar(){
        statvar = 10;
    }

    public static void Main(String[] args) {
        Classvar instavar1 = new Classvar();
        Classvar instavar2 = new Classvar();
        
        //statvar = 10
        System.out.println(instavar1.statvar);
        //statvar = 10
        System.out.println(instavar2.statvar);
    }
}

Static variable - Also known as class variable given that it is a class scope variable, it is declared inside a class and the value of that variable is not dependent to class objects, it means every object shares the same value of that variable. furthermore, it can be manipulated using any class objects.

public void method(){
    final int num = 10;
    num = 5; //error
}

Constant variable - It is a type of variable that is assigned to only one value, meaning, it can be only assigned to that value and cannot be changed anymore. Otherwise, it will give an error. In java it final keyword is used for constant variables.

For now, it doesn't matter if you don't understand the codes above, knowing the bsaic concepts is enough.

What are data types?

From the word data types, they describe the different kinds of data that can be stored in a variable.

kinds of data type

Primitive data types - predefined type of data in a programming language.

Non primitive data types - User defined type of data.

Common data types

//integer => 1
int
//float => 26.75f
float
//double => 3.14159265
double
//character => 'a'
char
//string => 'car'
String
//boolean => true or false
Boolean

Examples:

int number = 1;
float decimal = 5.86f;
double pi = 3.14159265;
String name = "Raven";
char letter = 'A';
boolean answer = true;

To remind you about the lesson outline, I have covered the definition of variable and data type, ways to name variables correctly, some common types variables and data types. I hope you learned something, keep practicing and goodluck!

4
$ 1.51
$ 1.51 from @TheRandomRewarder
Avatar for Raven_2510
2 years ago

Comments