0% found this document useful (0 votes)
8 views

Java cheat sheet

The document outlines data types in programming, distinguishing between primitive types (like byte, int, float) and non-primitive types (like Class, String, Array). It also discusses naming conventions for identifiers, class names, object names, variables, constants, and method names, emphasizing clarity and adherence to standards. Additionally, it provides an overview of operators used in programming, including their descriptions and examples.

Uploaded by

2mibonmi2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java cheat sheet

The document outlines data types in programming, distinguishing between primitive types (like byte, int, float) and non-primitive types (like Class, String, Array). It also discusses naming conventions for identifiers, class names, object names, variables, constants, and method names, emphasizing clarity and adherence to standards. Additionally, it provides an overview of operators used in programming, including their descriptions and examples.

Uploaded by

2mibonmi2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PALOS, BONROD ZILDIAN L.

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

Name Description Range Size


Class data type that acts as a blueprint for creating Varied Varied
objects
String represents a sequence of characters, allowing for Any valid sequence 2 bytes
text manipulation of characters per char
Array stores multiple values of the same data type in a Limited by memory Varied
sequence
Inteface a reference type that defines a contract of classes, N/a Varied
containing abstract methods.
Enum defines a fixed set of constants Limited to defined Varied
constants

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}

B. Identifiers and standard naming conventions


Identifiers
They can start with letters, underscore, or dollar sign but not with a digit. They can contain a
digit after a valid character.($amount, _watt, num1).
They are case-sensitive, Identifier and iDEnTifieR are different from each other.
Can’t be the same as Java reserved keywords.
There is no strict limit on the length, but it’s advisable to be concise while maintaining
meaning.
Avoid abbreviations, Instead of using ctx use context

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
^= &= |= <<= >>=
>>>=

You might also like