Java for Beginners - Part 1

In the Nutshell 🌰

  • Java variables require a type that will be kept throughout its lifetime.
  • The compiler can catch type errors at compile time that Python can’t catch until runtime.
🖊️
Java 语言中的variables一旦确认类型将不会被允许修改,而这样的设定可以让Java在编译的过程中就可以发现数据类型错误。(Python 只能在runtime才可以发现)

🏗️ Building Blocks

notion image

📐 Class and Object

A class is a blueprint, while an object is an instance of the class. A class can have fields (attributes), constructors and methods (and more, but not too important now).
🖊️
Class类似于一个蓝图,而Object是一个Class的例子。一个Class中可以包含 参数(fields / attributes), 构建函数(constructors) 和 函数(methods) 以及其他内容.
最简单的Class就可以就只是一个空的Class,但注意的是文件名需要与之匹配。 CoffeeMachine.java

🏷️ Fields and Types

Java 自带的primitive type 有
  • boolean: true or false
  • char: for single Unicode characters
  • byte, short, int, long: integers that are 8, 16, 32, and 64 bits wide, respectively
  • float: IEEE 32-bit floating-point number
  • double: IEEE 64-bit floating-point number
当然Java 也有自带的Class,其中最常用的是 String
String 的S因该大写,因为它并不是一个primitive type

类型转换

理解存在即可
  • Widening Casting (automatically) - converting a smaller type to a larger type size
    • 系统的扩容
      byte -> short -> char -> int -> long -> float -> double
  • Narrowing Casting (manually) - converting a larger type to a smaller size type
    • 人为的强制转换,会损失精准度
      double -> float -> long -> int -> char -> short -> byte

🛠️ 构架函数

构建函数的名字因该保持与Class的名字一致,且不声明return type。
🖊️
可以将其视为“返回”该类构造的实例,但无需在代码中显式返回该实例——只需设置对象的初始状态,然后像从一个无返回值的方法中返回一样返回即可。
notion image
notion image
 

Java vs Python: Quick Notes 📝

 
The code blocks on the left and right had been lined up for comparison. By looking at how Java and Python handle the same structure, you can probably get the idea a lot faster.
 
☕ Java class
🐍 Python class
 
 

1. 🏗️ Constructors

  • Java
    • Uses ClassName(...) constructor.
    • Has a default constructor if none is defined.
    • Custom constructors allowed.
    • Instance variables must be declared outside the constructor.
    • No default parameter values (unlike Python/C++):
    • Python
      • Uses __init__() method.
      • Supports default parameter values:

      2. 🤝 this (Java) vs self (Python)

      • Java this.
        • Implicit → compiler automatically adds it.
        • Keyword (fixed, cannot rename).
        • Constructor chaining:
        • Python self
          • Explicit → must be the first parameter.
          • Convention → usually named self, but not a keyword.
          • Used like a normal variable.

        3. 🔒 Access Modifiers (Java)

        Access Levels

        Non-Access Modifiers

        Modifier
        Accessible by…
        public (C)
        Any class
        default (C)
        Classes in the same package
        private
        Declared class only
        protected
        Same package & subclasses
        Modifier
        Description
        final (C)
        No inheritance, methods can’t be overridden
        abstract (C)
        Must be subclassed, may have abstract methods
        static
        Belongs to class, not instance
        transient
        Skips serialization
        synchronized
        Thread-safe execution
        volatile
        Ensures variable visibility across threads

        🐍 Python Access Control (Convention)

        • Publicname → accessible anywhere.
        • Protected_name → intended for internal use (but still accessible).
        • Private (Name Mangling)__name → renamed to _ClassName__name.
        💡
        But still, how to access private attributes?