Skip to content

Instantly share code, notes, and snippets.

@kevinAlbs
Created October 30, 2013 20:31
Show Gist options
  • Save kevinAlbs/7239700 to your computer and use it in GitHub Desktop.
Save kevinAlbs/7239700 to your computer and use it in GitHub Desktop.

##Arrays

###How to declare:

int [] myArr = new int[10];

int [] myArr = {1,5,0,10};

(not recommended, but valid)

int myArr[] = new int[10]

Arrays are accessed with zero based indices. The first element is accessed with:

myArr[0] //first element

###Iteration

Regular way of iterating all elements of an array:

int [] myArr = {1,2,3};

for(int i = 0; i < myArr.length; i++)
{
  System.out.println(myArr[i]);
}

*Remember, in contrast to the String class, accessing the number of elements of an array is done with the length property (not method) E.g.

myArr.length //correct
myArr.length() //incorrect

##Classes/Objects Constructors

Class constructors are like class methods except there is no return type:

public class MyClass{
  public MyClass(){
  
  }
  
}

You can overload constructors:

public class MyClass{
  public MyClass(){
  
  }
  public MyClass(int param){
  
  }
  //...etc
}

I don't think I mentioned this in recitation but constructors are optional, and if you do not include one Java will automatically include the blank constructor. For example:

public class MyClass{
}

and

public class MyClass{
  public MyClass(){
  }
}

are equivalent.

##Passing by reference vs Passing by value Both primatives and the string class are passed by value, while objects and arrays are passed by reference. ###Passing by Value Passing by value means the value of the passed variable is copied. For example, say we have the following snippet of code:

public static void myFunc(int i){
  i += 2;
}

public static void main(String[] args){
  int x = 5;
  myFunc(x);
}

Since int is a primitive, when we call myFunc Java copies the value of x (5) to i. When i is incremented by 2, the value of x remains to be 5. ###Passing by Reference In contrast, when you pass an array or object, that variable is passed by reference. This means both the variable you pass, and the parameter that receives the variable are both pointing to exactly the same object in memory. For example:

public static void myFunc(int[] arr){
  arr[0] = 6;
}

public static void main(String[] args){
  int[] x = {1,2,3};
  myFunc(x);
}

Now since arrays are passed by reference, setting arr[0] to 6 in the function also sets x[0] to 6 since they are pointing to the same location in memory.

###Some Important Notes

A common error people make is confusing the variable and what it points to.

Take this example:

public static void myFunc(int[] arr){
  arr = new int[5];
  arr[0] = 1;
}

public static void main(String[] args){
  int[] x = {1,2,3};
  myFunc(x);
}

Now, we know initially when we call the method, since arrays are passed by reference arr points to the same array that x does. However, once we reach the line: arr = new int[5]; the variable arr now references a new array of size 5. Since arr no longer points to the same array that x does, modifying arr has no effect on x. In this example x is the same before and after the function call.

###Strings

tl;dr Strings are in effect pass by value, but technically pass by reference.

Renish brought up a good point about Strings which is seemingly strange behavior, but makes sense if you understand how the string class works.

Strings are objects, so they are passed by reference. I said in class they were passed by value... I am ashamed. But effectively it's almost as if they are passed by value. This is because Java strings are immutable. You can read up on it here and a little bit down you'll see:

Note: The String class is immutable, so that once it is created a String object cannot 
be changed. The String class has a number of methods, some of which will be discussed 
below, that appear to modify strings. Since strings are immutable, what these methods 
really do is create and return a new string that contains the result of the operation.

What this means is that any time you modify a string, internally Java will create a new string. For example:

String s = "hello";
s = s + " world";

At first, our String s points to a string object containing "hello". When " world" is added, internally Java will create a new string with the contents "hello world" and discard the old object containing just "hello".

So, in regards to passing strings to methods, take the following example:

public static void myFunc(String param){
  //...
}

public static void main(String[] args){
  String s = "hello";
  myFunc(s);
}

Now when we pass s to myFunc, param will actually reference the same exact String object as s. However, anything we do to modify param will in effect create a new String and thus leave s unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment