Java短類的compareTo()方法和示例

簡短的類compareTo()方法 (Short class compareTo() method)

  • compareTo() method is available in java.lang package.

    compareTo()方法在java.lang包中可用。

  • compareTo() method is used to check equality or inequality for this Short object against the given Short object mathematically or in other words, we can say this method is used to compare two Short objects.

    compareTo()方法用于在數學上檢查給定Short對象與此給定Short對象的相等性或不相等性,換句話說,可以說此方法用于比較兩個Short對象。

  • compareTo() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    compareTo()方法是一種非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。

  • compareTo() method does not throw an exception at the time of comparing the Short object.

    在比較Short對象時, compareTo()方法不會引發異常。

Syntax:

句法:

    public int compareTo(Short value2);

Parameter(s):

參數:

  • Short value2 – represents the Short object to compare with.

    短值2 –表示要比較的短對象。

Return value:

返回值:

The return type of this method is int, it returns an integer value based on the following cases,

此方法的返回類型為int ,它基于以下情況返回整數值:

  • It returns 0 if value1 is mathematically equal to value2.

    如果value1在數學上等于value2 ,則返回0

  • It returns the value < 0 if value1 is mathematically less than value2.

    如果value1在數學上小于value2 ,則返回值<0

  • It returns the value > 0 if value1 is mathematically greater than value2.

    如果value1在數學上大于value2,則它返回> 0的 值 。

Example:

例:

// Java program to demonstrate the example 
// of compareTo(Short value2) method of  Short class
public class CompareToOfShortClass {
public static void main(String[] args) {
// Variables initialization
short s1 = 10;
short s2 = 20;
// Short instance 
Short value1 = new Short(s1);
Short value2 = new Short(s2);
// It compare two Short objects and placed the 
// result in another variable (compare) of integer type
int compare = value1.compareTo(value2);
// Display result
System.out.println("value1.compareTo(value2): " + compare);
System.out.println();
if (compare == 0)
System.out.println("value1 is equal to value2");
else if (compare < 0)
System.out.println("value1 is less than value2");
else
System.out.println("value1 is greater than value2");
}
}

Output

輸出量

value1.compareTo(value2): -10value1 is less than value2

翻譯自: https://www.includehelp.com/java/short-class-compareto-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541666.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541666.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541666.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

四則運算網頁版

一.設計思想&#xff1a; 1&#xff09;寫出一個菜單界面&#xff0c;有兩個選項一個是分數&#xff0c;一個是整數。 2&#xff09;而這兩個標簽后面則是轉向其更詳細的菜單&#xff0c;題目數量&#xff0c;有無括號&#xff0c;運算的項數等等詳細功能&#xff0c;再點擊這兩…

Java RandomAccessFile seek()方法與示例

RandomAccessFile類seek()方法 (RandomAccessFile Class seek() method) seek() method is available in java.io package. seek()方法在java.io包中可用。 seek() method is used to sets the file pointer position calculated from the starting of this file at which the …

Javascript開發技巧(JS中的變量、運算符、分支結構、循環結構)

一、Js簡介和入門 繼續跟進JS開發的相關教程。 <!-- [使用JS的三種方式] 1、HTML標簽中內嵌JS&#xff08;不提倡使用&#xff09;&#xff1a; 示例&#xff1a;<button οnclick"javascript:alert(你真點啊&#xff01;)">有本事點我呀&#xff01;&#…

android 顏色范圍,Android系統顏色的適用范圍

###All Clickable Views:ripple effect (Lollipop only) — “colorControlHighlight”###Status Bar:background (Lollipop only) – “colorPrimaryDark”###Navigation Bar:background (Lollipop only) – “android:navigationBarColor”###EditText:underline (unfocused)…

bytevalue_Java Short類byteValue()方法及示例

bytevalue短類byteValue()方法 (Short class byteValue() method) byteValue() method is available in java.lang package. byteValue()方法在java.lang包中可用。 byteValue() method is used to return the value denoted by this Short object converted to type byte (by …

分布式交換機配置備份和還原

1.備份和還原vSphere Distributed Switch配置 1.1導出 vSphere Distributed Switch 配置 可以將 vSphere Distributed Switch 和分布式端口組配置導出到某一文件。該文件保留有效的網絡配置&#xff0c;使這些配置能夠傳輸至其他環境。 步驟&#xff1a; 1) 在 vSphere Web Cli…

html自動執行函數,JS 自執行函數原理及用法

js自執行函數&#xff0c;聽到這個名字&#xff0c;首先會聯想到函數。接下來&#xff0c;我來定義一個函數&#xff1a;function aaa(a,b){return sum a b}定義了一個名為aaa的函數&#xff0c;在里面可以計算兩個數的和。如果想執行它&#xff0c;就必須得調用它&#xff0…

java reverse_Java Integer類reverse()方法與示例

java reverse整數類reverse()方法 (Integer class reverse() method) reverse() method is available in java.lang package. reverse()方法在java.lang包中可用。 reverse() method is used to returns the value generated by reversing the order of bits in binary 2s comp…

華為鴻蒙系統好在哪,華為鴻蒙2.0可以替代安卓嗎,華為鴻蒙2.0優勢在哪

在華為開發者大會上&#xff0c;華為消費業務CEO 余承東&#xff0c;正式發布鴻蒙OS2.0&#xff0c;并宣布華為鴻蒙OS將全面啟用全場景生態&#xff0c;并將于2020年12月發布手機版。余承東還表示&#xff0c;明年&#xff0c;華為的智能手機將全面升級&#xff0c;以支持鴻蒙操…

Java GregorianCalendar add()方法與示例

GregorianCalendar類的add()方法 (GregorianCalendar Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given quantity to the specified GregorianCalendar field (fi). add()方法用于…

JVM(三)——對象的訪問定位

JVM&#xff08;三&#xff09;——對象的訪問定位創建完對象&#xff0c;到了使用對象的時候&#xff0c;通常聲明一個同類型的引用指向該類型的對象&#xff0c;由這個引用來操作對象的字段、方法等。 Object obj new Object();復制代碼我們的Java程序需要通過棧上的 refere…

html5滑動刪除置頂,html5向左滑動刪除特效

html5向左滑動刪除特效* {padding: 0;margin: 0;list-style: none;}header {background: #f7483b;border-bottom: 1px solid #ccc}header h2 {text-align: center;line-height: 54px;font-size: 16px;color: #fff}.list-ul {overflow: hidden}.list-li {line-height: 60px;bord…

Java Double類hashCode()方法及示例

雙類hashCode()方法 (Double class hashCode() method) hashCode() method is available in java.lang package. hashCode()方法在java.lang包中可用。 hashCode() method is used to return hashcode of the Double-object. hashCode()方法用于返回Double對象的哈希碼。 hashC…

Java DataOutputStream size()方法及示例

DataOutputStream類的size()方法 (DataOutputStream Class size() method) size() method is available in java.io package. size()方法在java.io包中可用。 size() method is used to return the size of this stream or in other words, we can say it returns the value of…

html怎樣讓列表向下移動,Html無序列表ul控件實現行上下移動.

new document // 創建一個ulvar ul document.createElement("ul");ul.id "oList";// 創建divvar div document.getElementById(mergeCase);// 把ul放置到div中div.appendChild(ul);// 遍歷所有的li條目function trace(){var strTest "";for …

創建laravel項目

下載項目到本地 git clone https://github.com/251068550/LaraBlog.gitcompoer安裝 cd LaraBlog composer install如果composer install安裝很慢&#xff0c;推薦安裝國內鏡像 執行 composer config -g repo.packagist composer https://packagist.phpcomposer.com 配置.env文件…

Java ArrayList removeRange()方法與示例

ArrayList類removeRange()方法 (ArrayList Class removeRange() method) removeRange() method is available in java.util package. removeRange()方法在java.util包中可用。 removeRange() method is used to remove the elements whose range is between st_in(start index)…

Spring注釋事務失效及解決辦法

如果帶上事務&#xff0c;那么用annotation方式的事務注解和bean配置&#xff0c;事務會失效&#xff0c;要將service bean配置到xml文件中才行 在主容器中&#xff08;applicationContext.xml&#xff09;&#xff0c;將Controller的注解排除掉 <context:component-scan …

html5畫分形圖形,2.5 繪制透明圖形 - HTML5 Canvas 實戰

對于需要圖形分層的應用&#xff0c;經常需要處理透明度。本節&#xff0c;我們將學習如何使用全局透明度設置圖形的透明度。圖2-5 繪制透明圖形繪制步驟按照以下步驟&#xff0c;在一個不透明的矩形之上&#xff0c;繪制一個透明的圓&#xff1a;1. 定義2D畫布上下文&#xff…

計算機網絡子網劃分_子網劃分和超網| 計算機網絡

計算機網絡子網劃分1)子網劃分 (1) Subnetting) Subnetting is a concept of diving a block of addresses into sub-blocks of addresses. During the era of classful addressing (as it’s obscured now), subnetting was introduced. Say, an organization is granted a la…