PROGRAMMING 1 - CHAP 3.pdf

Document Details

HeartfeltClover9386

Uploaded by HeartfeltClover9386

Universidad de Dagupan

Tags

java programming variable declaration data types programming concepts

Full Transcript

UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan PROGRAMMING 1 Chapter 3: Mastering Java Syntax: Variables, Constants, Data Types, Comments, and Operators Variable Declaration A variable is a value that can be changed depend...

UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan PROGRAMMING 1 Chapter 3: Mastering Java Syntax: Variables, Constants, Data Types, Comments, and Operators Variable Declaration A variable is a value that can be changed depending on conditions or on information passed to the program, for example, we have a text string "Welcome Students!!!" or a variable score with a data type integer with a value 10. A variable can then be reused throughout your program, instead of having to type out the actual value all over and over again. In Java programming, you can define a variable with the following form: Data Type Variable Name and Value let say: String User_Name = "Juan Dela Cruz" The data type is String which refers to a series of characters. Our variable name is User_name and our value is "Juan Dela Cruz". Always remember that Java is case-sensitive programming language similar to C/C# and C+. Rules in Using Variables A variable can contain: 1. Any unicode character that is a letter (including numeric letters like Roman numerals) or a digit. 2. Currency sign (such as $). 3. Connecting punctuation character (such as _ ). A variable cannot: 1. Variable names are case-sensitive. 2. A variable's name can be any legal identifier. 3. It can contain unicode letter, digits and two special characters such as underscore and $ dollar sign. 4. Length of Variable name can be any number. BCNV 1 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan 5. It is necessary to use the alphabet at the start (however, we can use underscore). 6. Some auto generated variables may contain '$' sign. But try to avoid using dollar sign. 7. White space is not permitted. 8. Special Characters are not allowed. 9. A digit at the start is not allowed. 10. Subsequent characters may be letters, digits, dollar signs, or underscore characters. 11. Variable name must not be a keyword or reserved word. Constant Variables Constant variables refer to those variables whose value does not change during program execution. This type of variable is very useful if we want to assign value to a variable that is fixed. For example, final tax = 26.7; in this example we declare a variable tax with a value of 26.7. This variable becomes constant by declaring the keyword final before the name of our variable tax as we assigned a value of 26.7. Importance of Comments on Programs Comments - These are remarks and non-executable phrases or sentences in the program. 1. Program documentation contains information that improves the readability of the program. 2. Comments are part of program documentation because they help others read and understand how the program works. The compiler and interpreters, however, ignore comments and they are not translated in machine language. BCNV 2 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Identifiers Identifiers are names given to various program elements such as variables, functions and arrays. The following are the guidelines in creating identifiers in Java. 1. It consists of letters and digits without space, in any other, except that the first character must be a letter. 2. Uppercase and lowercase are permitted but they are not interchangeable. As an exception, an underscore may be used as the first character of the identifier. 3. Reserved words and standard methods must not be used. Reserved Words Reserved words are keywords that have standard predefined meaning in Java. These reserved words can be used only for intended purpose and they cannot be used as programmer's defined identifiers. BCNV 3 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Data Type This is a set of data that specifies the possible range of values of the set, the operations that can be performed on the values, and the way in which the values are stored in memory. Understanding this data type enables the computer to manipulate it appropriately. Java Operators Java provides a rich set of operators environment. Java operators can be divided into the following categories: 1) Arithmetic operators. Just like in algebra, arithmetic operators are utilized in mathematical expressions. OPERATOR DESCRIPTION + adds two operands - subtract second operands from first * multiply two operand / divide numerator by denominator % remainder of division ++ Increment operator increases integer value by one -- Decrement operator decreases integer value by one BCNV 4 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan 2) Relational operators. The table below lists all the relational operators that java supports. OPERATOR DESCRIPTION == adds two operands != subtract second operands from first > multiply two operand < divide numerator by denominator >= remainder of division Right shift BCNV 5 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Now lets see the truth table for bitwise &, !, and ^ Bitwise shift operators shift the bit value. The first (left) operand represents the value to undergo the shift, while the second (right) operand defines the number of positions to shift the bits in the value. The significance of both operands is equal. Example: a = 0001000 b= 2 a > b= 0000010 5) Assignment operators. Assignment operators supported by Java are as follows: OPERATOR DESCRIPTION EXAMPLE = The right-side operand's values are allocated a=b to the leftside operand. += The left operand is incremented by the value of the right operand, with the resultant sum at=b is same as a=a+b assigned back to the left operand. -= The left operand is diminished by the value of the right operand, and the resultant a-=b is same as a=a-b difference is assigned back to the left operand. *= The left operand is multiplied by the right operand, with the resultant product assigned a*=b is same as a=a*b back to the left operand. /= The left operand is divided by the right operand, and the resultant quotient is a/=b is same as a=a/b assigned back to the left operand. %= The modulus is computed using both operands, with the resultant remainder a%=b is same as a=a%b assigned to the left operand. BCNV 6

Use Quizgecko on...
Browser
Browser