MSA Java (rp) Cheat Sheet

A Programming › Java cheat sheet in English by rawbeef
11,298 views 2,620 downloads 10 Dec 2017 History
3.7 avg · 0 ratings

Data Types

String
" type string here "
int
E.G: 0
boolean
true/false
char
'­x'
double
E.G 0.00

Operators (Summary)

+
Addition / String concat­enation
%
Modulus (remai­nder)
++ , --
Increment / Decrement by 1
!
Inverts boolean value
= , ==
Assigns, Equals to
!=
Does Not equal to
>=
Greater than OR Equal to
<=
Lesser than OR Equal to
&&
Condit­ional - AND
||
Condit­ional - OR

Methods

System.ou­t.p­rin­tln­(line)
System.ou­t.p­rin­tf(­format, arguments)
Helper.readDataType(string)
// For printf formatting //
%d = integer
%s = String
%f = double (.2f = 2dp)
%b = boolean

Loops / Impt Statements

while(­con­dition)
(option != 4) { code }
for(va­r,c­ond­,incre)
(int 1=0;i<­10;i++)
if / else if / else
if(con­dition) { code }
switch­(Ex­pre­ssion)
switch (choice)
Switch syntax: E.g: Input is an int Choice

Switch (choice)
{
case 1:
< code >
break;
.....
default:
< code >
break;
}

Arrays

Declaring an Array
Syntax:
Datatype[ ] nameOf­Array =
new Datatype[ No. of elements in array ];

Example:
int [ ] random­Values = new int [ 7 ];
// Creates an array called random­Values with 7 elements (0-6)


Assigning Value to Array: (Using prev E.G)
random­Val­ues[3] = 100;
// Assigns value of 100 to the 4th element [3] of random­Values array


Declaring & Initia­lizing @ Same Time
int[ ] random­Values = {5,12,­51,­23,­12,­24,21};
// Creates an array called random­Values and assigning 7 elements in it in a single line.


Accessing Elements:
System.ou­t.p­rin­tln­(ra­ndo­mVa­lue­[3]);
// Prints out "­23" (prev example)


Finding out Array Length
System.ou­t.p­rin­tln­(ra­ndo­mVa­lue.le­ngth);
// Prints out 7

Values for Primitive Arrays

int
0
double
0.0
boolean
false
String
null

String Methods

charAt­(index)
Returns char @ index
endsWi­th(­suffix)
if ends w suffix
equals­Ign­ore­Cas­e(s­tring)
length()
Returns length of string
starts­Wit­h(p­refix)
if starts w prefix
toUppe­rCase()
Converts to upperCase
toLowe­rCase()
Converts to lowerCase

Class Diagram

Class Diagram
'­+' = Public , '­-' = Private
underlined = Static

// Constr­uctors have the same name as class.
// if return type is '­voi­d' , no return statement is required.

Creation of Class

// Using the Class Diagram above //
Declare Fields First:
public String licenseNo, colour;
public int speed;

Constr­uctor
[Right­-click, Source, Create constr­uctor using Fields, delete the super(); ]

Create Methods:
public void accele­rat­e(int acc)
{
}

public void honk ()
{
}
// Creating Array/ Object in Main Class //

Array:
Syntax:
ClassName[ ]arrayName = new ClassName[x];

E.g:
Car[ ] testArray = new Car[5]

Objects
E.g:
Car newObject = new Car();
// Creates a new object, called '­new­Obj­ect­'.

Calling a method in Main class from another class
newObj­ect.me­tho­dNa­me();

Badges

Nice Cheat Sheet Good Cheat Sheet

Comments

No comments yet.

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