or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Week 3
OOP: Classes and Objects
Java: Objects
System
belongs to the java.lang package, which is imported automatically.new
operator.null
value. As a result, trying to access spot.x attribute or invoking the spot.translate method results in aNullPointerException
.In Java, you don’t have to delete objects you create when they are no longer needed. As your program runs, the system automatically looks for stranded objects and reclaims them; then the space can be reused for new objects.
Java: Classes
this
keyword is a reference variable in Java that refers to the current object.this
can be used to refer to a constructor of a class within the same class too.OOP, Java: Class-Level Members
static
modifier.static
modifier, in combination with thefinal
modifier, is also used to define constants.System.out.println(...)
:out
is a class-level public attribute of the System class.println
is a instance level method of the out object.Java: Useful Classes
The
String
classcharAt
returns a char, a primitive type that stores an individual character (as opposed to strings of them).
toCharArray
toUpperCase
/toLowerCase
a string method cannot change the string object on which the method is invoked, because strings are immutable.
invoking the method has no effect if you don’t assign the return value to a variable.
replace
Accessing substrings:
substring
"banana".substring(0) -> "banana"
"banana".substring(2) -> "nana"
"banana".substring(6) -> ""
"banana".substring(0, 3) -> "ban"
"banana".substring(2, 5) -> "nan"
"banana".substring(6, 6) -> ""
Searching within strings:
indexOf
"banana".indexOf('a') -> 1
"banana".indexOf('a', 2) -> 3 searches for 'a', starting from position 2
"banana".indexOf('x') -> -1
"banana".indexOf("nan") -> 2 searches for the substring "nan"
Comparing Strings:
equals
compareTo
equalsIgnoreCase
If the strings differ, you can use
compareTo
to see which comes first in alphabetical order. The return value from compareTo is the difference between the first characters in the strings that differ.If the strings are equal, their difference is zero. If the first string comes first in the alphabet, the difference is negative. Otherwise, the difference is positive.The uppercase letters come before the lowercase letters, so "Ada" comes before "ada".
To check if two strings are similar irrespective of the differences in case, you can use the
equalsIgnoreCase
method.Others:
contains
: checks if one string is a sub-string of the other e.g., Snapple and appstartsWith
: checks if one string has the other as a substring at the beginning e.g., Apple and AppendsWith
: checks if one string has the other as a substring at the end e.g., Crab and abWrapper classes provide methods for parsing strings to other types.
ex. Integer.parseInt("1234") -> 1234
Wrapper classes also provide toString, which returns a string representation of a value.
ex. Integer.toString(1234) -> "1234"
The
Arrays
classThe
Scanner
classScanner
is a class that provides methods for inputting words, numbers, and other data.nextLine
that reads a line of input from the keyboard and returns a String.Code Quality: Naming
RCS: Using History