AP CSA Java Unit 1 Cheat Sheet

A Programming › Java cheat sheet in English by KETHEO

AP CSA Java by unit coverage.

10,206 views 1,490 downloads 26 Oct 2021 History

INPUT & OUTPUT

INPUT
To use Scanner, import Scanner library:
import java.O­bje­ct.S­canner
Scanner scan = new Scanne­­r(­S­y­st­­em.in);
name = scan.n­­ex­t­I­nt();
name = scan.n­­ex­t­L­ine();
name = scan.n­­ex­t­D­ou­­ble();
OUTPUT
System.ou­­t.p­rint ( "­­He­l­l­o" );
System.ou­­t.p­­r­intln ( "­­He­l­l­o" ); // New line.
COMMENTS
// A single line comment.
/*
* Multiple
* line comments.
*/

JAVADOC

packag­e-i­nfo.java //Need to generate.
/**
* Javadoc comments generate a HTML file.
* @author
* @version
*/

VARIABLES

PRIMITIVE DATA TYPES
int value = null; //Does­n't compile.
byte
8 bits
short
16 bits
int
32 bits
long
64 bits
char
Textual: (16 bits)
boolean
Logical: true/false
float (decimal)
Floats 15 places (32 bits)
double (decimal)
Floats 15 places (64 bits)
FINAL VARIABLES
Permanent Values
final double PI = 3.1415­9265;
final String LANGUAGE = "­Jav­a";

KEYWORDS

abstract
final/ly
public
assert
float
requires
boolean
for
return
break
goto
short
byte
if
static
case
implements
strictfp
catch
import
super
char
instanceof
switch
class
int
synchr­onized
continue
interface
this
const
long
throw/s
default
module
transient
do
native
true
double
new
try
else
null
var
enum
package
void
exports
private
volatile
extends
protected
while
false

STRINGS

REFERENCE DATA TYPE
String name = Always within quotes;
String words = "123 have no value";
STRING CONCAT­ENATION
System.ou­­t.p­ri­ntl­n("Hello " + variable + "." );
System.ou­­t.p­­rint (variable + " Hello World");
CONCAT­ENATION RULES
number + number = number
number + String = number + String
String + number = String
Above number changed to a String!
String + (number) = String + number
Use ( ) to override String change.

CALCUL­ATIONS

COMPOUND ASSIGNMENT OPERATORS
Syntax to assign arithmetic operation result back-into "­ass­ign­men­t" variable.
+=
Add values
-=
Minus values
*=
Multiply values
/=
Divide values
%=
Modulus (remai­nder)
++
Adds one
--
Subtracts one
x = x + 1;
x += 1;
x++;
//adds one to x
//adds one to x
//adds one to x
MODULAR DIVISION (%)
REMAINDER
Only works on integers!
7 % 2 =remainder 1
8 % 3 =remainder 2
Find even numbers
x % 2 = remainder 0
Find odd numbers
x % 2 = remainder 1

MINIMUM & MAXIMUM

Integer.MIN_VALUE Intege­r.M­AX_­VALUE
 2147483647 -2147483648

NAMING CONVEN­TIONS

Package
java, lang, shapes, etc...
packag­e.name, java.io, etc...
com.ex­amp­le.m­yp­ackage, etc...
Lowercase letters.
Separate words with dots (.)
Companies: "­rev­ersed Internet domain­" starts their package name.
Class
Color, Button, Cats, etc...
Cambri­dge­Cit­ySc­hools, etc...
NOUNS: Each word starts with an uppercase letter.No acronyms.
Method
main(), print(), equals(), etc...
newMet­hod(), toString() etc...
VERB: First word lowercase.
Adding words: First letter uppercase.
Variable
area, name, size, etc...
newTri­angle, rightSide, etc...
IDENTIFIER: first word lowercase.
Adding words; first letter uppercase.
No keywords.
No special characters to start.
Avoid one letter: x, y, z, etc.
Final Constant
PI, BLUE, MATH, etc...
MAX_PR­IORITY, CITY_ZIP, etc...
All uppercase letters: Every word separated with an unders­cor­e(_).

TYPE CASTING

(double) 1/3 = 0.3333
Results a double
int value = (int) 4.7
Narrowing Cast
(int) 3.86 = 3
Truncated
WIDENING & NARROWING CASTING
Does not round: Truncates!
byte→short→char→int→long→float→double
Narrowing Casting ( manually )
 
larger smaller
Widening Casting ( automatic )
 
smaller larger
ROUNDING
int nearestInt = (int) (number + 0.5)
int neares­­tN­egInt = (int) (negNumber – 0.5)

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.