Java中Integer方法實例詳解
Integer方法
Integer類提供了許多方法來操作整數(shù)值。
定義
public final class Integer extends Number implements Comparable<Integer> { // 類的成員變量和方法 }
分析
Integer類是final類,這意味著它不能被繼承。它實現(xiàn)了Comparable接口,這使得我們可以對Integer對象進行比較。此外,它還繼承了Number類,這意味著我們可以將Integer對象轉換為其他數(shù)字類型,如byte、short、long、float和double。
常用方法
- parseInt(String s):將字符串s解析為一個整數(shù)值并返回。
- valueOf(int i):返回一個表示指定整數(shù)值i的Integer對象。
- intValue():返回Integer對象的值作為一個int類型。
- compareTo(Integer anotherInteger):將Integer對象與anotherInteger進行比較,如果相等則返回0,如果小于anotherInteger則返回負數(shù),如果大于anotherInteger則返回正數(shù)。
- toString():返回Integer對象的字符串表示。
- equals(Object obj):將Integer對象與obj進行比較,如果obj是一個Integer對象且值相等則返回true,否則返回false。
- hashCode():返回Integer對象的哈希碼值。
- toBinaryString(int i):返回一個表示整數(shù)值i的二進制字符串。
- toHexString(int i):返回一個表示整數(shù)值i的十六進制字符串。
- toOctalString(int i):返回一個表示整數(shù)值i的八進制字符串。
基本類型包裝類
將基本數(shù)據(jù)類型封裝成對象的好處是可以通過對象調(diào)用方法操作數(shù)據(jù)
常用操作:用于基本數(shù)據(jù)類型與字符串之間的轉換
- byte->Byte
- short->Short
- int->Integer
- long->Long
- float->Float
- double->Double
- char->Character
- boolean->Boolea
Integer類在對象中包裝基本類型int的值
構造方法
- Integer(int value):根據(jù)int值創(chuàng)建Integer對象
- Integer(String s):根據(jù)String值創(chuàng)建Integer對象
成員方法
- static Integer valueOf(int i):返回表示指定的int值的Integer實例
- static Integer valueOf(String s):返回一個保存指定值的Integer對象String
代碼示例
public class crj { public static void main(String[] args) { // //Integer(int value): // Integer i1=new Integer(100); // System.out.println(i1); // //Integer(String s) // Integer i2=new Integer("100"); // System.out.println(i2); // static Integer valueOf(int i):返回表示指定的int值的Integer實例 // static Integer valueOf(String s):返回一個保存指定值的Integer對象String Integer i1=Integer.valueOf(100); Integer i2=Integer.valueOf("100"); System.out.println(i1); System.out.println(i2); } }
int String 類型相互轉換
代碼示例
public class crj { public static void main(String[] args) { //int->String int number = 100; //方法1 String s1 = number + ""; System.out.println(s1); //方法2 static String ValueOf(int i) String s2 = String.valueOf(number); System.out.println(s2); //String->int String s = "100"; //方法1 String->Integer->int Integer i = Integer.valueOf(s); int x = i.intValue(); System.out.println(x); //方法2 static int parseInt(String s) int y = Integer.parseInt(s); System.out.println(y); } }
綜合案例:猜數(shù)字
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class crj { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setTitle("猜數(shù)字"); jf.setSize(400, 300); jf.setDefaultCloseOperation(3); jf.setLocationRelativeTo(null); jf.setAlwaysOnTop(true); jf.setLayout(null); //產(chǎn)生數(shù)字 Random r = new Random(); int number = r.nextInt(100) + 1; //提示信息 JLabel messageLable = new JLabel("系統(tǒng)產(chǎn)生了一個1~100之間的數(shù)字,請猜一猜"); messageLable.setBounds(70, 50, 350, 20); jf.add(messageLable); //猜數(shù)字文本框 JTextField numberFiled = new JTextField(); numberFiled.setBounds(120, 100, 150, 20); jf.add(numberFiled); //猜數(shù)字按鈕 JButton guessButton = new JButton("我猜"); guessButton.setBounds(150, 150, 100, 20); jf.add(guessButton); guessButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //猜的數(shù)據(jù)不能為空 String StringNumber = numberFiled.getText().trim(); if (StringNumber.equals("")) { JOptionPane.showMessageDialog(jf, "猜的數(shù)字不能為空"); return; } //每次根據(jù)數(shù)字給出提示 int guessNumber = Integer.parseInt(StringNumber); if (guessNumber > number) { JOptionPane.showMessageDialog(jf, "你猜的數(shù)字"+guessNumber+"大了"); numberFiled.setText(""); }else if(guessNumber<number){ JOptionPane.showMessageDialog(jf,"你猜的數(shù)字"+guessNumber+"小了"); numberFiled.setText(""); }else{ JOptionPane.showMessageDialog(jf,"恭喜你猜中了"); } } }); jf.setVisible(true); } }
總結
到此這篇關于Java中Integer方法實例詳解的文章就介紹到這了,更多相關Java Integer方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)批量查找與替換Excel文本的思路詳解
在 Java 中,可以通過find和replace的方法來查找和替換單元格的數(shù)據(jù),下面小編將以Excel文件為例為大家介紹如何實現(xiàn)Excel文件內(nèi)容的批量替換,感興趣的朋友跟隨小編一起看看吧2023-10-10Java多線程編程之訪問共享對象和數(shù)據(jù)的方法
這篇文章主要介紹了Java多線程編程之訪問共享對象和數(shù)據(jù)的方法,多個線程訪問共享對象和數(shù)據(jù)的方式有兩種情況,本文分別給出代碼實例,需要的朋友可以參考下2015-05-05Java Comparable及Comparator接口區(qū)別詳解
這篇文章主要介紹了Java Comparable及Comparator接口區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07springboot mybatis-plus實現(xiàn)登錄接口
本文主要介紹了springboot mybatis-plus實現(xiàn)登錄接口,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11