Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Structure import java_packages; class JavaClass { ------ member variables declarations; ------ void otherMethod( ) { ------ } public static void main(String[]

Similar presentations


Presentation on theme: "Java Structure import java_packages; class JavaClass { ------ member variables declarations; ------ void otherMethod( ) { ------ } public static void main(String[]"— Presentation transcript:

1 Java Structure import java_packages; class JavaClass { ------ member variables declarations; ------ void otherMethod( ) { ------ } public static void main(String[] args) { ------ local variables declarations; ------ }

2 Java Structure Example: public class BasicsDemo { public static void main(String[] args) { int test1 = 10; int test2; test1 += 5; test2 = test1 + 15; System.out.println(“Test 1 = " + test1); System.out.println(“Test 2 = " + test2); }

3 Variables Definition: A variable is an item of data named by an identifier. –You must explicitly provide a name and a type for each variable you want to use in your program. –The variable's name must be a legal indentifier An unlimited series of Unicode characters that begins with a letter. –You use the variable name to refer to the data that the variable contains. –The variable's type determines what values it can hold and what operations can be performed on it.

4 Variables Declaration A variable declaration: var_type var_name = var_initialization ; Example: MaxVarDemo.java

5 Data Types Every variable must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. The Java programming language has two categories of data types: –Primitive –Reference A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean. The value of a reference type variable is a reference to (an address of) the value or set of values represented by the variable: an arrays, a class, and an interface.

6 Primitive Data Types KeywordDescriptionSize/Format (integers) byteByte-length integer8-bit two's complement shortShort integer16-bit two's complement intInteger32-bit two's complement longLong integer64-bit two's complement (real numbers) float Single-precision floating point 32-bit IEEE 754 double Double-precision floating point 64-bit IEEE 754 (other types) charA single character16-bit Unicode character boolean A boolean value (true or false) true or false

7 Literal Values A literal primitive value can directly put in the code. Example:int anInt = 4; The digit 4 is a literal integer value. LiteralData Type 178 int 8864L long 37.266double 37.266D double 87.363Ffloat 26.77e3 double ' c '' c ' char trueboolean falseboolean

8 Variable Name In the Java programming language, the following must hold true for a simple name: –It must be a legal identifier. An identifier is an unlimited series of Unicode characters that begins with a letter. –It must not be a keyword, a boolean literal ( true or false ), or the reserved word null. –It must be unique within its scope. A variable may have the same name as a variable whose declaration appears in a different scope. By Convention : –Variable & Method names begin with a lowercase letter –Class name begin with an uppercase letter. –If a variable, method or class names consist of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible.

9 Variable Scope The location of the variable declaration within your program establishes its scope and places it into one of these four categories: –member variable –local variable –method parameter –exception-handler parameter

10 Variable Scope Member (Instance) Variable: A member variable is a member of a class or an object. It is declared within a class but outside of any method or constructor. A member variable's scope is the entire class. Local Variable: Local variables are declared within a block of code. In general, the scope of a local variable extends from its declaration to the end of the code block in which it was declared. Consider the following code sample: if (...) { int i = 17;... } System.out.println("The value of i = " + i); // error The final line won't compile because the local variable i is out of scope.

11 Variable Scope Method Parameter: Parameters are formal arguments to methods and are used to pass values into methods. The scope of a parameter is the entire method for which it is a parameter. Exception-handler Parameter: Exception-handler parameters are similar to parameters but are arguments to an exception handler rather than to a method. The scope of an exception-handler parameter is the code block between { and } that follow a catch statement.

12 Variable Initialization Local variables and member variables can be initialized with an assignment statement when they're declared. The data type of the variable must match the data type of the value assigned to it. Examples: // integers byte byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; // other primitive types char aChar = 'S'; boolean aBoolean = true;

13 Final Variable The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages. To declare a final variable, use the final keyword in the variable declaration before the type: final int aFinalVar = 0; The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value to aFinalVar result in a compiler error.

14 Final Variable A final local variable that has been declared but not yet initialized is called a blank final. final int blankfinal;... blankfinal = 0;... Once a final local variable has been initialized, it cannot be set again. Any later attempts to assign a value to blankfinal result in a compile-time error.

15 Operators An operator performs a function on one, two, or three operands. An operator that requires one operand is called a unary operator. operator operand //prefix notation operand operator //postfix notation An operator that requires two operands is a binary operator. operand1 operator operand2 //infix notation An operator that requires two operands is a ternary operator. operand1 ? operand2 : operand3 //infix notation

16 Arithmetic Operators Example: ArithmeticDemo.java Binary Operator:

17 Arithmetic Operators Example: ArithmeticDemo2.java Unary Operator:

18 Relational Operators Example: RelationalDemo.java A relational operator compares two values and determines the relationship between them.

19 Conditional Operator Relational operators often are used with conditional operators to construct more complex decision- making expressions. Conditional Operators: Example: ConditionalDemo.java

20 Shift Operators A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left. Example: 13 >> 1  6 The binary representation of the number 13 is 1101. The result of the shift operation is 1101 shifted to the right by one position  110, or 6 in decimal. The left-hand bits are filled with 0’s as needed.

21 Logical (Bitwise) Operators

22 Example: 13 & 12  12 13 & 12  13 13 & 12  1 The binary representation of the number 13 is 1101. The binary representation of the number 13 is 1100. 1101  13 1101  13 1101  13 &1100  12 | 1100  12 ^ 1100  12 ------ ------ ------ 1100  12 1101  13 0001  1 Example Program: ShiftLogicalDemo.java

23 Assignment Operators

24 Other Operators Shortcut if-else Statement The ?: operator is a conditional operator that is short-hand for an if - else statement: op1 ? op2 : op3 The ?: operator returns op2 if op1 is true or returns op3 if op1 is false. The [ ] Operator Square brackets are used to declare arrays, to create arrays, and to access a particular element in an array. Here's an example of an array declaration: float[] arrayOfFloats = new float[10]; The previous code declares an array that can hold ten floating point numbers. Here's an example to access the 7 th element of array : arrayOfFloats[6];

25 Other Operators The. Operator The dot (.) operator accesses instance members of an object or class members of a class. The () Operator When declaring or calling a method, the list of the method's arguments are put between ( and ). The (data type) Operator Casts (or "converts") a value to the specified type. Example: …… int i; float j = 10.5; i = (int) j + 11; ……


Download ppt "Java Structure import java_packages; class JavaClass { ------ member variables declarations; ------ void otherMethod( ) { ------ } public static void main(String[]"

Similar presentations


Ads by Google