Variables Constant and Literals in JAVA JAVA by Ravinder Nath Rajotiya - April 12, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle VariablesPrimitive data typesExampleSome other data types are:Boolean-String data type-Example:Literls: Variables variables are used to store data. They act as lebel and store items of different types. We only store only the types ment for a variable Primitive data types Primitive data type in JAVA are predefined. Table below shows various primitive data types along with the size and the range of numbers that can be accomodated in each type. Data Type Size Range of numbers byte 1 byte Integers (-128 to + 127 short 2 byte Integers(-32768 to 32767) int 4 byte Integers(-2,147,483,648 to +2,147,483,647) long 4 byte Integers(-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807) float 4 byte Floating point numbers(+- 3.4E-38 to +/-3.4E38, 7 digit of accuracy) double 8 bytes Floating point numbers(+/-1.7E-308 to +/-1.7E308, 15 digit accuracy) Example To find the average, that is sum of two divide by ‘2’ which will be (20+5)/2= 12.5 which will be floating point, so need to declare average as double. Average = (firstNumber + secondNumber) / 2; But if we print out the average we gette answer will be 12.0 which is not the correct one. We can use the + operator in the System.out.println(“Average” + average); the + operator is used to concatenate the message string and the value of variable. System.out.println(“Average” + average); it prints: Average: 12.0 ; this is because of integer division. but the average should be (20+5)/2= 12.5 There are two solutions for this problem: 1. instead of division by 2; change it as division by 2.0 2. Or, cast the summation as (double)(firstNumber + secondNumber) / 2; this will convert integer division as the double division by integer division. Some other data types are: Boolean- Boolean data type can have only two values “TRUE” and “FALSE”. Use this data type as flag and represent one-bit of information, but its size is not precisely defined. boolean value; value=false; //if you try to assign some other values as string or integers , IntelliJ will report error now we can use System.out.println(value); // this will print values as false Char data type It is used to store single character of data char letter; letter=’R’; // assigned with in single quottes.// Intellij will not allow wrong type System.out.println(letter); String data type- String data type is used to store strings/text. String is a class, but for now let us treat as one of the data types. We will see the Classes in later chapters. IntelliJ will report errors and suggestions if you try to assign a wrong value to a variable. Declaration of string variable String name; //here String is a class, but for now, let us treat it as data type used to declare string variable String surname; then we can assign the values to string variable as: name=”Ravinder”; //IntelliJ will through error if we try assigning wrong values to name and Example: public class HelloWorld { public static void main(String[] args) { String name; String surname; name=“RAVINDER”; surname=“Rajotiya”; System.out.println(“Name:”+ name +“\n” + “surname :”+surname); } } See the use of “\n” with the + operators. It will concatenate the variable name and the value then it concatenates with newline character “\n” and then on the new line concates the surname and its values. Literls: literals is basically a notation for denoting fixed value to a variable. In the above examples firstNumber, secondNumber, value, name, surname, lettr are all variables by value1 = 5 value2 = 20 name = “Ravinder” surname = “Rajotiya” Boolean flag = false letter = ‘R’ These are all literals in above examples and are fixed values assigned to variable. Constants: Like variable, they can be assigned values but unlike variables their values cannot be be changed while the program is running. A constant in JAVA is declred with final keyword. The value once assigned cannot be then changed later Final int firstNumber ; //keyword final used to declare constant in JAVA firstNumber = 20; now hereafter if we assigne firstNumber = 25; then somewhere along the line it will be wrong to re-initialize that variable. Also it will be a good programming practice to make all constant declarations ar in UPPER_CASE. For example: final int FIRST_NUMBER; public class variable_Constants_literals { public static void main(String[] args) { final int FIRST_NUMBER; int secondNumber=20; FIRST_NUMBER = 11; double average; average = (FIRST_NUMBER + secondNumber)/2.0; System.out.println(“Average :”+average); } } Share on Facebook Share Send email Mail Print Print