variables in java

Data Types in JAVA

Java Tutorials
Share to others

Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:

  1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
  2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
data types in java

Variable Declaration

int a;
char ah;
byte value;
float salary

Variables and their ranges

variables in java

Naming Conventions

  • Variable names begin with lower case letters. Do not start with numbers or symbols (@, #,%, etc) except dollar ($) sign.
int number, $count;
  • If the variable name contains more than one word, the starting letter of the second word is capital or they are combined using the underscore (_).
int student_count;
int studentCount;
  • The symbols like @, #, *, %, etc are not included in the variable names.
int student@count;
  • Do not leave spaces in between the words.
int student count;
  • Always use meaningful names.

Variables default values

default values

Variable Initialization

When declaring the variable.

int a = 10;
char a = ‘A’;
float a = 23.67f;
double d = 56.678;

After declaring the variable.

int a;
a = 10;

Variable Types

There are three major types of variables

  • Local Variables
  • Class Variables (Static variables)
  • Instance Variables

Local Variables

  • These variables are declared inside methods.
  • Local variables are created when the method is called and it will be destroyed, once it exits from that particular method. (Memory management in JAVA)
  • Local variables can only be used within the method in which it was created.
  • Values should be assigned to local variables before they are used.
  • Access modifiers are not used for local variables.
class Variables{
   public static void main(String arg[]){
       char ch = ‘A’;
       float value;
       value = 78.9f;
       System.out.println(“The value of the number: ” + value);
       System.out.println(ch);
   }
}

Instance Variables

  • Declared within the classes, but outside the methods.
  • If we declare any instance variable in our Java program and use them without initializing, the compiler automatically assigns default values. These variables are used to store information needed by multiple methods in a single class.
  • Access modifiers can be used for instance variables.
class Student{
    int age=10;
    float marks;
    char grade;

    public static void main (String arg[]){
    
    }
}

Class/Static Variables

  • Global to a class and to all the instances of the class.That is there is only one copy of the variable.
  • Useful for communicating between different objects of the same class.
  • The keyword ‘static’ is used to declare the class variables.
  • Can be accessed directly using the name of the class without creating a separate object of the class
class Student{
    static int age=10;                     //class variable
    float mark1=80, mark2=34, mark3=56;    //instance variable

    public void calculateTotal(){
       float total=0;                      //local variable
       total=mark1+mark2+mark3;
       System.out.println(“The Total is:”+total);
    }
}


Share to others