Java Basics1
Java Basics1
COMPUTER SCIENCE I
JAVA BASICS
JAVA PROGRAMS
Class Header
Method Header
Keywords
Comment
Statement
CLASS HEADERS
Keywords
public
Access specifier – controls where the class may be accessed from
class
Indicates the beginning of a class definition
Case sensitive
Curly Braces
METHOD HEADERS
Main method header
Every Java application must have a method named main
Starting point of application
COMMENTS
Short notes intended for programmers
Provide better understanding of the code
Ignored by compiler
STATEMENTS
A executable snippet of code that represents a complete command
Terminated by a semicolon
SEMICOLONS
Do not put a semicolon at the end of:
Comments
Class headers
Method headers
Braces
STYLE AND READABILITY
Indentation
White space
Comments
Curly Brace Placement
OUTPUT
PRINTLN
Part of Java’s Application Programmer Interface (API)
System Class
out object
println method
Example:
“This statement”
produces 3
lines of output
ESCAPE SEQUENCES
Common Escape Sequences
Can these lines be rewritten while still using the print method and achieve the
desired effect?
QUIZ TIME!
Which of the following is the correct way to output a message?
a. System.out("Programming is fun!");
b. System.println("Programming is fun!");
c. System.out.println("Programming is fun!");
d. System.out.Println("Programming is fun!");
Literal
Value written into the code of a program
IDENTIFIER RULES
Must begin with a letter
Can only contain the following:
Letters
Numbers
Underscore
Examples:
thisIsAVariable
this_isaVariable
thisis1Variable
variable1
QUIZ TIME
Are the following legal identifier names?
week day
1997june
wee7Day
day_of_the_week
week@day
carTestTrack
JAVA NAMING CONVENTIONS
Class names begin with a capital letter
Method and variable names begin with lowercase letters
Try to limit to one word
If you must use more than one word, capitalize the first letter of each word
after the first word
Constants have uppercase letters and the words are separated by
underscores
Examples:
AllMyChildren //class
allMyChildren //variable or method
ALL_MY_CHILDREN //constant
PRIMITIVE DATA TYPES
Name for a category of data values that are all related
= operator
Stores the value on the right to the variable on the left
WATCH OUT!
42 = age; is not the same as age = 42;
Remember:
The assignment operator assigns the value on the right to the
variable on the left
It doesn’t work the other way around
PRINTING VARIABLE VALUES
Digits only!
PRINTING WITH FORMATTING
No commas or dollar signs in variable declarations, but we can print
them when we print the variable.
Printf method
SCOPE
Part of the program where it may be accessed by name
Begins for variables when they are declared/initialized and ends when
the method it is in ends
ARITHMETIC
OPERATORS
Three types
Unary
Binary
Ternary
Table of Binary Operators
Operator Meaning Example
amount = 4 + 8;
markUp = 12 * 0.25;
INTEGER DIVISION
If both operands are integers, the result will also be an integer
Example:
PRECEDENCE & ASSOCIATIVITY
Operator Precedence 5+2*4
Negation
2+3*4–6
Multiplication, Division, Modulus
22 + 4 * 2
Addition, Subtraction
Associativity 4 * 3 / 8 + 2.5 * 2
Negation – right to left
All other operations, left to right
COMPOUND OPERATORS
balance = balance + deposit;
balance = balance – withdrawal;
number = number / 2;
number = number * 2;
number
12
name
address
“Jillian”
STRING METHODS
Method Name Description Example
Data type