|
Java-Concepts-Java-Basics Seminar
|
|
12-14-2010, 04:26 AM
Post: #1
|
||||
|
||||
|
Java-Concepts-Java-Basics Seminar
Basic Java Syntax and Semantics
Methods, Messages, and Signatures Classes implement methods, and objects are instances of classes. Objects that share common behavior are grouped into classes. An object responds to a message only if its class implements a corresponding method. To correspond the method must have the same name as the message. Messages are sometimes accompanied by parameters and sometimes not: pen.move(); // No parameter expected pen.moveDown(8); // One parameter expected Basic Java Syntax and Semantics Some methods return a value and others do not. To use a method successfully we must know: What type of value it returns It’s name (identifier) The number and type of the parameters it expects This information is called the method’s signature. Basic Java Syntax and Semantics Programming Protocols: Use camelCase When forming a compound variable name, programmers usually capitalize the first letter of each word except the first. (For example: taxableIncome) All the words in a program’s name typically begin with a capital letter (ComputeEmployeePayroll). Constant names usually are all uppercase (TAX_RATE). Comments Comments are explanatory sentences inserted in a program in such a matter that the compiler ignores them. There are two styles for indicating comments: End of line comments: These include all of the text following a double slash (//) on any given line; in other words, this style is best for just one line of comments Multiline comments: These include all of the text between an opening /* and a closing */ Additional Operators Extended Assignment Operators The assignment operator can be combined with the arithmetic and concatenation operators to provide extended assignment operators. For example: int a = 17; String s = "hi"; a += 3; // Equivalent to a = a + 3; a -= 3; // Equivalent to a = a – 3; a *= 3; // Equivalent to a = a * 3; a /= 3; // Equivalent to a = a / 3; a %= 3; // Equivalent to a = a % 3; s += " there"; // Equivalent to s = s + “ there”; Additional Operators Increment and Decrement Java includes increment (++) and decrement (--) operators that increase or decrease a variables value by one: int m = 7; double x = 6.4; m++; // Equivalent to m = m + 1; x--; // Equivalent to x = x – 1.0; The precedence of the increment and decrement operators is the same as unary plus, unary minus, and cast. Standard Classes and Methods Eight methods in the Math Class Standard Classes and Methods Using the Math class double absNum, powerNum, randomNum; absNum = Math.abs(-30); powerNum = Math.pow(-3, 3); randomNum = Math.random(); Results: absNum = 30 //Absolute value of -30 powerNum = -27 //-3 to the 2nd power randomNum = ????? //Random number between 0 and 1 |
||||
|
« Next Oldest | Next Newest »
|
![]() |
|||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
User(s) browsing this thread: 1 Guest(s)
Search
Member List
Calendar
Help




![[-] [-]](images/collapse.gif)






