A simple program are using Variables, Declarations and Assignments with full descrption

0




Variables, Declarations and Assignments

The following program illustrates how to declare and assign values to int, float, boolean, char, and String type variables:

1   public class Assign {

2   public static void main (String args [])

{

3    // declare integer variables

4     int x, y;

5   // declare and assign floating point

6   float z = 3.414f;

7   // declare and assign double

8   double w = 3.1415;

9   // declare and assign boolean

10   boolean truth = true;

11   // declare character variable

12   char c;

13   // declare String variable

14   String str;

15   // declare and assign String variable

16   String str1 = “bye”;

17   // assign value to char variable

18  c = ‘A’;

19  // assign value to String variable

20   str = “Hi out there!”;

21   // assign values to int variables

22   x = 6;

23   y = 1000;

24   }

25   }

The following are examples of illegal assignments:

y = 3.1415926;

// 3.1415926 is not an int

// It requires casting and decimal will be truncated.

w = 175,000;

// The comma symbol (,) cannot appear;

truth = 1;

// this is a common mistake made by ex-C / C++ programmers

z = 3.14156;

// Can’t fit double into a float. This requires casting.