methods,
variables
& strings
class overview
- Homework Solution
- Methods
- Variable Scopes
- Guess the Scope!
- Strings
- Immutability
- Common String Manipulation
- String Comparison
methods
- Must belong to a Class
- Arguments and their types must be declared
- { } brackets are used for designate a Method definition
public class Arithmetic { public static void main (String [] args) {
}
private static int add (int a, int b) {
}
}
methods
- Invoke a method by calling it with parentheses after it
- If the declaration has arguments, you must supply those when invoking it.
- "return" keyword used to return a result. Result type must match the declared return type.
public class Arithmetic {
public static void main (String [] args) {
int sum = add(10, 15);
System.out.println(sum);
}
private static int add(int num1, int num2) {
return num1 + num2;
}
}
methods
- Static methods can be called without referencing an Object
- Object methods should be called on an instantiated object
- Methods used in the same class may be referenced by the name only
- Static methods cannot call a non-static method without an Object reference
static method calls non-static method
public class Arithmetic {
public static void main (String [] args) {
Arithmetic a = new Arithmetic();
int sum = a.add(10, 15);
System.out.println(sum);
}
private int add(int num1, int num2) {
return num1 + num2;
}
}
method arguments
- Method arguments are evaluated before passing into the method.
- This allows for cleaner code and less variables.
public class Arithmetic {
public static void main (String [] args) {
Arithmetic a = new Arithmetic();
System.out.println(a.add(10, 15));
}
private int add(int num1, int num2) {
return num1 + num2;
}
}
method arguments
- Method arguments are passed by value.
- Any argument modifications inside a method don't affect the variable on the outside.
public class Arithmetic {
public static void main (String [] args) {
Arithmetic arithmetic = new Arithmetic();
int a = 10;
int b = 15;
arithmetic.add( a + a, b * 2 );
System.out.println(a);
}
public void add (int a, int b) {
a = a + b;
}
}
variable scope
- Scope is determined by where a variable is declared
- Types of variable scopes:
- Class variables
- Method variables
- Block variables
- Class names must be unique
- Variable names in the same class must be unique
- Variable names in the same method must be unique
- If class variable and method/block variables have the same name, the method/block variable overrides the class variable.
- Once a variable goes out of scope, it is garbage collected
guess the scope!
public class MyClass {
int number;
String myString;
public static void main (String [] args) {
number = 2;
myString = "A Sentence.";
String anotherString = myString;
{
int blockNum = 62;
number = blockNum;
}
}
}
- What is the scope of:
- number
- blockNum
- anotherString
- myString
strings
- String is not a primitive data type.
- String is a special Class.
- String data is stored as a char array
- Strings have methods
- Common String methods:
- equals, compareTo, length, split, replace, trim
immutability
- Strings are immutable
- Once created, they cannot be changed
- Any String modifications result in a new String
- Even concatenation creates a new String
- What happens to a String when it is no longer being referenced?
Common string manipulation
- StringBuilder
- Similar to String Class, but mutable.
- Very efficient at appending.
StringBuilder sb = new StringBuilder();
System.out.println(sb.append("Test").append("a string").append("building").toString());
- String.format(String format, Object... args)
- Easy way to dynamically build Strings
System.out.println(String.format("%s %s %s", "Test", "a string", "building"));
string comparison
- Comparison operator == is only used for primitive types
- For String, use the equals() method
String a = new String("Test");
String b = new String("Test");
if(a == b)
System.out.println("equal");
else
System.out.println("not equal");
String a = new String("Test");
String b = new String("Test");
if(a.equals(b))
System.out.println("equal");
else
System.out.println("not equal");
string comparison
- When not explicitly initializing a new String, the Java Compiler may reuse memory space in a String literal
String a = "Test"; String b = "Test"; if(a == b) System.out.println("equal"); else System.out.println("not equal");
- a and b point to the same "Test"
- Since Strings are immutable, you don't have to worry about one changing the other
JF Lecture 03: Methods, Variables, Strings
By Ryan Lewis
JF Lecture 03: Methods, Variables, Strings
- 594