590 likes | 724 Views
JAVA. Data Types. Data types are very important in every language for storing and manipulating date. Java Provides eight simple types of data which can be grouped into four groups. 1. Integer 2. Floating Point Numbers 3. Characters 4. Boolean. Integer Types. Floating Point Numbers.
E N D
Data Types
Data types are very important in every language for storing and manipulating date. Java Provides eight simple types of data which can be grouped into four groups. 1. Integer 2. Floating Point Numbers 3. Characters 4. Boolean
Characters Character data type is used to store single UNICODE character. Unicode character set is 16 bit character set. The standard set of ASCII characters still ranges from 0 to 255. NOTE : char data type is unsigned type.
Boolean Java has a simple type called boolean, for logical values. It can have only one of two possible values.
The variable is the basic unit of storage in a Java program. A variable is defined by the combination of a type, variable name and an optional initializer. In java all variables must be declared before they can be used. int a = 4; Initializevalue Type Name
Some variable declarations examples: - 1. int a = 3; 2. int x,y,z; x = 4; y = 6; z = 3; 3. char c = ‘a’; 4. boolean b = true; 5. double d = 44.4, v = 43.3;
NOTE : All the integer types are considered int type by default. All the floating point types are considered double by default.
So if you want to store 4.4 in float datatype you can not write this: - float f = 4.4; Instead you write float f = 4.4f; This way you explicitly tell the compiler to treat 4.4 as float.
Identifier Identifiers are used to name variables, classes, interfaces, methods, and other Java language elements. An identifier is a sequence of characters that starts with an underscore (_), a dollar sign ($), or a letter (ASCII or Unicode). Subsequent characters may contain these characters plus the digits 0 through 9.
So followings are valid identifiers: - _minute $total Sum4 Followings are invalid identifiers: - &total Minute# 4hour
Both uppercase and lowercase letters can be used in an identifier. Because java is case sensitive, therefore these are treated as different identifiers: - Total total TOTAL toTal
Expressions An Expressions can be thought of as a programmatic equation. More formally, “An expression is a sequence of one or more operands and zero or more operators that produce a result. “ e.g. X = y / 3; A = 4 + 3 * 3/2;
Keywords const and goto are reserved but not used. Words like true, false, null are not used as variable name because they are used as literals.
Operators can be used to combine or alter the program values. Java contains a very rich set of operators. There are three types of operators: - 1. Unary Operators 2. Binary Operators 3. Ternary Operators
Unary Operators are that operators which require one operand to perform calculation. -4; 5++; Binary Operators are that operators which require two operand to perform calculation. 4/2; 5>=7;
Ternary Operators are that operators which require three operands to perform calculation. There are only one ternary operator in java. ?: (5>3) ? Value1 : Value2
Java Unary Operators Java Binary Operators Arithmetic Operators Shift Operators
Comparison Operator Bitwise Operators Short Circuit Logical Operators
Increment and Decrement Operator The ++ and – are java’s increment and decrement operators. The increment operator increases its operand by one; the decrement operator decreases its operand by one.
The expression a = a + 1; can written using increment operator as: a++; Similarly the statement a = a – 1; Is equal to following: a--;
The operator a++ first assign the value and then increment a by one. These operators can be used as: ++a; --a; In this case the increment is decrement is done before assignment.
Bitwise Inversion Operator This operator performs bitwise inversion on integral types. This operator works by converting all the 1 bits in a binary to 0s and all the 0 to 1s.
For example binary representation : 01001101 Using the ~ operator convert into following 10110010 You may notice that all the 0 bits are converted into 1s and all the 1 bits are converted into 0s.
For a positive value the result is always negative and increase the value by one. For example: ~15 returns -16 ~1128 returns -1129 ~0 returns -1 ~8888888 returns -8888889
For a nagative value the result is always positive and decrease the value by one. For example: ~-15 returns 14 ~-1128 returns 1127 ~-1 returns 0 ~-88888 returns 88887
Boolean Complement Operator The ! Operator inverts the value of a boolean expression. So !true results into false !false results into true
Arithmetic Operators Basic arithmetic operators are addition(+), subtraction(-), multiplication(*), and division(/). All behave the same, as you would expect for all numeric types. Modulus(%) operator returns the remainder of a division operation.
Comparison Operator These are also called relational operators. They determine the relationship that one operand has to the other. The outcome of these operators is always a boolean value.
Bitwise Operator The bitwise operators provides bitwise AND, XOR and OR operations respectively. These operators can be apply to both integer types and boolean types. These operators compare each bit of first operand with the corresponding bit of the second
operand and give results according to following rules. For AND operation 1 AND 1 results 1. any other combination produces 0.
For OR operations, 0 OR 0 produces 0. any other combination produces 1.
For XOR operations, 1 XOR 0 produces 1, as 0 XOR 1 does, any other combination produces 0.
The &,^ and | behave in the same way when applied to boolean. However instead of calculating the result on a bit-by-bit basis, the boolean values are treated as single bits, with true corresponding to 1 bit and false to a 0 bit.
For AND operation true AND true results true. any other combination produces true.
For OR operations, false OR false produces false. any other combination produces true.
For XOR operations, true XOR false produces true, as false XOR true does, any other combination produces false.
Short Circuit Logical Operators The short circuit logical operators && and || provide logical AND and OR operations on boolean types similar to the & and | operators. However they have a valuable additional feature.
The ability to “short circuit” a calculation if the result is definitely known. • For an AND operation, if first operand is false, the result is false without checking the other operand. • For an OR operation, if first operand is true, the result is true, without checking the other operand.
Example int a = 5; boolean b = ( (a>8) && (a==5) ); The first expression (a>8) returns false so the second expression (a==5) never executes and false is stored in b.
Example int a = 5; boolean b = ( (a>3) | | (a==2) ); The first expression (a>3) returns true so the second expression (a==5) never executes and true is stored in b.