EE5805-Java-Lecture2:Objects and class

Intended Learning Outcomes

  • To understand relationship between objects and classes.
  • To understand the use of constructors.
  • To use private modifier to protect data fields
  • To allow controlled access of private fields through getter or setter.
  • To know the difference between instance and static variables and methods.
  • To determine the scope of variables in the context of a class.
  • To use the keyword this as the reference to the current running object
  • To store and process objects in arrays.
Constructors
  1. Class is a construct that defines objects of the same type.
  2. A class provides a special type of methods, known as constructors, which are automatically invoked when an object is created.
  3. constructors mush have the same name as the class ifself
  4. constructors don’t have a return type
  5. Using new operator when an object is created
    请添加图片描述
  6. Constructor play the role of initializing objects.You should place your code of initialization inside a constructor.
new Circle();//without args
new Circle(5.0);//with args

Default Constructor

  1. A class may be declared without constructors. So,a no-arg constructor with an empty body is implicitly declared in the class.
    Automatically inserted by compiler

Declaring objects

similar to declaring a variable of primitive data types, you can declare a reference variable for an object,

Circle myCircle;
Circle myCircle=new Circle();//assign the object to a reference variable

请添加图片描述
Java assigns no default value to local variables inside method
在 Java 中,局部变量是定义在方法内部的变量,它们在方法执行时被创建,在方法执行结束后被销毁。对于局部变量,如果没有显式地初始化,Java 不会为其赋予默认值。

这意味着,在方法中定义的局部变量,如果没有赋初值,就不能直接使用,否则编译器会报错。这是因为在 Java 中,局部变量是不会自动赋初值的,必须在使用前进行初始化。
请添加图片描述

Different between Primitive Type and Object Type

  1. Primitive types include byte, short, int, long, float, double, char, and boolean.
    They are pre-defined data types in the Java language used to store simple numerical or Boolean values.
    Primitive types are characterized by their small memory footprint, fast access, inability to be null, and being passed by value.
  2. Reference types include classes, interfaces, arrays, enumerations, etc. They are data types defined by programmers themselves and are used to store complex objects or data structures. Reference types are characterized by their large memory footprint, slow access, ability to be null, and being passed by reference.
  • Storage: Primitive type values are stored directly on the stack, while reference type values are stored on the heap, with the stack storing the reference to the object.
  • Default value: Primitive type variables are automatically initialized to their default values, such as 0 for int, while reference type variables default to null.

Data Encapsulation

Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a
restricted set of operations on the hidden (private) members of the class by executing special
(public) methods. As a result:

  1. Data fields are being protected from illegal access
  2. Maintenance of a class becomes easier in the long run

Immutable

  1. 在 Java 中,像 String、Integer、Boolean 等基本数据类型的包装类都是 immutable 的。一旦创建了这些对象,就不能再改变它们的值。

  2. A class with all private data fields and without mutators(setter method) is not necessarily immutable.

  3. For a class to be immutable, it must:

    • make all data fields private;
    • provide no setter(mutator)methods;
    • provide no getter(accessor)methods that would return a reference to a mutable data field object.
    • [may provide getter (accessor) methods to allow read-only access to its data fields, but these methods should not return a reference to a mutable data field object. Instead, they should return a copy of the data field or an immutable object that represents the data.]
  4. 在 Java 中,可以通过以下方式创建一个 immutable 对象:
    将变量声明为 final,确保其值不会被修改。
    将变量声明为 private,通过提供 getter 方法来访问其值。
    使用不可变的集合类,例如:
    Collections.unmodifiableList()、Collections.unmodifiableSet()、Collections.unmodifiableMap() 等。

Useful classes:Date and Random

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值