1/14
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
Java returns a __ to the _ of the array
pointer, first element
Code to create an array of 10 unknown doubles
double[] data = new double(10);
Code to create an array of ints containing 1,2,3
int[] nums = {1,2,3};
how to call length of an array called nums
nums.length
Contants use the __ keyword
final
Meaning of static keyword
Thing belongs to class itself, not an instance
Thing about accessing null in Java
Can print null, but can’t access null using . notation