1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Signiture of methods
public static void main(String[] args)
Printing in Java
System.out.println
println gives \n
System.out.println("Answer = " + 3 + 5);
Answer = 35 since 3 will be treated as a string
Ways to define a string (2)
String str1 = new String("Java");
String str2 = "Java"
Pointer:
Pointer to memory for the class
No need to delete since Java has garbage collector
Shortcut:
Special shortcut for strings
Adds Java to string pool
int x = 3;
int y = 3;
if (x == y) {
System.out.println("x and y are equal!");
}
Prints fine since comparing values
if (str1 == str2) {
System.out.println("Equal")
}
if (str1.equals(str2)) {
System.out.println("Equal")
}
First one won’t print since str1 and str2 initialized differently
Second one is intended method
str1 and str2 both initialized by shortcut method (String str = “Java”)
if (str1 == str2) {
System.out.println("Equal")
}
Prints fine since both are memory addresses pointing to the same string pool
str3.replace("+", "-")
What does this do?
Nothing! Since strings are immutable, replace returns pointer to newly created string in string pool