The content of this article is to introduce what objects and classes are in Java, so that everyone can understand the connection between objects and classes in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In Java, objects can be: physical entities and logical entities, while classes can only be: logical entities. Let's take a closer look at what an object is and what a class is.
What are objects in java?
Entities with state and behavior are called objects, for example: table, chair , bicycles, cars, airplanes, pens, etc. Objects can be physical (tangible) or logical (intangible). Intangible objects such as banking systems.
An object has three characteristics:
State: Represents the data (value) of the object.
Behavior: represents the behavior or function of the object, such as depositing and withdrawing money, writing, etc.
Identity: Object identity is usually achieved through a unique ID. External users cannot see the value of this ID. However, the JVM uses it internally to uniquely identify each object.
For example: Pencil is an object. Its name is xx pencil, which is its unique ID; its color is white, which is called its status. It is used for writing, so writing is its act.
Explanation:
Objects are entities with status and behavior that can run or have specific functions in the real world; objects are classes A member or instance (result) of .
What are classes in java?
A class is a collection of objects with common properties. It is a template for defining objects and defines the properties of objects, including valid value ranges and default values; classes also Describes object behavior. A class is not a physical entity, but a logical entity.
Classes in Java mainly include:
◇ Field
◇ Method: In Java, a method is like a user A function that exposes the behavior of an object is the process of operating an object. It has the advantages of code reusability and code optimization.
◇ Constructor
◇ Block
◇ Nested classes and interfaces
Basic syntax for declaring a class:
class <类的名字>{ 字段; 方法; ...... }
Description:
1. The main purpose of a class is to save data or information. This is accomplished through properties, which are also called data members.
2. Member functions can determine the behavior of the class, that is, provide definitions for supporting various operations on data saved in the form of objects.
Examples of objects and classes:
First let’s take a look at the new keyword in java
The new keyword is used to allocate memory at runtime. All objects obtain memory in the heap memory area.
Examples of objects and classes:
Example 1:
Created a Student class, which has two data Member id and name. Then we use the new keyword to create an object of the Student class and output the value of the object.
In the example, only one main() method is created in the class.
//Java程序,用于说明如何定义类和字段 //定义Student类 class Student{ //定义字段 int id; //字段或数据成员或实例变量 String name; //在Student类中创建main方法 public static void main(String args[]){ // 创建对象或实例 Student s1=new Student();// 创建一个Student对象 //输出对象的值 System.out.println(s1.id);// 通过引用变量访问成员 System.out.println(s1.name); } }
Output:
Example 2: Calling and using another class in one class
In actual development , we often create a class but use it in another class.
//用于演示主要方法的Java程序 //创建Student类 class Student{ int id; String name; } //创建另一个包含main方法的TestStudent1类 class TestStudent1{ public static void main(String args[]){ Student s1=new Student(); System.out.println(s1.id); System.out.println(s1.name); } }
Running results:
#Summary: A class is a template used to define an object. It specifies the names of variables that can exist in the object. and types, and a "method", a procedure that operates on these variables. Classes can be thought of as "types" and objects are the "variables" of that type.
The above is the entire content of this article, I hope it will be helpful to everyone's study. More related video tutorial recommendations: JavaTutorial!
The above is the detailed content of What are objects and classes in java. For more information, please follow other related articles on the PHP Chinese website!