Java - Variable and Data Type

Types of data :
  • Integer,
  • FloatData,
  • boolean,
  • Char, and
  • String
  • Variable is a name of memory location.
  • Each variable in Java has a specific type, which determines the size.
  • The range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. The basic form of a variable declaration is shown here:
data type variable [ = value][, variable [= value] ...] ;

Following are valid examples of variable declaration and initialization in Java:
							 
int a, b, c;         // Declares three ints, a, b, and c.
int a = 10, b = 20;  // Example of initialization
byte B = 20;         // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a';        // the char variable a is initialized with value 'a'


Valid Syntax Invalid Syntax
byte x=127 byte x=128;//Out of range
short x=10; short x=32768;//Out of range
int x=10 int x = 2147483648;//Out of range
long a = 100000L; long a = 100000;//End with L letter
float a2=10.66f; float a2=10.66;//Not end with f letter
double b=12.6664287277627762; double b=12.6664287277627762f;//not end with f
Different between Float and Double
							  
public class SampleEx {

	public static void main(String[] args) {
		
		float  a1=12.6664287277627762f;
		double b=12.6664287277627762;

		System.out.println("Float value : "+a1);
		System.out.println("Double value : "+b);
		
	}
}	

							  

Output :
							  
Float value : 12.666429
Double value : 12.666428727762776


							  

Float can handle about 7 decimal places. A double can handle about 16 decimal places.
Example : Find out MIN and MAX values of premitive datatypes
							  
public class SampleEx {

	public static void main(String[] args) {

		System.out.println("Min byte value   = " + Byte.MIN_VALUE);
		System.out.println("Max byte value   = " + Byte.MAX_VALUE);
		System.out.println("Min short value  = " + Short.MIN_VALUE);
		System.out.println("Max short value  = " + Short.MAX_VALUE);
		System.out.println("Min int value    = " + Integer.MIN_VALUE);
		System.out.println("Max int value    = " + Integer.MAX_VALUE);
		System.out.println("Min long value    = " + Long.MIN_VALUE);
		System.out.println("Max long value    = " + Long.MAX_VALUE);
		System.out.println("Min float value  = " + Float.MIN_VALUE);
		System.out.println("Max float value  = " + Float.MAX_VALUE);
		System.out.println("Min double value = " + Double.MIN_VALUE);
		System.out.println("Max double value = " + Double.MAX_VALUE);

	}
}	

							  

OUTPUT
							  
Min byte value   = -128
Max byte value   = 127
Min short value  = -32768
Max short value  = 32767
Min int value    = -2147483648
Max int value    = 2147483647
Min long value    = -9223372036854775808
Max long value    = 9223372036854775807
Min float value  = 1.4E-45
Max float value  = 3.4028235E38
Min double value = 4.9E-324
Max double value = 1.7976931348623157E308
	
							  

Rules For Constructing Variable Name :
  1. Characters Allowed :
    • Underscore(_) and dollar($)
    • Capital Letters ( A – Z )
    • Small Letters ( a – z )
    • Digits ( 0 – 9 )
  2. Blanks & Commas are not allowed
  3. No Special Symbols other than underscore(_) are allowed
  4. First Character should be alphabet or Underscore or dollar
  5. Variable name Should not be Reserved Word
  6. Variable names are case-sensitive.
Example :
Valid Names
num
Num1
_NUM
NUM_temp2
Invalid Names
number 1//blanks are not allowed
addition of program//blanks are not allowed
List of keywords in java

BytecodeTechnosolutions.com | Raju