DEV Community

Kavya S
Kavya S

Posted on

Java Variable and Data type

What is Variable?
-Variable is a container that can hold value
In java,declaring a varible should be follows like

//dataType variablename=value;
int number=10;
Enter fullscreen mode Exit fullscreen mode
  • datatype : This defines what kind of data the variable can hold(eg:numbers,text,true/false)
  • variablename : This is a unique identifier you give to the variable so you can refer it later
  • = : this is assignment operator.It assigns the value to the variable on its left
  • value : the actual data you want to store

Variable Rules

  • Can contain letters, digits, underscores (_), and dollar signs ($).
  • Cannot start with a digit. (7days is invalid, but days7 is valid).
  • Cannot be a Java keyword (like int, class, public, if).
  • Case-sensitive (myVar and myvar are different variables).

DataType

  • as discussed,it defines the type of value
    There are 2 types of datatype

  • primitive datatype

  • non-primitive datatype

Primitive Datatype
There are 8 primitive datatypes

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean

Top comments (0)