Topic 2 Part1
Topic 2 Part1
1. Describe the features and basic concepts of Java language. 2. Write, compile Java source code and interpret Java byte code using Java Development Kit (JDK). 3. Implement the key of OOP concepts: classes and objects, inheritance and polymorphism.
Identify identifiers, variables and constants in Java program. Define assignment and expression statements in Java program List numeric data types in Java program Implement numeric data types in Java program Explain character data types in Java program : Implement character and Boolean data types, operator precedence in Java programs. Define typecasting in Java programs Implement typecasting in Java programs
Unicode and ASCII code Escape sequence for special character
Identifiers :
The name given to the variables, constants and method. Rules to naming Identifiers :
An identifier must start with a character (A-Z, a-z), an underscore (_) or a dollar sign ($). In an identifier, the letters other than the first one can be any character except the operators or keywords in Java (Examples: +, -, switch). The special characters such as #, !, @ and ^ cannot be used.
Blank and tab space cannot be used. Java is case-sensitive. Keywords listed in Java cannot be used as identifiers.
Identifiers Valid/Invalid Quantity 1amount $price my car Total_Price Valid Invalid Valid Invalid Valid
Reason Identifier should start with an alphabet. Identifier must not begin with a number. Identifier can start with a $. Blank spaces not allowed in an identifier. Special symbol underscore ( _ ) can be used in an identifier.
Identifiers First!
Valid/Invalid Invalid
Reason Special character exclamation mark ( ! ) cannot be used in an identifier. Identifier can have numbers. Identifier name must not end with the hash ( # ) symbol.
Number5 Price#
Valid Invalid
Identify which of the following are valid identifiers. If it is invalid, explain why.
a. b. c. d. e. f. bite the cake trouble 1stname Bubble good-bye Second!
Variables :
Is the name given to the memory location where a value is stored.
The name given to the variable is called as identifier. Each variable has :
Name Type Holds a value that you assign to them
Declaration :
A Java variable may refer to an object, an array, or an item of primitive. Syntax to define a variable : <data type> <variable_name>; E.g : char grade; //defines a character int [] grade; //defines a reference to array of ints int age = 25; // defines an integer Vector v; // reference to a Vector object
Initializations
Variables may be initialized as follow :
Primitive type : int i = 3; boolean g = true; Objects : Button b = null; Employee e = new Employee(); Arrays : int[] a={1,2,3,4};
Is a value that does not change throughout program execution. Every constant has :
Name Type Values that are set to them
Defines the type of the value to be stored in the memory. Data types in Java can be classified into two groups:
Primitive or Built-in data types Reference or User-defined data types
Description true/false
8 bits
char
16 bits (UNICODE)
2 bytes, unsigned, Unicode, 0 to 65,535. A char is a single character which can contains a letter, a digit, a punctuation mark, a tab, a space, or something similar. Char can be implemented by a single one character enclosed in single qoute marks such as : char MyChar=g;
Description 16 bits 2 bytes, signed (twos complement), covers value from 32,768 to 32,767 4 bytes, signed (twos complement), covers value from 2,147,483,648 to 2,147,483,647. All numeric types in may be cast into other numeric types(byte,short,long,float,double). When lossy casts are done(e.g:int to byte), the conversion is done depends to the length of the smaller type.
int
32 bits
Primitive Type Description long 64 bits 8 bytes, signed (twos complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (posotive or negative). Like all numeric types, floats also can be cast into other numeric types(byte,short,long,float,double). When lossy casts to integer type are done(e.g:float to short), the fractional part is truncated and ignored depends to the length of the smaller type.
float
Primitive Type Description double 64 bits IEEE 754-1985 8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d To 1.79769313486231570e+308d (positive or negative)
Are the reserved words available in Java language. The functions of each of the keywords are predefined.
Keywords
abstract, Boolean, byte, char, double, extends, final, float, implements, import, int, inner, long, new, native, package, private, protected, public, short, static, super, this, transient, var, volatile
Types
Keywords
Exception handling
class, cast, instanceof, void, synchronized if, else, switch, case, default for, while, do, break, continue, goto and return Catch, finally, try, throw, throws
1. Write a program to declare two variables hours_worked and pay_rate of integer data type and assign the values 8 and 300. Print the value of the variables hours_worked and pay_rate. 2. Write a program to declare a variable total_classroom of integer data type and assign the value 30. Print the value.
Is the conversion of a value of one data type into another data type. Typecasting is of two types:
Implicit conversion Explicit conversion
Implicit Conversion
When a value of data type with lower range is assigned to a variable of data type with higher range. Java automatically makes the conversion. E.g :
double x; // Size is 8 byte int y=2; //Size is 4 bytes float z=2.2f; x=y+z;//Implicit conversion
Explicit Conversion
When a value of data type with higher range is assigned to a variable of data type with lower range, the user needs to make explicit conversion.
Explicit conversion leads to data loss. E. g:
int total_value; double book1 = 300.40; total_value=(int)book1; // Explicit conversion
Write a Java program to declare a variable d of type double. Assign the value 102.5 to d. Declare a float variable fl. Assign the value of d to fl by performing an explicit conversion and print the value on the screen.
Is a symbol that instructs Java compiler to perform an operation, or action. Mathematical Operator :
Are operators that are used for performing simple mathematical calculations such as addition, subtraction, multiplication and division.
Calculate the total marks for the given five subjects. Declare appropriate variables, assign values as given below, and calculate the total marks: English = 85 Maths = 100 History = 75 Geography = 70 Art = 85
Assignment operator
Example 1
x = 3;
Syntax
Assignment operator
Example 2
x = x + 3; x += 3;
Syntax
<Variable name> <operator>= <value>;
Increment Operator
Increments the existing value by one.
Example
num++;
Prefix :
++a a=a+1; OR a+=1; a=a+1; OR a+=1;
Postfix :
a++
Decrement Operator
Decrements the existing value by one.
Example
num--;
Prefix :
--a a=a-1; OR a-=1; a=a-1; OR a-=1;
Postfix :
a--
Relational Operator
Are used to perform a logical comparison of values, which results in a value that is either true or false.
Usually controls other statements Used to compare data Example
>= <=
!= ==
a >= b a <=b
a != b a == b
Result True
15 > 20.75
15 == 15
False
True
15 <= 15
15 >=20.75 15 != 15
True
False True
Result
10 < 7+5
-35 >= 0
Logical Operator
Are used to combine two or more logical expressions to form a more complex logical expression.
To combine relational operator into better data testing statements
Example
(number<0 || number>100)
Operators && ||
Logical NOT
Condition1 true
Condition2 true
&& true
|| false
true
false false
false
true false
false
false false
true
true false
Conditional Operator
Is used to construct conditional expressions.
Example
Operator Presedence
Determines the order in which mathematical computations and operations are performed in an expression.
Is any combination of operators and operands that evaluates to a value. The expressions can be classified into three main categories:
Numerical Expressions - Combine variables, numbers and mathematical operators. Assignment Expressions - Assign values to variables. Logical Expression - Results in true or false.
S. No. 1 2 3
a 24 4 2
b 2 2 4
c 8 5 2
d 1
Result
4
5 6
-34 -34
3.5
6 6
2.0
-
(a+b)*c
a/b a%b