1/53
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
How does System.out.print and System.out.println differ?
Both send text to standard output.
println appends a newline at the end.
print does not.
Newline escape sequence
\n
Backspace escape sequence
\b
Tab escape sequence
\t
Carriage return escape sequence
\r
Backslash escape sequence
\\
Single quote escape sequence
\'
Double quote escape sequence
\"
How do you write the main method starting point?
public static void main(String[] args) {
// code here!
}public accessible from anywhere.
static belongs to the class, not an object.
void does not return any value.
main the name of the method.
String[] args parameter for command-line arguments.
Single line comment
// Everything on this line is ignored.Multi-line comment
/* Everything in-between
is ignored */Javadoc comment
/** Generates documentation, prints to screen */Variable
A named memory location with a declared type.
Must be declared before use.
Must be initialised before reading.
What is the difference between variable declaration and assignment?
Declaration tells Java the name and type of the variable.
Assignment gives the variable a value.
int value; // declaration
value = 5; // assignment
int value = 5; // both in one-lineLiteral
A fixed value written directly in code.
Static typing
The type of every variable must be declared on creation.
Once declared, you can not assign a different type to the variable.
Why is static typing used?
Promotes better performance.
Makes code more robust.
Identifiers cannot start with a digit or use reserved keywords.
True
Identifiers can use letters, digits, _, and $.
True
Identifiers are case-sensitive and cannot contain spaces.
True
What naming convention do variables and methods use?
lowerCamelCase
What naming convention do classes use?
UpperCamelCase
What naming convention do constants use?
UPPER_SNAKE_CASE
How are constants declared?
Using the final keyword, for example:
final int DAYS_IN_YEAR = 365;Constants can change value once initialised.
False
What are Java’s eight primitive types?
byte, short, long
int, float, double, char, boolean
Primitive types store raw values and are fast.
True
How do integer and floating-point types differ?
Integer types (byte, short, int, long) store whole numbers.
Floating types (float, double) store fractional values.
What is the default type for floating-point literals?
They are double by default, but we can append F or f to make it a float.
Literals cannot include commas or currency symbols.
True
What type of quotes do char values and Strings use?
char literals use single quotes. 'A'
Strings use double quotes. "A"
What is a reference type?
Reference types (arrays, Strings, enums) store the addresses of objects.
Uninitialised references default to null.
Methods are invoked on objects via references.
Assigning one reference to another makes both refer to the same object.
Primitive types in Java are predefined and built into the language, while reference types are created by the programmer (except for String).
True
Reference types can be used to call methods to perform certain operations, whereas primitive types cannot.
True
Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String)
True
Primitive types always hold a value, whereas non-primitive types can be null.
True
What is an enum?
An enum defines a fixed set of constants, for example:
enum Level = { LOW, MEDIUM, HIGH };
Level x = Level.MEDIUM;String literals may be interned in the String Pool, meaning?
Identical literals share the same reference.
How do we compare equality between Strings?
Compare content: strA.equals(strB)
Compare references: strA == strB
Strings are immutable, so their values can not be changed after creation.
True
String methods return new String instances.
True
How to align text with String formatting?
String word = new String("hi");
System.out.printf("%s...%n", word); // no padding
System.out.printf("%10s...%n", word); // right-align
System.out.printf("%-10s...%n", word); // left-alignSecond statement right-aligns word in a field of 10 characters.
Third statement left-aligns word in a field of 10 characters.
How to pad numbers with leading zeroes?
int id = 42;
System.out.printf("ID: %05d%n", id);Print 42 as a 5-digit number, padded with leading zeroes.
Dividing two integers with / produces an integer result.
True
Implicit Conversion (Widening)
When a smaller type is assigned to a larger type. No data is lost.
byte > short > int > long > float > double
For example:
int a = 10;
double b = a; // int converts to double automaticallyExplicit Conversion (Narrowing)
Must be done manually using a cast. Can result in information lost.
For example:
double x = 9.8;
int y = (int) x; // y = 9, decimal part is droppedHow to convert a boolean to another type?
This is not possible, you can not convert to a boolean or from any other type.
We can perform String to number conversion using?
String input = "123";
int number = Integer.parseInt(input);
String piStr = "3.14";
double pi = Double.parseDouble(piStr);We can perform number to String conversion using?
int value = 42;
String text = String.valueOf(value);How to read input in Java?
import java.util.Scanner;
Scanner = input = new Scanner(System.in);
System.out.print("Some kind of prompt message: ");
int num = input.nextInt(); // Some method to read input.What Scanner methods exist to read different types of input?
nextInt() reads an integer.
nextDouble() reads a floating-point number.
next() reads a single word (String).
nextLine() reads an entire line of text.
String Pool
A special memory region where string literals are stored. When a new string is created using a literal, Java checks if the string already exists in the pool:
If it exists, return the reference to the existing string.
Otherwise, create a new string in the pool.
String methods
str.charAt(3); // return character at index 3
str.substring(2,5); // substring from index 2 to 4
str.length(); // get length of string
str.indexOf('x'); // return index of first occurrence
str.split("\\s"); // splits on whitespace
str.split(","); // splits string on comma
str.concat(str); // concatenates two strings
str.trim(); // removes whitespace from both endsFor mutable String operations, use?
StringBuffer Methods
Includes append and reverse operations.