Java cheat sheet
Java cheat sheet
BSIT-2A
A. Data types
a. Primitive
Name byte Description
byte 1 stores whole numbers from -128 to 127
short 2 stores whole numbers from -32,768 to 32,767
int 4 stores whole numbers from -2,147 ,483,648 to 2,147,483,647
long 8 stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 stores true or false values
char 2 stores a single character/letter or ASCII values
Examples
byte myByteNum=127;
short myShortNum=32767;
int myNum=2147483647;
long myLongNum=9223372036854775807L;
float myFloatNum=5.99F;
double myDoubleNum=5.123456789012345;
boolean myBool=true;
char myChar=’%’;
b. Non-primitive
Examples
public class Bike{}
String myString=”text”;
int[] arr={1,2,3,4}; or int[][] arr=new arr[r][c];
public interface MyInterface {void myMethod();}
public enum Day{SUNDAY, MONDAY, TUESDAY,
WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
a. Class Names
They should be written in CamelCase, where each word starts with a capital letter.
(StudentDetails, EmployeeRecord)
It shouldn’t include special characters. You can use ‘_’ and ‘$’ but it’s generally avoided.
b. Object Names
Written in lowerCamelCase like myObject
Objects should by nouns since they represent entities(employee, book)
When naming collections, lists or arrays, use plural nouns
(students, orderList)
Some devs might use prefixes like ‘obj’. Instead of sing objCar just use car
c. Variables
Description Example
Use lowerCamelCase myVariable
Describe the variable purpose itemPrice
Use nouns accountBalance
Avoid abbreviations unless widely amt -> amount
recognized
Avoid using special characters $amount -> cashAmount
d. Constants
Use uppercase letters with underscores. Declare
them as ‘public static final’ so that they can be accessed from anywhere in the code.
public static final int MAX_LEVEL =100;
public static final String COMPANY_NAME = ”Sylph co.”;
public static final double PI = 3.14159;
e. Method names
Standards Example
use lowerCamelCase for calculateTotal()
method names
names should be verbs getUserName()
that describe the method’s
action, should be
descriptive
avoid abbreviations calc() -> calculate()
C. Operators
Operator Description Associativity Examples
. used for accessing
methods and
variables within
objects and classes
[] used for arrays
() groups expressions
++ --
!
~
instanceof returns true or “foo” instanceof String //
false based on true Point pt = new
whether the object is Point(10,10); pt instanceof
an instance of the String // false
named class or any
of that class’s
superclasses
new the new operator is
(type)expression used for creating new
instances of classes
* / % multiplications,
division, modulus
+ - addition, subtraction
<< >> >>> bitwise left and right
shift
< > <= >= relational comparison
test
== != equality
& AND
^ XOR
| OR
&& Logical AND
|| Logical OR
? : Shorthand for
if...then...else
= += -= *= /= %= Various assignments
^= &= |= <<= >>=
>>>=