Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Unit 2: Data types, Tokens in java Java Tokens The tokens of a language are the basic building blocks which can be put together to construct programs. A token can be a reserved word (such as int or while), an identifier (such as b or sum), a constant (such as 25 or "1.33"), a delimiter (such as { or ;) or an operator (such as + or =). For example, consider the following portion of the program – public class PrintSum { public static void main(String[] args) { int a, b, sum; a = 14; b = 25; sum = a + b; } } public - reserved word class - reserved word PrintSum - user identifier { - left brace, delimiter public - reserved word static - reserved word void - reserved word main - user identifier ( - left parenthesis, delimiter String - class name identifier [ - left square bracket, delimiter ] - right square bracket, delimiter args - user identifier ) - right parenthesis, delimiter { - left brace, delimiter int - reserved word a - user identifier , - comma, delimiter b - user identifier , - comma, delimiter sum - user identifier ; - semicolon, delimiter a - user identifier = - equals sign, operator 14 – constant ; - semicolon, delimiter Java Data Types Java programming language is a language in which all the variables must be declared first and then to be used. That means to specify the name and the type of the variable. This specifies that Java is a strongly-typed programming language. Like int num = 1; There are 2 types of data types which are supported by Java language programming – Primitive or intrinsic data type Non-primitive or derived data type A primitive data type is a data type which is predefined in Java. Following are the eight primitive data types: int It ranges from -2,147,483,648 to 2,147,483,647. Size is 4 bytes. This data type is used for integer values. However for wider range of values use long. byte It ranges from -128 to127. Size is 1 byte. We can save memory in large arrays using byte. We can also use byte instead of int to increase the limit of the code. short It ranges from -32,768 to 32,767. Size is 2 bytes. Short is used to save memory in large arrays. long It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. Size is 8 bytes. Float Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. Decimal place value - 7 and size 4 bytes. double This data type is generally the default choice for decimal values. Decimal place value - 7 and size 4 bytes. boolean The boolean data type is 1-bit and has only two values: true and false. We use this data type for conditional statements. True and false are not the same as True and False. They are defined constants of the language. char The char data type is a single 16-bit (or 2 bytes). It ranges from 0 to 65,535. They are not same as ints, shorts etc. Variables A variable is a container that holds values that are used in a Java program. To be able to use a variable it needs to be declared. SyntaxData type variable name; Declaring variables: byte Stream; short hour; long totalNumber; float reactionTime; double itemPrice; Choosing Variable Names The name given to a variable is known as an identifier. There are certain rules for identifiers: Reserved words cannot be used. They cannot start with a digit but digits can be used after the first character (e.g., name1, n2ame are valid). They can start with a letter, an underscore (i.e., "_") or a dollar sign (i.e., "$"). You cannot use other symbols or spaces between variable names (e.g., "%","^","&","#"). Variable name should be meaningful. There are several kinds of variables: Member variables in a class—these are called fields. Variables in a method or block of code—these are called local variables. Variables in method declarations—these are called parameters. Constants Constants are declared using the final keyword. The values of the constant can't be changed once its declared. Symbolic Constant1. Symbolic means take the same form as variable names. But they are written in CAPITALS to visually distinguish them from normal variable names. 2. After declaration of symbolic constants, they should not be assigned any other value within the program by using an assignment statement. For example, STRENGTH=200; is illegal. 3. They can NOT be declared inside a method. They should be used only as class data members in the beginning of the class. Declarationfinal int HOURS = 24; // Here HOURS is Symbolic constant and 24 is constant Now if we try to change the value of hoursfinal int HOURS = 24; HOURS = 36; we will get the following error from the compiler: cannot assign a value to final variable hours Define a constant value and use it 1. Using the final keyword to declare a variable. 2. The final keyword specifies that the value of a variable is final and cannot be changed. 3. It is a convention in Java to write constants in uppercase letters. public class MainClass { public static void main(String[] arg) { final int FEET_PER_YARD = 3; // Constant values final double MM_PER_INCH = 25.4; // that cannot be changed System.out.println(FEET_PER_YARD); System.out.println(MM_PER_INCH); } } Output 3 25.4 Casting (Type Conversion) It is sometimes necessary to convert a data item of one type to another type. Conversion of data from one type to another type is known as type casting. In java one object reference can be type cast into another object reference. This casting may be of its own type or to one of its subclass or superclass types or interfaces. Some compile time or runtime type casting rules are there in java. Some circumstances require automatic type conversion, while in other cases it must be "forced" manually (explicitly). Why it is required? Because, sometimes data is in a form that is unrecognizable to the program. Example: int a = 2; int b = 3; double c = a / b; We cast c as a double. If it was not, then the data would not have a decimal value. Automatic Conversion Java performs automatic type conversion when the type of the expression on the right hand side of an assignment operator safely promotes to the type of the variable on the left hand side of the assignment operator. Thus we can safely assign: byte -> short -> int -> long -> float -> double. Symbol (->) used here interprets to "to a". Example// 64 bit long integer long myLongInteger; // 32 bit standard integer int myInteger; myLongInteger = myInteger; In the above example, extra storage associated with the long integer, simply results in padding with extra zeros. The Narrowing Conversion The narrowing conversion occurs from a type to a different type that has a smaller size, such as from a long (64 bits) to an int (32 bits). In general, the narrowing primitive conversion can occur in these cases: 1. short to byte or char 2. char to byte or short 3. int to byte, short, or char 4. long to byte, short, or char 5. float to byte, short, char, int, or long 6. double to byte, short, char, int, long, or float 1. The narrowing primitive conversion must be explicit. 2. You need to specify the target type in parentheses. Example public class mainclass { public static void main(String [ ] args) { long a=10; int b=(int)a; //narrowing conversion System.out.println(a); System.out.println(b); } } Narrowing conversion with information loss Narrowing conversion may incur information loss, if the converted value is larger than the capacity of the target type. In the following conversion, there is some information loss because 9876543210L is too big for an int. public class MainClass { public static void main(String[ ] args) { long a = 9876543210L; int b = (int) a; System.out.println(b); } } Output 1286608618 An automatic type conversion An automatic type conversion will take place if the following two conditions are met: 1. The two types are compatible. 2. The destination type is larger than the source type.