Java Variables – Java example

 

Java Variables :

Java variables  are used to store values and they are of different types i.e., data types. So let’s get to see them and understand how to initialize and assign a value to them.

When we are writing even a sample java program we need to use several variables to process the user inputs and a process the data and return value so every where we use variables.

Lets get started, different types of java variables available are..

  1. String
  2. int
  3. float
  4. boolean
  5. char

 

We use these java variables in different scenarios according to their usages while calculating user inputs, to display the percentages, to print a simple message, to show a status with true or false or to accept a single variable we use these variables lets see them with a example of initialization.

 

String:

We can store a message and process them using this data type for example we can store a message by initializing the java variable with their data type and assign value to it as

String abc;

 

here we have initialized a java variable abc as String and we can use this abc to store a String type value now assign a value to it as

abc = "welcome";

 

We can make them in a single line even as

 

Note:

Make sure that the value is to be surrounded by ” ” i.e, double quotes and String is also called as a char array which has a maximum length limit of 2147483647.

 

String abc = "welcome";

and let’s see by printing this String to know whether this works or not!!!

 

// String

String abc = "welcome";

System.out.println(abc);

Java Variables

 

 

 

 

 

 

int:

This data type is used to create a variables which can take integers and help us to make calculations and everything we need to do with integers

We can declare and initialize a java variable and assign a value to it as

Note:

We can store integers with in a valid range of -2,147,483,648 to 2,147,483,647 i.e., both positive as well as negative numbers within that limit

int a = 10;

 

  •  int –> data type
  •  a –> variable
  •  10 –> is value assign to “a” variable

Now you can test this as

 

// integer

int a = 10;

System.out.println(a);

Java Variables

 

 

 

 

 

float:

The float data type accepts integers too but the major difference is it accepts decimal points too i.e., when we try to print a exact value with out any rounding up we can make use of these for example when you try to show your exam result as a percentage out of 100 we can show with flat as 70.15 or 65.23 …. now i think you can understand , this type of declaration is not possible using integers

So every data type has its own importance and unique usage

We can declare them as

float value = 10.15f;

 

Note:

Here ‘f’ represents float need to add that at end of value if there is decimal value with a maximum limit of  1.4E-45  to  3.4028235E38

 

// float

float value = 10.15f;

System.out.println(value);

Java Variables

 

 

 

 

 

 

Now what if i don’t specify a decimal value  and use float will it be integer or float confused lets see.

// float

float value = 10;

System.out.println(value);

 

here there is no decimal value and ‘f’ specified

Java Variables

 

 

 

 

 

 

when you specify float it will automatically consider it as value containing decimals.

 

boolean:

Now when you want to decide in between two conditions whether its true or false we use boolean

For example want to know did you pass or fail in a exam then

boolean exam = true;

declared a variable exam as boolean and assign it true to check it we need to use if else condition  now what is if else condition will see it later for now just get through it as below

 

if (exam==true)
// when true
else
// when false

 

as we have assigned true to variable we get true block executed

 

// boolean

boolean exam = true;

if (exam==true)
    System.out.println("pass");
else
    System.out.println("fail");

 

Java Variables

 

 

 

 

 

 

char:

char data type accepts a single characters like ‘a’, ‘b’….’z’, we have seen String data type previously which stores a message i.e.,

array of characters i.e., group of characters to together stored as a String.

 

Note:

char has a value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).

 

// char

char text = 'a';

System.out.println(text);

 

Java Variables

 

 

 

 

 

 

If any query’s on this tutorial on Java Variables do let us know in the comment section below.

For more interesting tutorials do like and share this tutorial.

Show Buttons
Hide Buttons