Final- Java Basics ppt Flashcards
Sample program
public class NameWithSameNameAsFile {
public static void main (String[] args)
{
System.out.println(“Happy Thanksgiving!”);
} //end main
} //end class
data types (5)
int, double, boolean, char, String
int
whole numbers
double
numbers with a decimal point
boolean
true or false
char
use ‘ ‘ to enclose a single character
String
Not a primitive type, notice the capital S. Use “ “
For naming, the first character can be
a letter, underscore, or $
Subsequent characters may be
letters, numbers, underscores, or $
variable names cannot be
keywords
Are names case-sensitive?
yes.
constant names should be
ALLCAPS
variable names
shouldBeLikeThis
Variables and constants will be declared
at the beginning of the method.
Variable and constant declaration looks like this:
final int MAX_HOURS = 18; String name; int age; double gradePointAvg; char status; boolean active;
You can initialize a variable as well. This means…
to include a value for each variable with the declarations. Example: String name ="Spongebob”; int age = 5; double gradePointAvg = 3.0; char status = ‘C’; boolean active = true;
normal arithmetic operators
*
division
/
If both operands int, returns an int.
If at least one operand double, returns a double.
Modulus
%
Remainder or leftovers from division.
3%10=
1
5/10=
0
5.0/10=
0.5
used for input from the keyboard
Scanner class
to let Java know that you are going to use the Scanner class
add import statement to top of program