java的一些問題

来源:http://www.cnblogs.com/z360519549/archive/2017/07/09/7140378.html
-Advertisement-
Play Games

1. 判斷是否是奇數: public static boolean isOdd(int i) { return i %2 != 0 ; } 2. System.out.println(2.0 - 1.1); 輸出:0.89999999 99999999 (Double型的) System.out.p ...


1. 判斷是否是奇數:

public static boolean isOdd(int i) { return i %2 != 0 ; }

 

2. System.out.println(2.0 - 1.1); 輸出:0.89999999 99999999 (Double型的)

   System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.10")));   --輸出 0.90 要加上引號,否則會有小數。

 System.out.println(new BigDecimal("2.01").subtract(new BigDecimal("1.65")));   -- 輸出 0.36

System.out.println(new BigDecimal(2.0).subtract(new BigDecimal(1.1)));  -- 輸出    0.899 99999999 99999111 82158029 98747676 61094665 52734375

System.out.printf("%.2f\n", 2.0-1.115);   --輸出:0.89

 

 

final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(24 * 60 * 60 * 1000 * 1000);                              --- 輸出: 500654080  , 當做了int型,導致越界。
System.out.println(24 * 60 * 60 * 1000 );                                        --- 輸出: 86400000 
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);         --- 輸出 :5

 

對於長整型的計算,要加上L:  System.out.println(24L * 60 * 60 * 1000 * 1000);  --- 輸出: 86400000 000

 

System.out.println("H" + "a"); 輸出: Ha

System.out.println("H" + 'a');     Ha

System.out.println('H' + 'a');    169

 

char[] numbers = {'1','2','3'};

System.out.println("a" + numbers);     輸出:a[C@c3c749

void java.io.PrintStream.println(String x)

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

 

System.out.println(numbers);  輸出:123

void java.io.PrintStream.println(char[] x)     重載的方法

Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

 

 

\u0022 是雙引號的unicode編碼。

System.out.println("a\u0022 + \u0022b ".length());   相當於: System.out.println("a" + "b ".length());  , 輸出 a2  。(b後面有一個空格)

 

 

System.out.println(Test.class.getName().replace(".","/"));   輸出: com/Test 

String java.lang.String.replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

 

System.out.println(Test.class.getName().replaceAll(".","/"));   輸出:////////              

System.out.println(Test.class.getName().replaceAll("\\.","/"));   輸出:com/Test

String java.lang.String.replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)

 

StringBuffer word = null;
word = new StringBuffer('P');

‘P’ 當做了int數。

java.lang.StringBuffer.StringBuffer(int capacity)

Constructs a string buffer with no characters in it and the specified initial capacity.

Parameters:
capacity the initial capacity.


System.out.println(word);                           輸出換行
System.out.println(word.append("a"));      輸出 a

 

 

int j = 0;
for(int i = 0; i < 100; i++){
j = j++;
System.out.println(j);
}

輸出100 行 0 (j的值總是 0):

0

0

……

 

 

final int END=Integer.MAX_VALUE;                    //2147483647
final int START = END - 100;
int count = 0;
for(int i = START; i <= END; i++){
  count++;
  System.out.println(i);     -- 死迴圈。  當 i 達到 2147483647 ,再增加會變成負數。
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Redis主從架構持久化存在一個問題,即前次測試的結論,持久化需要配置在主實例上才能跨越實例保證數據不丟失,這樣以來主實例在持久化數據到硬 盤的過程中,勢必會造成磁碟的I/O等待,經過實際測試,這個持久化寫硬碟的過程給應用程式帶來的影響無法忍受;因而在大多數場景下,會考慮把持久化配置 在從實例上,當 ...
  • Redis的複製功能是完全建立在之前我們討論過的基於記憶體快照的持久化策略基礎上的,也就是說無論你的持久化策略選擇的是什麼,只要用到了redis的複製功能,就一定會有記憶體快照發生,那麼首先要註意你的系統記憶體容量規劃,原因可以參考我上一篇文章中提到的Redis磁碟IO問題。 Redis複製流程在Slav ...
  • 那點所謂的分散式——redis 日常開發中,總會接觸到一些好玩的東西,比如這篇的redis,一說到redis,可能就有人跟memcache做比較了,是呀, memcache只能說是簡單的kv記憶體數據結構,而redis支持的數據類型就豐富多了,當然最能讓人看上眼的就是SortedSet。 有了它,我們 ...
  • NoSQL簡介 介紹Redis前,我想還是先認識下NoSQL,即not only sql, 是一種非關係型的數據存儲,key/value鍵值對存儲。現有Nosql DB 產品: redis/MongoDB/Memcached/Hbase/Cassandra/ Tokyo Cabinet/Voldem ...
  • SQL Server 是Microsoft 公司推出的關係型資料庫管理系統。具有使用方便可伸縮性好與相關軟體集成程度高等優點,可跨越從運行Microsoft Windows 98 的膝上型電腦到運行Microsoft Windows 2012 的大型多處理器的伺服器等多種平臺使用。Microsoft ...
  • [root@localhost ~]# yum install -y mysql-server mysql mysql-devel [root@localhost ~]# service mysqld start mysql> grant all privileges on *.* to 'root ...
  • 一.文件系統概述 1.文件系統是基於操作系統的,用來管理和組織保存在磁碟驅動器上的數據的系統軟體,通過對數據存儲佈局/空間管理/文件命名/安全控制等 方面的管理,解決瞭如何在設備上有效的存儲數據。 2.文件系統是操作系統與磁碟設備之間交互的一個橋梁,通過文件系統實現了數據合理組織和有效存取,表現在操 ...
  • pwd print working directory 列印工作目錄hostname my computer's network name 電腦在網路中的名稱mkdir make directory 創建路徑cd change directory 改變路徑ls list directory 列出路徑... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...