02.java Basics
02.java Basics
02.java Basics
file: HelloWorld.java
void
main(String[
] args
){ System .
out.println("Weirdly-spaced code still runs!"
);}}
Bad Whitespace Example #2
Valid, but horribly written, code.
(one-liners aren't always best!)
Code like this might not receive any credit! Seriously, don't do this in
anything you ever turn in. Never make the grader unhappy.
Good Whitespace Example
public class GoodSpacing {"
public static void main (String[] args) {"
int x = 5;"
int y = 12;"
System.out.println("x+y = "+ (x+y));"
}"
}
1-7
Identifiers
Identifiers cannot be keywords
By convention, programmers use different case
styles for different types of identifiers, such as
lower case for variables
response, previousIndex
title case for class names
Person, MasonStudent
upper case for constants
MASON, MAX_INT
Identifier Examples
Legal Identifier Examples: Illegal Identifier Examples:
decimal: start with 1-9, followed by 0-9. (or just the 0 value.)
0 10 483 9876501234 66045
3.14 double
7e200 double
We must have all three parts, and the 2nd and 3rd
expressions' types must agree with (fit in) the
resulting type of the entire expression.
Expression Examples
Legal:
4+5 (3>x) && (! true)
x%2==1 (x<y)&&(y<z)
numPeople drawCard()
(2+3)*4 y!=z