Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Variables

Hi all,
In this post I am going to share my knowledge in java variables. According to my previous post, I mentioned that a java class is same as a factory. In a factory there should be raw materials to produce something. In a java class, variables do the job of raw materials.

In java we have several kinds of variable types.


eg: Integer
Double
Char
Float
Long
Byte
Short
Boolean

Integer
Integers are whole numbers, for example, -35, 0, 2048, ....

this is the way of using integers.


class MyTest{
public static void main(String args[]){

int x = 10;
int y = 20;
int z = x + y;

System.out.println(z);
}
}

copy this code and paste to a notepad. Then compile and run to see the output. You can see the output as 30


In this code int x = 10; means,
x is an integer type of variable. Value of x is equal to 10.

Minimum value of an integer is -2,147,483,648 and maximum value of an integer is +2,147,483,647


byte
class MyTest{
public static void main(String args[]){

byte x = 10;
byte y = 20;
int z = x + y;

System.out.println(z);
}
}
Minimum value of a byte variable is -128 and maximum value of a byte variable is +127


short

class MyTest{
public static void main(String args[]){

short x = 10;
short y = 20;
int z = x + y;

System.out.println(z);
}
}

Minimum value of a short variable is -32,768 and maximum value of a short variable is +32,767



long

class MyTest{
public static void main(String args[]){

long x = 10;
long y = 20;
long z = x + y;

System.out.println(z);
}
}


Minimum value of a long variable is -9,223,372,036,854,775,808

and maximum value of long variable is +9,223,372,036,854,775,807

Double

double and float variables are used to represent decimal values

class MyTest{
public static void main(String args[]){

double x = 10.1d;
double y = 20.3d;
double z = x + y;

System.out.println(z);
}
}


float

class MyTest{
public static void main(String args[]){

float x = 10.1f;
float y = 20.3f;
float z = x + y;

System.out.println(z);
}
}

Boolean
A Boolean variable has two values(true or false).

This is the way of assigning a value to a boolean variable

boolean b = false;



String

class MyTest{
public static void main(String args[]){

String name = "mahesh";
System.out.println(name);
}
}


Data Type Description Size Default Value
boolean true or false 1-bit false
char Unicode Character 16-bit \u0000
byte Signed Integer 8-bit (byte) 0
short Signed Integer 16-bit (short) 0
int Signed Integer 32-bit 0
long Signed Integer 64-bit 0L
float Real number 32-bit 0.0f
double Real number 64-bit 0.0d




This post first appeared on Java For Beginners, please read the originial post: here

Share the post

Java Variables

×

Subscribe to Java For Beginners

Get updates delivered right to your inbox!

Thank you for your subscription

×