Java variable declaration and initialization.

Points to Remember of the variables.

  • All numeric data types are signed(+/-).
  • char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports. internationalization. UNICODE is a character set which covers all known scripts and language in the world
  • The size of data types remain the same on all platforms (standardized).
·        data type variable [ = value] [, variable [ = value] ...] ;

Here data type is one of Java’s datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.

Java Variable Type Conversion & Type Casting.

Variable of smaller value is  assigning to another variable of that have bigger capacity.

Variable of larger capacity is be assigned to another variable of smaller capacity

Java data type summary.

Data TypeDefault ValueDefault size
byte01 byte
short02 bytes
int04 bytes
long0L8 bytes
float0.0f4 bytes
double0.0d8 bytes
booleanfalse1 bit
char‘\u0000’2 bytes
Variable Declaration.

To declare a variable, we need to specify the data type and variable name. This variable name is unique for the variable.

Int a

Variable Initialization.
Java variable declaration and initialization

Int a = 100

int a, b, c;         // Declares three ints, a, b, and c.

int a = 10, b = 10;  // Example of initialization

byte B = 22;         // initializes a byte type variable B.

double pi = 3.14159; // declares and assigns a value of PI.

char a = ‘a’;        // the char variable a iis initialized with value ‘a’

class Javavariables{
 public static void main(String args[]) {
  byte y;
  int a = 370;
  double b = 132.132;
  System.out.println("int converted to byte");
  y = (byte) a;
  System.out.println("a and y " + a + " " + y);
  System.out.println("double converted to int");
  a = (int) b;
  System.out.println("b and a " + b + " " + a);
  System.out.println("\ndouble converted to byte");
  y = (byte)b;
  System.out.println("b and y " + b + " " + y);
 }
}

Output.

int converted to byte
a and y 370 14
double converted to int
b and a 132.132 132
 
double converted to byte
b and y 132.132 - 132

Java Variable.

One thought on “Java variable declaration and initialization.”

Leave a Reply

Your email address will not be published. Required fields are marked *