Notes for Learn Java @ Codecademy

I used the Learn Java modules from Codeacademy to refresh my Java knowledge.

Notes:

The precedence of each Boolean operator is as follows:

  1. ! is evaluated first
  2. && is evaluated second
  3. || is evaluated third

ternary conditional statement:

char canDrive = (fuelLevel > 0) ? ‘Y’ : ‘N’;

Switch

int subwayTrain = 5;

switch (subwayTrain){

case 1 : System.out.println(“This is a South Ferry bound train!”);
break;
case 5 : System.out.println(“This is a Brooklyn bound train!”);
break;
case 7 : System.out.println(“This is a Queens bound train!”);
break;
default:
System.out.println(“I’m not sure where that train goes…”);

}

By the way, I found switch statement very powerful combined with finite state machine (FSM) when doing my Arduino course projects.

Object Oriented Programming

structure:

class Name extends AnotherClass{

instance variable

constructor (parameters)

method (void, or has return value)

main{

create new object;

call method;

}

}

ArrayList

Declare an ArrayList object storing integers:

ArrayList<Integer> listName = new ArrayList<Integer>();

Add 3 to the list:

listName.add(3);

Get a value at index 0:

listName. get(0);

Insert a new value 22 to index 2:

listName.add(2,22);

the size of list:

int size = listName.size();

for each loop:

for (Integer value : listName){

}

HashMap

Declare a HashMap object (store String keys and Integer values):

HashMap<String, Integer> name = new HashMap<String, Integer>();

the size of HashMap:

int size = name.size();

Add keys and values:

name.put(“key”, 1);

Get value using key:

name.get(“key”);

for each loop:

for (String item: name.keySet()) {

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s