Lecture02-Java Class Basics
Lecture02-Java Class Basics
TO JAVA
Java Basics
Key Words
■ Key words in the sample program are:
– public
– class
– static
– void
– String
■ String is not really a key word but is the name of a
predefined class in Java
■ We’ll go over the difference between these later
■ Key words
– lower case (Java is a case sensitive language).
– cannot be used as a programmer-defined identifier.
3
The Java Keywords
5
Lines vs Statements
6
Capitalization
■ Case matters!
7
Identifiers
• Identifiers are used for
• class names,
• method names,
• variable names.
• Java is case-sensitive
• VALUE is a different identifier than Value.
Identifiers
Valid identifiers are:
■AvgTemp , count , a4 ,$test , this_is_ok
■ () Parentheses
– Used to contain lists of parameters in method
definition and invocation. Also used for defining
precedence in expressions, containing
expressions in control statements, and
surrounding cast types.
■ {} Braces
– Used to contain the values of automatically
– initialized arrays. Also used to define a block of
code, for classes, methods, and local scopes.
■ [] Brackets
– Used to declare array types. Also used when
dereferencing array values.
Separators
• ; Semicolon
– Terminates statements.
• , Comma
– Separates consecutive identifiers in a variable
declaration.
– Also used to chain statements together inside a fo
statement.
• . Period
– Used to separate package names from subpackage
and classes. Also used to separate a variable or
Escape Sequence Description
String Literals
“Hello World”
“two\n lines”
“\”This is in quotes\””
Data Types
15
Primitive variable types
■ Java has 8 (or so) primitive types:
– float
real numbers
– double
– boolean two values: true and false
– char a single character
– byte
– short
integer numbers
– int
– long
17
Primitive integer types
■ Consider a byte:
0 1 0 0 0 1 0 1
1 byte = 8 bits
Each bit has two possibilities: 0 or 1
28 = 256
Thus, a byte can have any one of 256 values
18
Primitive integer types
19
Primitive character type
20
Primitive boolean type
■ The boolean type has only two values:
– true
– false
21
Java operators
■ The following are the common operators for ints:
– +-/*%
– Division is integer division
■ 6 / 2 yields 3
■ 7 / 2 yields 3, not 3.5
■ Because everything is an int, the answer is an int
– Modulus is %
■ Returns the remainder
■ 7 % 2 yields 1
■ 6 % 2 yields 0
22
Java operators
■ Booleans have their own operators
– && is AND
■ Only true when both operands are true
■ true && true yields true
■ false && true yields false
– || is OR
■ True when either of the operands (or both) are true
■ true || false yields true
■ false || false yields false
– ! is NOT
■ Changes the value
■ !true yields false
■ !false yields true
23
Variables
Defining variables
■ We’ve seen variables before in math
– y = mx + b
– Here y, m, x, and b can hold any value
■ To store things in a computer program, we also use
variables
■ Example:
– int x = 5;
– Visualization:
– This defines an integer variable with value 5 x 5
■ The variable is x
■ The type is int
25
Basic Variable Declaration
Basic form of variable declaration:
More on variables
■ The variable is d
■ The type is double
27
Primitive variable
assignment
■ Assignment operator =
– Allows the memory location for a variable to be
updated
Assignment operator =
Allows the variable to be updated
– Consider
int j = 11;
j 1985
11
Consider
int j = 11;
j = 1985;
28
Primitive variable
assignment
Consider
a 1
5
int a = 1;
int aSquared = a * a; 25
1
a = 5; aSquare
aSquared = a * a; d
Consider
i 0
1
int i = 0;
i = i + 1;
Consider
int asaRating; asaRati 400
-
asaRating = 400; ng
29
Primitive variable
assignment
Consider
double x = 5.12; x 19.28
5.12
double y = 19.28;
double rememberX = x;
y 19.28
5.12
x = y;
y = rememberX;
rememberX 5.12
30
Printing variables
– int x = 5;
– System.out.println (“The value of x is “ + x);
■ Important points:
– Strings are enclosed in double quotes
– If there are multiple parts to be printed, they
are separated by a plus sign
31
public class SolvingABC {
// calculate results
double result1 = d * a;
Note that I don’t double result2 = c + 2 * a;
show a lot of double result3 = d - b / c;
comments so that double result4 = c * b % c;
double result5 = b / 2;
the code will fit on
a single slide // display the results
System.out.println();
Also note all the System.out.println("result1 : " + result1);
semi-colons System.out.println("result2 : " + result2);
System.out.println("result3 : " + result3);
System.out.println("result4 : " + result4);
System.out.println("result5 : " + result5);
System.out.println();
}
}
Variable initialization
– int x;
– x = 5;
– int x = 5;
33
You can only declare
variables once
■ The following code will not work:
– int x = 5;
– int x = 6;
34
Let’s Practice
// display result
}
} 37