Programming Basics (Java)
Integer: 整数 せいすう
- A whole number that can be positive, negative, or zero, without any fractional or decimal parts.
- Example:
42
,-15
,0
,1000
Decimal: 小数 しょうすう
-
Any numeral value that consists of a decimal point is a decimal number.
-
Example:
3.14
,2.5
,0.001
,10.0
-
https://www.shinko-keirin.co.jp/keirinkan/sansu/WebHelp/03/page3_24.html
Create a Variable
int apple;
int
means “this is an integer”apple
is the name of the variable;
is the end of the statement, indicating that this line of code is completeint apple;
creates a variable namedapple
of typeint
(integer).
Assign a Value to a Variable
int apple;
apple = 2;
apple
is the variable we created earlier.=
is the assignment operator, “put the right side value into the left side”2
is the value being assigned to the variableapple
.;
is the end of the statement, indicating that this line of code is complete
Use a Variable
int apple;
apple = 2;
System.out.println( apple );
System.out.println(apple);
prints the value of the variableapple
to the console- this time we use
apple
withoutint
because we are using the variable, not creating it - actually, when we assign
2
toapple
at line 3, we are also using it - To sum up
- Line 1: we create a variable named
apple
of typeint
- Line 3: we assign the value
2
to the variableapple
- Line 5: we use the variable
apple
to print its value to the console
- Line 1: we create a variable named
Try to create a variable named orange
and assign it the value 5
, then print it to the console.
Create a Variable and Assign a Value to It in One Line
int banana = 2;
This is an important pattern.
[data type] [variable name] [assignment operator] [value] [semicolon]
- Create an integer variable
- named banana
- assign it the
- value 2
- end
Try to use this pattern, then print it to the console.
Integer Types: 整数型 せいすうがた
- Integer types are
byte
,short
,int
, andlong
.
Floating Point Types(Decimal): 浮動小数点 ふどうしょうすうてん 小数 しょうすう
- Floating point (Decimal) types are
float
anddouble
.
Primitive Type
-
A data type that is not composed of other data types.
-
All the primitive types in Java are:
byte
- 8-bit signed integer (-128 to 127)short
- 16-bit signed integer (-32,768 to 32,767)int
- 32-bit signed integer (-2³¹ to 2³¹-1)long
- 64-bit signed integer (-2⁶³ to 2⁶³-1)float
- 32-bit IEEE 754 floating pointdouble
- 64-bit IEEE 754 floating pointboolean
-true
orfalse
char
- 16-bit Unicode character (‘\u0000’ to ‘\uffff’)
-
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Reference Type
-
A data type that refers to an object or an instance of a class.
-
Example:
String
,ArrayList
,HashMap
,Integer
-
https://docs.oracle.com/javase//8/docs/api/java/lang/Integer.html
Operator
- A symbol that performs operations on variables and values.
Arithmetic Operators: 算術演算子 さんじゅつえんざんし
+
- Addition (加算 かさん):5 + 3 = 8
-
- Subtraction (減算 げんざん):5 - 3 = 2
*
- Multiplication (乗算 じょうざん):5 * 3 = 15
/
- Division (除算 じょざん):15 / 3 = 5
%
- Modulo (剰余 じょうよ):15 % 4 = 3
Comparison Operators: 比較演算子 ひかくえんざんし
==
- Equal to (等しい ひとしい):5 == 5
returnstrue
!=
- Not equal to (等しくない ひとしくない):5 != 3
returnstrue
<
- Less than (より小さい よりちいさい):3 < 5
returnstrue
>
- Greater than (より大きい よりおおきい):5 > 3
returnstrue
<=
- Less than or equal to:3 <= 5
returnstrue
>=
- Greater than or equal to:5 >= 5
returnstrue
Logical Operators: 論理演算子 ろんりえんざんし
&&
- AND (論理積 ろんりせき):true && false
returnsfalse
||
- OR (論理和 ろんりわ):true || false
returnstrue
!
- NOT (否定 ひてい):!true
returnsfalse
Assignment Operators: 代入演算子 だいにゅうえんざんし
=
- Assignment (代入 だいにゅう):int x = 5;
+=
- Add and assign:x += 3
(same asx = x + 3
)-=
- Subtract and assign:x -= 2
(same asx = x - 2
)*=
- Multiply and assign:x *= 4
(same asx = x * 4
)/=
- Divide and assign:x /= 2
(same asx = x / 2
)
Increment/Decrement: 増減演算子 ぞうげんえんざんし
++
- Increment (増加 ぞうか):x++
or++x
(adds 1)--
- Decrement (減少 げんしょう):x--
or--x
(subtracts 1)
What is the difference between ++i
and i++
?
The key insight is that both always increment the variable , but they differ in what value they return to be used in the surrounding expression.
++i
: “Increment i, then give me the result”i++
: “Give me i’s current value, then increment i”
Try this example:
int i = 5;
System.out.println(++i); // Prints: 6 (i is now 6)
int j = 5;
System.out.println(j++); // Prints: 5 (j becomes 6 after printing)
ASCII code
-
ASCII (American Standard Code for Information Interchange) is a character encoding standard that uses numbers to represent characters.
In the old days, computers can only display characters, like the command line(terminal) in your computer. That is CLI(Command Line Interface).
Computer only understand 0 and 1, so people create a table, use a number to represent a character
Find the character of these: 76 48 86 101
In this ascii table
The answer is L 0 V e
What if I want a red heart? ❤️
It is in the unicode U+2764
Unicode is a superset of ASCII . You can see in this unicode table
76 48 86 101
is also L 0 V e
ASCII Art
In CLI, People do things like this:
# User type this
echo "Hello!"
# Computer displays this
Hello!
It’s boring! Then some programmer created a program called cowsay
# User type this
cowsay "Hello!"
# Computer displays this
________
< Hello! >
--------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ASCII Game
Are you a gamer? Do you want to play game in CLI with full screen of ASCII characters?
A classic:
A modern:
A dream:
UI(User Interface)
User Interface (UI) is the space where interactions between humans and machines occur. For example, when you use “google search”, you talk to “google search” by typing into the input box, and it talks back to you by displaying the search results on the screen. What you see and interact with is the UI.
API(Application Programming Interface)
CSV(Comma-Separated Values)
Let’s say I have 2 red apples, 4 green apples, 3 yellow bananas, 6 black bananas. I want to use a text file to store it.
fruit , color , quantity
apple , red , 2
apple , green , 4
banana , yellow , 3
banana , black , 6
There is a table header, containing 3 columns: “fruit”, “color”, and “quantity”.
Below that are several rows of data. This entire file is separated by commas, and new line for each row.
This is a CSV file. This is a readable table. And yes, you can import it into a spreadsheet software like Microsoft Excel or Google Sheets.
The closest documentation for CSV is the RFC 4180