Identifiers:
-Start with a letter.
-Then letters, numbers or ‘_’
-No spaces
-No Reserved Words
-Case sensitive
Naming Conventions:
-Use names not letters (i.e. instead of ‘g’ we use ‘grade’)
-Variable start lowercase
--If more than one word, first word is not capitalized every word after that is (ex. studentGrade, studentInClass)
-Class start uppercase
-All capitals only used for constants (ex. PI)
Data Types:
int (integer)
-May not hold decimals. If an integer is assigned to ‘3.14’ the value will be displayed as ‘3’.
double (floating-point number)
-Larger in memory than an integer. May hold decimals or integers. If double is assigned to ‘3’ the value will be displayed as ‘3.0’.
char (character)
-Must be defined with quotes ‘’ around the value. (ex. ‘value’). Can hold any character on the keyboard.
boolean (true or false)
-Can only hold the value ‘true’ or ‘false’
Variables:
How to Declare and Assign Variables:
int grade; //declaration grade = 11; //assignment //or int grade = 11; //combining declaration and assignment //or int a,b; //creates a integer ‘a’ and and integer ‘b’
int = variable type
grade = variable
value of grade = 11
Math Operators:
Computer knows order of operations.
double y = 7.0; double answer; answer = 3.0 + 5.0; answer = y – 2.5; answer = y * 3.2; answer = y / 2.0; /*‘answer’ would only equal the value of 3.5 The computer goes down the list and ignores previous assignments.*/
double a ,b = 5.0; //creates a blank double, ‘a’ and creates a double with the value of 5.0, ‘b’ int x = 2; a = b / x; a = 2.5 a = (int) b / x; a = 2.0. /*This is because b is converted into an integer for this equation. 5/2, an integer/integer, will result in an integer answer. The real answer is 2.5 but it discards the decimal making it 2. a is a double however, so the value of a is 2.0*/
Modulus Operator = %
-Only works with integers
int a = 7; int b = 2; int c; c = a % b; c = 7 % 2 c = 1Printing:
System.out.println ( “bla” ); //prints out “bla” System.out.println ( c ); //prints out the value of the variable c System.out.println ( “c = “ + c); //prints out “c = “ and the value of c
Assignment Operators:
int a = 7; a = a + 5 //adds 5 to the value of a a = 12
0 comments:
Post a Comment