Read Java Interview Questions and Answers for Beginners and Experienced

Java Variables Examples

In this Java tutorial with examples, we will study about Java variables. What is Java variables? Why we use variables in Java and How to use Java variables.. we will see various Java variables examples.

 


Java Variables

Variables are used to hold or store the specific value.These values could be numeric, String, decimal etc. These values are stored using variables. In Java we also use variables to store values to execute the flow.

In Java we have different types of variables depending upon the value it holds.

Strings: String type of variable hold the String data.

Number: Numeric data can be hold by number type variable. numeric data type like int, float, double.

Boolean: Boolean data like true or false are stored in boolean Java variables.

 


Declare Java Variables

Before using variable in Java, it must be declare first. Now let’s see how to declare Java variable. We can learn to declare Java variable by looking Java variable syntax.

data_type var_name = var_value

 

  • data_type  is the data type of variable
  • var_name is the name of the Java variable.
  • var_value is the value of the Java variable.

 


Java variable naming convention

In Java there are some rules to declare any variable, In general language we call them naming conventions. so let’s see what are those.

  • Java variable can be start with lower case alphabets or uppercase alphabets  like

int age =10;

int Age=10;

int AGE=10;

All above are valid

  • Java variable can not have any special character like  @,!#$%^ etc. only _ is allowed.
  • Java variable can not start with numeric values like int 1age=10;

public class JavaExamples {

public static void main(String[] args) {
//Java variable Example in Java by ByteArray.in
int firstNo = 10; //valid
int _secondNo = 20; //valid
int AGE=20; //valid
int !age=10; // Invalid
int page_no=20; //valid

}

}

 

 

 


Java variable Example

Above we study about the Java variable example. Let’s understand how Java variable works by an example.


public class JavaExamples {

public static void main(String[] args) {
//Java variable Example in Java by ByteArray.in
int a = 10;

System.out.println("value of variable a is :"+a);

}

}

 

Output

value of variable a is :10

 

Explanation

In above Java variable example, there is one variable a which is of datatype int. Java variable a value is 10.

 

Let’s see some more examples of Java variables.

 


Java int variable example

In this Java int variable example, we will see how to declare int data type variable in java by an example.


public class JavaExamples {

public static void main(String[] args) {
//Java int variable Example in Java by ByteArray.in
int firstNo = 10;
int secondNo = 20;

System.out.println("Sum of two variables is \n"+(firstNo+secondNo));

}

}

 

Output

Sum of two variables is 
30

 

Explanation

In above Java int variable example, there are two int variables firstNo and secondNo declare with int datatype.

 


 

Java long variable example

Here we will see about the Java long variable example. Java long variable is large than Java int variable.


public class JavaExamples {

public static void main(String[] args) {
//Java long variable Example in Java by ByteArray.in
long number1=1234567891000900L;
long number2=1234567891011121314L;
System.out.println("Addition of two long number is : "+(number1+number2));

}

}

 


output

Addition of two long number is : 1235802458902122214

 


Explanation

Java long variable is declared by long keyword and after the value, put L or l . it will tell the compiler that it is long value.

 


Java boolean variable example

Java boolean variable returns true or false. let’s see boolean variable example


public class JavaExamples {

public static void main(String[] args) {
//Java boolean variable Example in Java by ByteArray.in
String name="Allen";
boolean isActive=true;
boolean isMarried=false;
System.out.println("Employee :"+name+" \n is Active "+isActive+"\n Married :"+isMarried);

}

}

 


output

Employee :Allen 
is Active true
Married :false

 


Explanation

boolean variable is declared with boolean keyword and can have two values true or false.

 


Java char variable example

Java char variable represent character variables in Java. let’s see Java char variable example.

 


public class JavaExamples {

public static void main(String[] args) {
//Java char variable Example in Java by ByteArray.in
char a ='a';
char b ='b';

System.out.println("Value of character \n a is "+a+"\n b is "+b);

}

}

 


output

Value of character 
a is a
b is b

 


Explanation

character variable in Java is represented with char. values of char data type is written inside single quotes ”.

 


Java String variable example

Here we will see Java String variable example how String variable are used in Java with example.


public class JavaExamples {

public static void main(String[] args) {
//Java String variable Example in Java by ByteArray.in
String fName="Byte";
String lName="Arrray.in";

System.out.println("full Name is :"+fName+lName);

}

}

 


Output

full Name is :ByteArrray.in

 


Explanation

In Java String variable example, we have two Strings. String variables are declared by String keyword.

 


Java Float variable example

Let’s discuss about Java float variables with example. Java float variables store the decimal values in Java.


public class JavaExamples {

public static void main(String[] args) {
//Java float variable Example in Java by ByteArray.in
float apple=12.50f;
float milk=50.45f;

System.out.println("total price is :"+(apple+milk));

}

}

 


output

total price is :62.95

 


Explanation

Here milk and apple are two float variables in Java as they have decimal or fractional values. In Java decimal values can be written in float variables.

 


Java double variable example

Java double and Java float are both used for decimal values in Java. In Java double larger value can be put than Java float. Lets see Java double variable example


public class JavaExamples {

public static void main(String[] args) {
//Java double variable Example in Java by ByteArray.in
double price=12.57;

System.out.println("total price is :"+price);

}

}

 


output

total price is :12.57

 


Explanation

double type variables in java are declared by double keyword.

 


Java byte variable example

Java byte variable is smallest data type in integer.  it range from -128 to 127. it means only values in between -128 to 127 can be stored in this byte variable in Java. Let’s see Java byte variable example.


public class JavaExamples {

public static void main(String[] args) {
//Java byte variable Example in Java by ByteArray.in
byte price=127;

System.out.println("total price is :"+price);

}

}

 


Output

total price is :127

 


Java short variable example

Java short variables are bigger than Java short data type and smaller than Java int data type. Let’s see Java short variable example.


public class JavaExamples {

public static void main(String[] args) {
//Java short variable Example in Java by ByteArray.in
short tweets=32724;

System.out.println("total tweets are :"+tweets);

}

}

 


Output

total tweets are :32724

 


So, in this Java Variable example, we study about various Java variables with example. we study about int, float, double, short, byte, char, String variables with example.