Java
CHEAT SHEET
Basic code structure
text file named HelloWorld.java
| mame
’ main() method
public class HellWorld
q
public static void main(String[] args)
t
System.out.print(“Hello, World”) ;
System. out.print1n();
t Statements f
body
put and outpu
use any editor to type javac HelloWorld. java type java Hellovorld
create your program to compile your program to compile your program
t ' '
editor »Hellovorld.java compiler »Helloworld.class~ JVM -»“Hello, World”
t { 4
your program computer-language
s output
(a text file) version of your program a
1 type
pe set of values common operators sample literal values
int intergers +24 % 99 -12 2147483647
double floating-point numbers +7 */ 3.14 -2.5 6.022e23
boolean boolean values a ||! true false
char characters Le In
String sequence of characters + *aB" "Hello" "2.5"
Assignment status
declaration statement
: values true or false
variable name (ENE As D3) tierat
a = (1234 literals true false
assignment Bee 99%
statement = 995 ‘
ee ee epee) eee
operators && lI !
combined declaration
and assignment statement
Comparison
op meaning true false
equal 2 2 2 == 3
l= not equal alt= 2 2 1= 2
< less than 2< 13 2<2
<= less than or equal 2<=2 3<=2
» greater than 13 >2 2 > 413
>= greater than or equal =3 >= 2 2 >= 3
Loops
initialization is a oop declare and initialize
Seperate statement contintation —jniialication another 12° control variable
condition variable in a loop
intvea; ¢ tet “Condition itorgment
while ( v <= N/2 ) int v= 1;
braces are ¢ for (int i = 0; i <=N; i++ )
reel, A aaa
starement~Y system.out.printin(i + =“ + v);
body v = 245
} 4
body
Functions
signature return method argument argument
type name ‘ype variable
=
public static double sqrt ( double c )
{
if (c < @) return Double.NaN;
variables] Hou err = le-15;
[ doubike is = c;
method __» while (Math.abs(t - c/t) > err * t)
ka t= (c/t +t) / 2.0;
return t;
eall on another method
‘return statement
PSG e RE ee cers
os ep ate Redeye
Pes eet acuta