Skip to Content
202506Programming Basics (Java)

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: 小数 しょうすう

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 complete
  • int apple; creates a variable named apple of type int (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 variable apple.
  • ; 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 variable apple to the console
  • this time we use apple without int because we are using the variable, not creating it
  • actually, when we assign 2 to apple at line 3, we are also using it
  • To sum up
    • Line 1: we create a variable named apple of type int
    • Line 3: we assign the value 2 to the variable apple
    • Line 5: we use the variable apple to print its value to the console

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.

  • can you see the space? It makes the code more readable.

Integer Types: 整数型 せいすうがた

  • Integer types are byte, short, int, and long.

Floating Point Types(Decimal): 浮動小数点 ふどうしょうすうてん 小数 しょうすう

  • Floating point (Decimal) types are float and double.

Primitive Type

Reference Type

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 returns true
  • != - Not equal to (等しくない ひとしくない): 5 != 3 returns true
  • < - Less than (より小さい よりちいさい): 3 < 5 returns true
  • > - Greater than (より大きい よりおおきい): 5 > 3 returns true
  • <= - Less than or equal to: 3 <= 5 returns true
  • >= - Greater than or equal to: 5 >= 5 returns true

Logical Operators: 論理演算子 ろんりえんざんし

  • && - AND (論理積 ろんりせき): true && false returns false
  • || - OR (論理和 ろんりわ): true || false returns true
  • ! - NOT (否定 ひてい): !true returns false

Assignment Operators: 代入演算子 だいにゅうえんざんし

  • = - Assignment (代入 だいにゅう): int x = 5;
  • += - Add and assign: x += 3 (same as x = x + 3)
  • -= - Subtract and assign: x -= 2 (same as x = x - 2)
  • *= - Multiply and assign: x *= 4 (same as x = x * 4)
  • /= - Divide and assign: x /= 2 (same as x = 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

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

Last updated on
Do not shoot this.