1 / 58

JAVA

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.

mandy
Download Presentation

JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA

  2. Data Types

  3. 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

  4. Integer Types

  5. Floating Point Numbers

  6. 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.

  7. Boolean Java has a simple type called boolean, for logical values. It can have only one of two possible values.

  8. Variables

  9. 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

  10. 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;

  11. NOTE : All the integer types are considered int type by default. All the floating point types are considered double by default.

  12. 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.

  13. 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.

  14. So followings are valid identifiers: - _minute $total Sum4 Followings are invalid identifiers: - &total Minute# 4hour

  15. 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

  16. 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;

  17. Keywords

  18. 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.

  19. Operators

  20. 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

  21. 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;

  22. Ternary Operators are that operators which require three operands to perform calculation. There are only one ternary operator in java. ?: (5>3) ? Value1 : Value2

  23. Java Unary Operators Java Binary Operators Arithmetic Operators Shift Operators

  24. Comparison Operator Bitwise Operators Short Circuit Logical Operators

  25. Assignment Operators

  26. 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.

  27. The expression a = a + 1; can written using increment operator as: a++; Similarly the statement a = a – 1; Is equal to following: a--;

  28. 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.

  29. Examples

  30. 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.

  31. 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.

  32. 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

  33. 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

  34. Boolean Complement Operator The ! Operator inverts the value of a boolean expression. So !true results into false !false results into true

  35. 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.

  36. 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.

  37. 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

  38. operand and give results according to following rules. For AND operation 1 AND 1 results 1. any other combination produces 0.

  39. For OR operations, 0 OR 0 produces 0. any other combination produces 1.

  40. For XOR operations, 1 XOR 0 produces 1, as 0 XOR 1 does, any other combination produces 0.

  41. 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.

  42. For AND operation true AND true results true. any other combination produces true.

  43. For OR operations, false OR false produces false. any other combination produces true.

  44. For XOR operations, true XOR false produces true, as false XOR true does, any other combination produces false.

  45. 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.

  46. 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.

  47. 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.

  48. 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.

More Related