Java 中的點運算子是什麼?


本文將幫助你瞭解 Java 程式語言中的點運算子是什麼。在進入點運算子之前,讓我們複習一下運算子。

運算子

在計算機程式設計中,我們經常需要執行一些算術或邏輯運算。在這種情況下,我們需要運算子來執行這些任務。因此,運算子基本上是一個符號或標記,執行算術或邏輯運算並給我們有意義的結果。參與運算的值稱為運算元。

下面是運算子的基本直觀表示。

現在,讓我們討論可用的運算子型別。

運算子型別

Java 中有三種類型的運算子,即 −

  • 算術運算子

  • 關係運算符

  • 邏輯運算子

除了這些運算子,Java 中還有一些其他運算子,例如點運算子、範圍解析運算子、instanceOf 運算子等。

點運算子

點運算子是一個語法元素,即它表示類和包、方法和類、變數和引用變數之間的分隔。它也可以稱為分隔符運算子。它主要用於將變數和方法與引用變數分隔開來,用於從包中訪問類和子包,用於訪問類或包的任何成員。

示例

public class DotExample {  // class declaration
   void calculate() {  // function to calculate area of circle
      double pi = 3.14; // initializing value of pi in variable pi
      int radius = 5; // initializing value of radius in variable radius
      int area = ((int)pi) * radius * radius; // calculating area of circle
      System.out.println("Area of circle is"  + area + "square unit"); // Displaying area of circle
   }
   public static void main(String args[]) {  // main function declaration 
      DotExample obj = new DotExample(); // object of class DotExample created
      obj.calculate(); // function calling using object of class DotExample, see dot operator is separating the object from the class function
   }
}

輸出

Area of circle is 75 square unit

更新於:05-Sep-2022

6K+ 檢視

啟動你的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.